落地输入合规、探索题库、报告日/时辰刷新、账号头像、OEJTS 量表,并补齐 H5 埋点与 Admin 漏斗;同步 ESS 工件、切至自建 Git、清理 GitHub Actions。 Co-authored-by: Cursor <cursoragent@cursor.com>
123 lines
3.4 KiB
Go
123 lines
3.4 KiB
Go
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/service/scale"
|
|
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
|
)
|
|
|
|
// ScaleHandler exposes 探索测试 APIs.
|
|
type ScaleHandler struct {
|
|
Svc *scale.Service
|
|
}
|
|
|
|
// Register mounts routes.
|
|
func (h *ScaleHandler) Register(rg *gin.RouterGroup) {
|
|
rg.GET("/scales", h.List)
|
|
rg.GET("/scale-bank/catalog", h.BankCatalog)
|
|
rg.GET("/scale-bank/categories/:key", h.BankCategory)
|
|
rg.GET("/scales/:slug", h.Get)
|
|
rg.GET("/scales/:slug/result", h.LatestResult)
|
|
rg.POST("/scales/:slug/result", h.Submit)
|
|
}
|
|
|
|
// BankCatalog handles GET /scale-bank/catalog.
|
|
func (h *ScaleHandler) BankCatalog(c *gin.Context) {
|
|
out, err := h.Svc.BankCatalog()
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50003, "bank catalog failed")
|
|
return
|
|
}
|
|
response.OK(c, out)
|
|
}
|
|
|
|
// BankCategory handles GET /scale-bank/categories/:key.
|
|
func (h *ScaleHandler) BankCategory(c *gin.Context) {
|
|
cat, items, err := h.Svc.BankCategory(c.Param("key"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusNotFound, 40402, "category not found")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"category": cat, "items": items})
|
|
}
|
|
|
|
// List handles GET /scales.
|
|
func (h *ScaleHandler) List(c *gin.Context) {
|
|
items, err := h.Svc.List(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50003, "list failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
// Get handles GET /scales/:slug.
|
|
func (h *ScaleHandler) Get(c *gin.Context) {
|
|
userID, _ := middleware.UserIDFromContext(c)
|
|
d, err := h.Svc.Get(c.Request.Context(), userID, c.Param("slug"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusNotFound, 40402, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, d)
|
|
}
|
|
|
|
// LatestResult handles GET /scales/:slug/result.
|
|
func (h *ScaleHandler) LatestResult(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
out, err := h.Svc.LatestResult(c.Request.Context(), userID, c.Param("slug"))
|
|
if err != nil {
|
|
if err.Error() == "scale not found" {
|
|
response.Fail(c, http.StatusNotFound, 40402, err.Error())
|
|
return
|
|
}
|
|
response.Fail(c, http.StatusNotFound, 40411, "result not found")
|
|
return
|
|
}
|
|
response.OK(c, out)
|
|
}
|
|
|
|
// Submit handles POST /scales/:slug/result.
|
|
func (h *ScaleHandler) Submit(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"`
|
|
Answers map[string]string `json:"answers" 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
|
|
}
|
|
out, err := h.Svc.Submit(c.Request.Context(), userID, c.Param("slug"), scale.SubmitInput{
|
|
ProfileID: pid, Answers: req.Answers,
|
|
})
|
|
if err != nil {
|
|
if errors.Is(err, scale.ErrMembershipRequired) {
|
|
response.Fail(c, http.StatusForbidden, 40310, "需要成长会员")
|
|
return
|
|
}
|
|
response.Fail(c, http.StatusBadRequest, 30006, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, out)
|
|
}
|