保留远程用户侧 ECR-009–016 与本地 Ops 目录/RBAC/CMS/危机等能力;文档标注分叉期间 ECR 编号冲突。 Co-authored-by: Cursor <cursoragent@cursor.com>
105 lines
3.4 KiB
Go
105 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/admin"
|
|
homesvc "github.com/yuxingu/digital-psychology/apps/api/internal/service/home"
|
|
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
|
)
|
|
|
|
func (h *AdminHandler) registerContent(authed *gin.RouterGroup) {
|
|
authed.GET("/home/tools", middleware.RequireAdminPermission(h.Svc, admin.PermContentWrite), h.ListHomeTools)
|
|
authed.PUT("/home/tools", middleware.RequireAdminPermission(h.Svc, admin.PermContentWrite), h.ReplaceHomeTools)
|
|
authed.GET("/scales", middleware.RequireAdminPermission(h.Svc, admin.PermContentWrite), h.ListScales)
|
|
authed.PATCH("/scales/:id", middleware.RequireAdminPermission(h.Svc, admin.PermContentWrite), h.PatchScale)
|
|
}
|
|
|
|
func (h *AdminHandler) ListHomeTools(c *gin.Context) {
|
|
items, err := h.Svc.ListHomeTools(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50031, "list home tools failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *AdminHandler) ReplaceHomeTools(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40101, "admin auth required")
|
|
return
|
|
}
|
|
var body struct {
|
|
Items []homesvc.ReplaceInput `json:"items"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40040, "invalid body")
|
|
return
|
|
}
|
|
if err := h.Svc.ReplaceHomeTools(c.Request.Context(), adminID, body.Items); err != nil {
|
|
if errors.Is(err, admin.ErrForbidden) {
|
|
response.Fail(c, http.StatusForbidden, 40301, "forbidden")
|
|
return
|
|
}
|
|
if errors.Is(err, homesvc.ErrInvalidTools) || errors.Is(err, homesvc.ErrTooManyTools) {
|
|
response.Fail(c, http.StatusBadRequest, 40041, err.Error())
|
|
return
|
|
}
|
|
response.Fail(c, http.StatusInternalServerError, 50032, "replace home tools failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"ok": true})
|
|
}
|
|
|
|
func (h *AdminHandler) ListScales(c *gin.Context) {
|
|
items, err := h.Svc.ListScalesAdmin(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50033, "list scales failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *AdminHandler) PatchScale(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, 40042, "invalid scale id")
|
|
return
|
|
}
|
|
var body struct {
|
|
Status string `json:"status"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil || body.Status == "" {
|
|
response.Fail(c, http.StatusBadRequest, 40043, "status required")
|
|
return
|
|
}
|
|
if err := h.Svc.PatchScaleStatus(c.Request.Context(), adminID, id, body.Status); err != nil {
|
|
if errors.Is(err, admin.ErrForbidden) {
|
|
response.Fail(c, http.StatusForbidden, 40301, "forbidden")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrInvalidScaleStatus) {
|
|
response.Fail(c, http.StatusBadRequest, 40044, "invalid status")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrScaleNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40410, "scale not found")
|
|
return
|
|
}
|
|
response.Fail(c, http.StatusInternalServerError, 50034, "patch scale failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"ok": true})
|
|
}
|