Files
TradarrV2/backend/internal/api/handlers/summary_models.go
Blomios 989996fbd6 feat(ai): add personal summary models and retire Q&A
Add an admin-managed provider model catalog with per-user summary selection, migration backfill, and generation snapshots. Retire report creation and messaging while preserving owner-scoped history.

Refs #15 #16

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-21 20:45:21 +01:00

96 lines
2.5 KiB
Go

package handlers
import (
"database/sql"
"errors"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/tradarr/backend/internal/httputil"
"github.com/tradarr/backend/internal/models"
)
func catalogHTTPError(c *gin.Context, err error) {
switch {
case errors.Is(err, models.ErrCatalogConflict):
c.JSON(409, gin.H{"error": "summary_model_conflict"})
case errors.Is(err, sql.ErrNoRows):
httputil.NotFound(c)
default:
c.JSON(500, gin.H{"error": "summary model operation failed"})
}
}
func (h *Handler) ListMySummaryModels(c *gin.Context) {
rows, err := h.repo.ListSummaryModels(true)
if err != nil {
catalogHTTPError(c, err)
return
}
type option struct {
ID string `json:"id"`
Provider string `json:"provider"`
Model string `json:"model"`
Label string `json:"label"`
}
result := []option{}
for _, row := range rows {
result = append(result, option{row.ID, row.Provider, row.Model, row.Label})
}
httputil.OK(c, result)
}
func (h *Handler) ListSummaryModels(c *gin.Context) {
rows, err := h.repo.ListSummaryModels(false)
if err != nil {
catalogHTTPError(c, err)
return
}
httputil.OK(c, rows)
}
func (h *Handler) CreateSummaryModel(c *gin.Context) {
var req struct {
ProviderID string `json:"provider_id" binding:"required,uuid"`
Model string `json:"model" binding:"required,max=256"`
Enabled *bool `json:"enabled"`
}
if c.ShouldBindJSON(&req) != nil || strings.TrimSpace(req.Model) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid summary model"})
return
}
enabled := true
if req.Enabled != nil {
enabled = *req.Enabled
}
row, err := h.repo.CreateSummaryModel(req.ProviderID, req.Model, enabled)
if err != nil {
catalogHTTPError(c, err)
return
}
httputil.Created(c, row)
}
func (h *Handler) UpdateSummaryModel(c *gin.Context) {
var req struct {
Enabled *bool `json:"enabled" binding:"required"`
}
if c.ShouldBindJSON(&req) != nil || !assetUUID.MatchString(c.Param("id")) {
c.JSON(400, gin.H{"error": "invalid summary model"})
return
}
if err := h.repo.UpdateSummaryModel(c.Param("id"), *req.Enabled); err != nil {
catalogHTTPError(c, err)
return
}
httputil.OK(c, gin.H{"ok": true})
}
func (h *Handler) DeleteSummaryModel(c *gin.Context) {
if !assetUUID.MatchString(c.Param("id")) {
c.JSON(400, gin.H{"error": "invalid summary model"})
return
}
if err := h.repo.DeleteSummaryModel(c.Param("id")); err != nil {
catalogHTTPError(c, err)
return
}
httputil.NoContent(c)
}