package handler import ( "errors" "net/http" "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/yuxingu/digital-psychology/apps/api/internal/middleware" "github.com/yuxingu/digital-psychology/apps/api/internal/repository" imagecardsvc "github.com/yuxingu/digital-psychology/apps/api/internal/service/imagecard" "github.com/yuxingu/digital-psychology/apps/api/pkg/response" ) // ImageCardHandler exposes 意象卡片 APIs. type ImageCardHandler struct { Svc *imagecardsvc.Service } // Register mounts image-card routes. func (h *ImageCardHandler) Register(rg *gin.RouterGroup) { rg.GET("/image-cards/scenes", h.Scenes) rg.GET("/image-cards/quota", h.Quota) rg.POST("/image-cards/draw", h.Draw) } // Scenes handles GET /image-cards/scenes. func (h *ImageCardHandler) Scenes(c *gin.Context) { response.OK(c, gin.H{"items": h.Svc.Scenes()}) } // Quota handles GET /image-cards/quota. func (h *ImageCardHandler) Quota(c *gin.Context) { userID, ok := middleware.UserIDFromContext(c) if !ok { response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized") return } q, err := h.Svc.Quota(c.Request.Context(), userID) if err != nil { response.Fail(c, http.StatusInternalServerError, 50000, err.Error()) return } response.OK(c, q) } // Draw handles POST /image-cards/draw. func (h *ImageCardHandler) Draw(c *gin.Context) { userID, ok := middleware.UserIDFromContext(c) if !ok { response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized") return } var req struct { Scene string `json:"scene"` ProfileID string `json:"profile_id" binding:"required"` Depth bool `json:"depth"` } if err := c.ShouldBindJSON(&req); err != nil { response.Fail(c, http.StatusBadRequest, 10000, "invalid request") return } pid, err := uuid.Parse(req.ProfileID) if err != nil { response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id") return } out, err := h.Svc.Draw(c.Request.Context(), userID, imagecardsvc.DrawInput{ Scene: req.Scene, ProfileID: pid, Depth: req.Depth, }) if err != nil { if errors.Is(err, repository.ErrQuotaExhausted) { response.Fail(c, http.StatusPaymentRequired, 40201, err.Error()) return } response.Fail(c, http.StatusBadRequest, 30011, err.Error()) return } response.OK(c, out) }