Add host-first environment contracts (Local vs CI vs Prod), deps-only compose, and the Profile → Portrait → deep-access mock payment slice with device identity and auto-migrate on API startup. Co-authored-by: Cursor <cursoragent@cursor.com>
127 lines
3.4 KiB
Go
127 lines
3.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"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/service/report"
|
|
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
|
)
|
|
|
|
// ReportHandler exposes portrait reports and commerce mock.
|
|
type ReportHandler struct {
|
|
Svc *report.Service
|
|
}
|
|
|
|
// Register mounts report/commerce routes.
|
|
func (h *ReportHandler) Register(rg *gin.RouterGroup) {
|
|
rg.POST("/reports/portrait", h.CreatePortrait)
|
|
rg.GET("/reports/:id", h.Get)
|
|
rg.POST("/orders", h.CreateOrder)
|
|
rg.POST("/orders/:id/pay-mock", h.PayMock)
|
|
}
|
|
|
|
// CreatePortrait handles POST /reports/portrait.
|
|
func (h *ReportHandler) CreatePortrait(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
ProfileID string `json:"profile_id" binding:"required"`
|
|
}
|
|
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
|
|
}
|
|
rep, err := h.Svc.CreatePortrait(c.Request.Context(), userID, pid)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30002, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// Get handles GET /reports/:id.
|
|
func (h *ReportHandler) Get(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
rid, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid id")
|
|
return
|
|
}
|
|
rep, err := h.Svc.Get(c.Request.Context(), userID, rid)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusNotFound, 40401, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// CreateOrder handles POST /orders.
|
|
func (h *ReportHandler) CreateOrder(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
Kind string `json:"kind" binding:"required"`
|
|
Plan string `json:"plan"`
|
|
ReportID *string `json:"report_id"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
var rid *uuid.UUID
|
|
if req.ReportID != nil && *req.ReportID != "" {
|
|
id, err := uuid.Parse(*req.ReportID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid report_id")
|
|
return
|
|
}
|
|
rid = &id
|
|
}
|
|
oid, err := h.Svc.CreateOrder(c.Request.Context(), userID, report.CreateOrderInput{
|
|
Kind: req.Kind, Plan: req.Plan, ReportID: rid,
|
|
})
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30003, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"order_id": oid.String()})
|
|
}
|
|
|
|
// PayMock handles POST /orders/:id/pay-mock.
|
|
func (h *ReportHandler) PayMock(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
oid, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid id")
|
|
return
|
|
}
|
|
if err := h.Svc.PayMock(c.Request.Context(), userID, oid); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30004, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"paid": true})
|
|
}
|