growth.write POST/PUT · migration 000056 · 无新 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) registerFunnelDefinitions(authed *gin.RouterGroup) {
|
|
g := authed.Group("/analytics")
|
|
g.GET("/funnel-definitions", middleware.RequireAdminPermission(h.Svc, admin.PermGrowthRead), h.ListFunnelDefinitions)
|
|
g.GET("/funnel-definitions/:id", middleware.RequireAdminPermission(h.Svc, admin.PermGrowthRead), h.GetFunnelDefinition)
|
|
g.POST("/funnel-definitions", middleware.RequireAdminPermission(h.Svc, admin.PermGrowthWrite), h.CreateFunnelDefinition)
|
|
g.PUT("/funnel-definitions/:id", middleware.RequireAdminPermission(h.Svc, admin.PermGrowthWrite), h.UpdateFunnelDefinition)
|
|
}
|
|
|
|
func (h *AdminHandler) ListFunnelDefinitions(c *gin.Context) {
|
|
items, err := h.Svc.ListFunnelDefinitions(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50050, "list funnel-definition failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *AdminHandler) GetFunnelDefinition(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.GetFunnelDefinition(c.Request.Context(), id)
|
|
if errors.Is(err, admin.ErrFunnelDefinitionNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40420, "funnel-definition not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50051, "get funnel-definition failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|
|
|
|
func (h *AdminHandler) CreateFunnelDefinition(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40101, "admin auth required")
|
|
return
|
|
}
|
|
var body admin.FunnelDefinitionWriteBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40054, "invalid body")
|
|
return
|
|
}
|
|
row, err := h.Svc.CreateFunnelDefinition(c.Request.Context(), adminID, body)
|
|
if errors.Is(err, admin.ErrInvalidFunnelDefinition) {
|
|
response.Fail(c, http.StatusBadRequest, 40055, "invalid funnel definition")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrFunnelDefinitionConflict) {
|
|
response.Fail(c, http.StatusConflict, 40912, "funnel definition code conflict")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50052, "create funnel definition failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|
|
|
|
func (h *AdminHandler) UpdateFunnelDefinition(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.FunnelDefinitionWriteBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40054, "invalid body")
|
|
return
|
|
}
|
|
row, err := h.Svc.UpdateFunnelDefinition(c.Request.Context(), adminID, id, body)
|
|
if errors.Is(err, admin.ErrFunnelDefinitionNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40420, "funnel-definition not found")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrInvalidFunnelDefinition) {
|
|
response.Fail(c, http.StatusBadRequest, 40055, "invalid funnel definition")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrFunnelDefinitionConflict) {
|
|
response.Fail(c, http.StatusConflict, 40912, "funnel definition code conflict")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50053, "update funnel definition failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|