Files
digital-psychology/apps/api/internal/handler/growth.go
T
jackyu66gitandCursor 89756f65b4 feat(ECR-012–016): 合规、题库、时辰刷新、头像、MBTI OEJTS 与埋点
落地输入合规、探索题库、报告日/时辰刷新、账号头像、OEJTS 量表,并补齐 H5 埋点与 Admin 漏斗;同步 ESS 工件、切至自建 Git、清理 GitHub Actions。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-13 01:27:58 +08:00

148 lines
3.9 KiB
Go

package handler
import (
"net/http"
"time"
"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"
"github.com/yuxingu/digital-psychology/apps/api/internal/textsafe"
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
)
// GrowthHandler exposes growth plans and mood trail.
type GrowthHandler struct {
Plans *repository.GrowthRepo
}
// Register mounts growth routes.
func (h *GrowthHandler) Register(rg *gin.RouterGroup) {
rg.GET("/growth/plans", h.ListPlans)
rg.POST("/growth/plans", h.CreatePlan)
rg.POST("/growth/plans/:id/checkin", h.Checkin)
rg.GET("/growth/plans/:id/checkins", h.ListCheckins)
rg.GET("/moods/recent", h.MoodsRecent)
}
// ListPlans handles GET /growth/plans.
func (h *GrowthHandler) ListPlans(c *gin.Context) {
userID, ok := middleware.UserIDFromContext(c)
if !ok {
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
return
}
items, err := h.Plans.ListPlans(c.Request.Context(), userID)
if err != nil {
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
return
}
response.OK(c, gin.H{"items": items})
}
// CreatePlan handles POST /growth/plans.
func (h *GrowthHandler) CreatePlan(c *gin.Context) {
userID, ok := middleware.UserIDFromContext(c)
if !ok {
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
return
}
var req struct {
Title string `json:"title" binding:"required"`
Focus string `json:"focus"`
}
if err := c.ShouldBindJSON(&req); err != nil {
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
return
}
title, err := textsafe.Check(textsafe.Title, req.Title)
if err != nil {
_ = failTextCompliance(c, err)
return
}
focus, err := textsafe.Check(textsafe.Focus, req.Focus)
if err != nil {
_ = failTextCompliance(c, err)
return
}
p, err := h.Plans.CreatePlan(c.Request.Context(), userID, title, focus)
if err != nil {
response.Fail(c, http.StatusBadRequest, 30020, err.Error())
return
}
response.OK(c, p)
}
// Checkin handles POST /growth/plans/:id/checkin.
func (h *GrowthHandler) Checkin(c *gin.Context) {
userID, ok := middleware.UserIDFromContext(c)
if !ok {
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
return
}
pid, err := uuid.Parse(c.Param("id"))
if err != nil {
response.Fail(c, http.StatusBadRequest, 10000, "invalid id")
return
}
var req struct {
Note *string `json:"note"`
}
_ = c.ShouldBindJSON(&req)
if req.Note != nil {
n, err := textsafe.Check(textsafe.Note, *req.Note)
if err != nil {
_ = failTextCompliance(c, err)
return
}
if n == "" {
req.Note = nil
} else {
req.Note = &n
}
}
out, err := h.Plans.Checkin(c.Request.Context(), userID, pid, time.Now(), req.Note)
if err != nil {
response.Fail(c, http.StatusBadRequest, 30021, err.Error())
return
}
response.OK(c, out)
}
// ListCheckins handles GET /growth/plans/:id/checkins.
func (h *GrowthHandler) ListCheckins(c *gin.Context) {
userID, ok := middleware.UserIDFromContext(c)
if !ok {
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
return
}
pid, err := uuid.Parse(c.Param("id"))
if err != nil {
response.Fail(c, http.StatusBadRequest, 10000, "invalid id")
return
}
items, err := h.Plans.ListCheckinsRecent(c.Request.Context(), userID, pid, 14)
if err != nil {
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
return
}
response.OK(c, gin.H{"items": items})
}
// MoodsRecent handles GET /moods/recent.
func (h *GrowthHandler) MoodsRecent(c *gin.Context) {
userID, ok := middleware.UserIDFromContext(c)
if !ok {
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
return
}
items, err := h.Plans.ListMoodsRecent(c.Request.Context(), userID, 7)
if err != nil {
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
return
}
response.OK(c, gin.H{"items": items})
}