Record generation outcomes and summaries for history while keeping article content transient. Refs #7 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/httputil"
|
|
)
|
|
|
|
func (h *Handler) ListSummaries(c *gin.Context) {
|
|
userID := c.GetString("userID")
|
|
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
|
|
summaries, err := h.repo.ListSummaries(userID, limit)
|
|
if err != nil {
|
|
httputil.InternalError(c, err)
|
|
return
|
|
}
|
|
httputil.OK(c, summaries)
|
|
}
|
|
|
|
func (h *Handler) GenerateSummary(c *gin.Context) {
|
|
if h.generationUseCase == nil {
|
|
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "generation unavailable"})
|
|
return
|
|
}
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Hour)
|
|
defer cancel()
|
|
result, err := h.generationUseCase.Generate(ctx, c.GetString("userID"))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "generation unavailable"})
|
|
return
|
|
}
|
|
switch result.Outcome.Status {
|
|
case "succeeded", "partial":
|
|
c.JSON(http.StatusCreated, gin.H{"data": result})
|
|
case "no_results":
|
|
httputil.OK(c, result)
|
|
case "failed":
|
|
c.JSON(http.StatusBadGateway, gin.H{"data": result})
|
|
default:
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "invalid generation outcome"})
|
|
}
|
|
}
|
|
func (h *Handler) ListGenerations(c *gin.Context) {
|
|
limit, err := strconv.Atoi(c.DefaultQuery("limit", "10"))
|
|
if err != nil || limit < 1 || limit > 50 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "limit must be between 1 and 50"})
|
|
return
|
|
}
|
|
rows, err := h.repo.ListGenerations(c.Request.Context(), c.GetString("userID"), limit)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "history unavailable"})
|
|
return
|
|
}
|
|
httputil.OK(c, rows)
|
|
}
|