77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"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.generationJobs == nil {
|
|
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "generation unavailable"})
|
|
return
|
|
}
|
|
job, err := h.generationJobs.Start(c.Request.Context(), c.GetString("userID"))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "generation unavailable"})
|
|
return
|
|
}
|
|
c.Header("Location", "/api/summaries/generation-jobs/"+job.ID)
|
|
c.JSON(http.StatusAccepted, gin.H{"data": job})
|
|
}
|
|
func (h *Handler) RecentGenerationJob(c *gin.Context) {
|
|
job, err := h.repo.RecentGenerationJob(c.Request.Context(), c.GetString("userID"))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "generation unavailable"})
|
|
return
|
|
}
|
|
httputil.OK(c, job)
|
|
}
|
|
|
|
var generationJobUUID = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
|
|
|
|
func (h *Handler) GetGenerationJob(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if !generationJobUUID.MatchString(id) {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "generation not found"})
|
|
return
|
|
}
|
|
job, err := h.repo.GetGenerationJob(c.Request.Context(), c.GetString("userID"), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "generation unavailable"})
|
|
return
|
|
}
|
|
if job == nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "generation not found"})
|
|
return
|
|
}
|
|
httputil.OK(c, job)
|
|
}
|
|
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)
|
|
}
|