feat(handlers): add system-wide scan settings handler
Create new SystemSettingsHandler for managing system-wide scan settings: - GetScanSettings: retrieve scan frequency and auto-scan status - UpdateScanSettings: update scan settings (15-1440 minutes range) - Admin-only access (no user-specific data) - Key-value based storage instead of per-user settings Replaces per-user scan settings with centralized system configuration. This handler is used by /api/libraries/scan-settings endpoints.
This commit is contained in:
@@ -0,0 +1,111 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bookhoard/internal/database"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SystemSettingsHandler struct {
|
||||||
|
db *database.Queries
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSystemSettingsHandler(db *database.Queries) *SystemSettingsHandler {
|
||||||
|
return &SystemSettingsHandler{
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateScanSettingsRequest struct {
|
||||||
|
ScanFrequencyMinutes int32 `json:"scan_frequency_minutes" validate:"required,min=15,max=1440"`
|
||||||
|
AutoScanEnabled bool `json:"auto_scan_enabled"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ScanSettingsResponse struct {
|
||||||
|
ScanFrequencyMinutes int32 `json:"scan_frequency_minutes"`
|
||||||
|
AutoScanEnabled bool `json:"auto_scan_enabled"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *SystemSettingsHandler) UpdateScanSettings(c echo.Context) error {
|
||||||
|
var req UpdateScanSettingsRequest
|
||||||
|
if err := c.Bind(&req); err != nil {
|
||||||
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request"})
|
||||||
|
}
|
||||||
|
if err := c.Validate(&req); err != nil {
|
||||||
|
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
scanFrequencyValue := strconv.FormatInt(int64(req.ScanFrequencyMinutes), 10)
|
||||||
|
autoScanValue := strconv.FormatBool(req.AutoScanEnabled)
|
||||||
|
|
||||||
|
err := h.db.UpdateSystemSetting(c.Request().Context(), database.UpdateSystemSettingParams{
|
||||||
|
SettingKey: "scan_frequency_minutes",
|
||||||
|
SettingValue: scanFrequencyValue,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if err == pgx.ErrNoRows {
|
||||||
|
return c.JSON(http.StatusNotFound, map[string]string{"error": "system setting not found"})
|
||||||
|
}
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
err = h.db.UpdateSystemSetting(c.Request().Context(), database.UpdateSystemSettingParams{
|
||||||
|
SettingKey: "auto_scan_enabled",
|
||||||
|
SettingValue: autoScanValue,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
if err == pgx.ErrNoRows {
|
||||||
|
return c.JSON(http.StatusNotFound, map[string]string{"error": "system setting not found"})
|
||||||
|
}
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, ScanSettingsResponse{
|
||||||
|
ScanFrequencyMinutes: req.ScanFrequencyMinutes,
|
||||||
|
AutoScanEnabled: req.AutoScanEnabled,
|
||||||
|
Message: "scan settings updated successfully",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *SystemSettingsHandler) GetScanSettings(c echo.Context) error {
|
||||||
|
scanFrequencySetting, err := h.db.GetSystemSetting(c.Request().Context(), "scan_frequency_minutes")
|
||||||
|
if err != nil {
|
||||||
|
if err == pgx.ErrNoRows {
|
||||||
|
return c.JSON(http.StatusOK, ScanSettingsResponse{
|
||||||
|
ScanFrequencyMinutes: 60,
|
||||||
|
AutoScanEnabled: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
autoScanSetting, err := h.db.GetSystemSetting(c.Request().Context(), "auto_scan_enabled")
|
||||||
|
if err != nil {
|
||||||
|
if err == pgx.ErrNoRows {
|
||||||
|
return c.JSON(http.StatusOK, ScanSettingsResponse{
|
||||||
|
ScanFrequencyMinutes: 60,
|
||||||
|
AutoScanEnabled: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
scanFrequency, err := strconv.ParseInt(scanFrequencySetting, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "invalid scan frequency setting"})
|
||||||
|
}
|
||||||
|
|
||||||
|
autoScanEnabled, err := strconv.ParseBool(autoScanSetting)
|
||||||
|
if err != nil {
|
||||||
|
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "invalid auto scan setting"})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, ScanSettingsResponse{
|
||||||
|
ScanFrequencyMinutes: int32(scanFrequency),
|
||||||
|
AutoScanEnabled: autoScanEnabled,
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user