Remove public registration, support administrator-managed accounts, and persist personal preferences. Refs #2 #5 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/httputil"
|
|
"github.com/tradarr/backend/internal/models"
|
|
)
|
|
|
|
func preferencesError(c *gin.Context, err error) {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
httputil.NotFound(c)
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "preferences unavailable"})
|
|
}
|
|
|
|
func (h *Handler) GetMyPreferences(c *gin.Context) {
|
|
if h.repo == nil {
|
|
preferencesError(c, errors.New("repository unavailable"))
|
|
return
|
|
}
|
|
p, err := h.repo.GetUserPreferences(c.GetString("userID"))
|
|
if err != nil {
|
|
preferencesError(c, err)
|
|
return
|
|
}
|
|
httputil.OK(c, p)
|
|
}
|
|
|
|
func (h *Handler) UpdateMyPreferences(c *gin.Context) {
|
|
var req models.UserPreferences
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid preferences"})
|
|
return
|
|
}
|
|
p, err := h.repo.UpdateUserPreferences(c.GetString("userID"), req)
|
|
if err != nil {
|
|
preferencesError(c, err)
|
|
return
|
|
}
|
|
httputil.OK(c, p)
|
|
}
|