复用 admin.explore.write、POST/PUT+审计、C端 GET /rhythm/configs、 H5 无 active 空态/失败回退;migration 000053。ExploreConfig Loop STOP,禁自动 ECR-045。 Co-authored-by: Cursor <cursoragent@cursor.com>
112 lines
3.6 KiB
Go
112 lines
3.6 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/admin"
|
|
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
|
)
|
|
|
|
func (h *AdminHandler) registerRhythmConfigs(authed *gin.RouterGroup) {
|
|
g := authed.Group("/explore")
|
|
g.GET("/rhythm-configs", middleware.RequireAdminPermission(h.Svc, admin.PermExploreRead), h.ListRhythmConfigs)
|
|
g.GET("/rhythm-configs/:id", middleware.RequireAdminPermission(h.Svc, admin.PermExploreRead), h.GetRhythmConfig)
|
|
g.POST("/rhythm-configs", middleware.RequireAdminPermission(h.Svc, admin.PermExploreWrite), h.CreateRhythmConfig)
|
|
g.PUT("/rhythm-configs/:id", middleware.RequireAdminPermission(h.Svc, admin.PermExploreWrite), h.UpdateRhythmConfig)
|
|
}
|
|
|
|
func (h *AdminHandler) ListRhythmConfigs(c *gin.Context) {
|
|
items, err := h.Svc.ListRhythmConfigs(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50050, "list rhythm-config failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *AdminHandler) GetRhythmConfig(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
|
|
return
|
|
}
|
|
row, err := h.Svc.GetRhythmConfig(c.Request.Context(), id)
|
|
if errors.Is(err, admin.ErrRhythmConfigNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40420, "rhythm-config not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50051, "get rhythm-config failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|
|
|
|
func (h *AdminHandler) CreateRhythmConfig(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40101, "admin auth required")
|
|
return
|
|
}
|
|
var body admin.RhythmConfigWriteBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40054, "invalid body")
|
|
return
|
|
}
|
|
row, err := h.Svc.CreateRhythmConfig(c.Request.Context(), adminID, body)
|
|
if errors.Is(err, admin.ErrInvalidRhythmConfig) {
|
|
response.Fail(c, http.StatusBadRequest, 40055, "invalid rhythm config")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrRhythmConfigConflict) {
|
|
response.Fail(c, http.StatusConflict, 40912, "rhythm config code conflict")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50052, "create rhythm config failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|
|
|
|
func (h *AdminHandler) UpdateRhythmConfig(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40101, "admin auth required")
|
|
return
|
|
}
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
|
|
return
|
|
}
|
|
var body admin.RhythmConfigWriteBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40054, "invalid body")
|
|
return
|
|
}
|
|
row, err := h.Svc.UpdateRhythmConfig(c.Request.Context(), adminID, id, body)
|
|
if errors.Is(err, admin.ErrRhythmConfigNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40420, "rhythm-config not found")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrInvalidRhythmConfig) {
|
|
response.Fail(c, http.StatusBadRequest, 40055, "invalid rhythm config")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrRhythmConfigConflict) {
|
|
response.Fail(c, http.StatusConflict, 40912, "rhythm config code conflict")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50053, "update rhythm config failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|