growth.write POST/PUT · migration 000057 · 无新 C 端 · Loop STOP Co-authored-by: Cursor <cursoragent@cursor.com>
112 lines
3.7 KiB
Go
112 lines
3.7 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) registerReportTemplates(authed *gin.RouterGroup) {
|
|
g := authed.Group("/growth")
|
|
g.GET("/report-templates", middleware.RequireAdminPermission(h.Svc, admin.PermGrowthRead), h.ListReportTemplates)
|
|
g.GET("/report-templates/:id", middleware.RequireAdminPermission(h.Svc, admin.PermGrowthRead), h.GetReportTemplate)
|
|
g.POST("/report-templates", middleware.RequireAdminPermission(h.Svc, admin.PermGrowthWrite), h.CreateReportTemplate)
|
|
g.PUT("/report-templates/:id", middleware.RequireAdminPermission(h.Svc, admin.PermGrowthWrite), h.UpdateReportTemplate)
|
|
}
|
|
|
|
func (h *AdminHandler) ListReportTemplates(c *gin.Context) {
|
|
items, err := h.Svc.ListReportTemplates(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50050, "list report-template failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *AdminHandler) GetReportTemplate(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.GetReportTemplate(c.Request.Context(), id)
|
|
if errors.Is(err, admin.ErrReportTemplateNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40420, "report-template not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50051, "get report-template failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|
|
|
|
func (h *AdminHandler) CreateReportTemplate(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40101, "admin auth required")
|
|
return
|
|
}
|
|
var body admin.ReportTemplateWriteBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40054, "invalid body")
|
|
return
|
|
}
|
|
row, err := h.Svc.CreateReportTemplate(c.Request.Context(), adminID, body)
|
|
if errors.Is(err, admin.ErrInvalidReportTemplate) {
|
|
response.Fail(c, http.StatusBadRequest, 40055, "invalid report template")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrReportTemplateConflict) {
|
|
response.Fail(c, http.StatusConflict, 40912, "report template code conflict")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50052, "create report template failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|
|
|
|
func (h *AdminHandler) UpdateReportTemplate(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.ReportTemplateWriteBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40054, "invalid body")
|
|
return
|
|
}
|
|
row, err := h.Svc.UpdateReportTemplate(c.Request.Context(), adminID, id, body)
|
|
if errors.Is(err, admin.ErrReportTemplateNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40420, "report-template not found")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrInvalidReportTemplate) {
|
|
response.Fail(c, http.StatusBadRequest, 40055, "invalid report template")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrReportTemplateConflict) {
|
|
response.Fail(c, http.StatusConflict, 40912, "report template code conflict")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50053, "update report template failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|