membership_plans 表、admin 套餐页、Grant/CreateOrder 读表; Loop continuous 自动 Approve/Closed。Next:ECR-015 RedemptionCode。 Co-authored-by: Cursor <cursoragent@cursor.com>
79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"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) registerMembershipPlans(authed *gin.RouterGroup) {
|
|
authed.GET("/membership-plans", middleware.RequireAdminPermission(h.Svc, admin.PermMembershipPlansRead), h.ListMembershipPlans)
|
|
authed.GET("/membership-plans/:code", middleware.RequireAdminPermission(h.Svc, admin.PermMembershipPlansRead), h.GetMembershipPlan)
|
|
authed.PUT("/membership-plans/:code", middleware.RequireAdminPermission(h.Svc, admin.PermMembershipPlansWrite), h.PutMembershipPlan)
|
|
}
|
|
|
|
func (h *AdminHandler) ListMembershipPlans(c *gin.Context) {
|
|
items, err := h.Svc.ListMembershipPlans(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *AdminHandler) GetMembershipPlan(c *gin.Context) {
|
|
plan, err := h.Svc.GetMembershipPlan(c.Request.Context(), c.Param("code"))
|
|
if errors.Is(err, admin.ErrPlanNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40400, "plan not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, plan)
|
|
}
|
|
|
|
func (h *AdminHandler) PutMembershipPlan(c *gin.Context) {
|
|
adminID, ok := middleware.AdminIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40102, "admin session invalid")
|
|
return
|
|
}
|
|
var body struct {
|
|
Title string `json:"title"`
|
|
DurationDays int `json:"duration_days"`
|
|
AmountCents int `json:"amount_cents"`
|
|
Active *bool `json:"active"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40000, "invalid body")
|
|
return
|
|
}
|
|
active := true
|
|
if body.Active != nil {
|
|
active = *body.Active
|
|
}
|
|
plan, err := h.Svc.UpdateMembershipPlan(
|
|
c.Request.Context(), adminID, c.Param("code"), body.Title, body.DurationDays, body.AmountCents, active,
|
|
)
|
|
if errors.Is(err, admin.ErrPlanNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40400, "plan not found")
|
|
return
|
|
}
|
|
if errors.Is(err, admin.ErrInvalidPlanU) {
|
|
response.Fail(c, http.StatusBadRequest, 40000, err.Error())
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, plan)
|
|
}
|