package handler import ( "net/http" "strconv" "github.com/gin-gonic/gin" "wx_service/internal/marketing/repository" "wx_service/internal/marketing/service" "wx_service/internal/model" ) type TemplateHandler struct { svc *service.TemplateService } func NewTemplateHandler(svc *service.TemplateService) *TemplateHandler { return &TemplateHandler{svc: svc} } // ListEnabled returns enabled templates for mini-program users. func (h *TemplateHandler) ListEnabled(c *gin.Context) { categoryID := parseUintQuery(c, "category_id", 0) page := parseIntQuery(c, "page", 1) pageSize := parseIntQuery(c, "page_size", 20) resp, err := h.svc.List(repository.TemplateListParams{ CategoryID: categoryID, OnlyEnabled: true, Page: page, PageSize: pageSize, }) if err != nil { c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误")) return } c.JSON(http.StatusOK, model.Success(resp)) } // GetDetail returns a single template by ID. func (h *TemplateHandler) GetDetail(c *gin.Context) { id, ok := parseID(c) if !ok { return } tpl, err := h.svc.GetByID(id) if err != nil { if service.IsNotFoundError(err) { c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "模板不存在")) return } c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误")) return } c.JSON(http.StatusOK, model.Success(tpl)) } // AdminList returns all templates for admin. func (h *TemplateHandler) AdminList(c *gin.Context) { categoryID := parseUintQuery(c, "category_id", 0) page := parseIntQuery(c, "page", 1) pageSize := parseIntQuery(c, "page_size", 20) resp, err := h.svc.List(repository.TemplateListParams{ CategoryID: categoryID, OnlyEnabled: false, Page: page, PageSize: pageSize, }) if err != nil { c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误")) return } c.JSON(http.StatusOK, model.Success(resp)) } func (h *TemplateHandler) AdminCreate(c *gin.Context) { var req service.TemplateRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误")) return } tpl, err := h.svc.Create(req) if err != nil { if service.IsBadRequestError(err) { c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, err.Error())) return } c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误")) return } c.JSON(http.StatusOK, model.Success(tpl)) } func (h *TemplateHandler) AdminUpdate(c *gin.Context) { id, ok := parseID(c) if !ok { return } var req service.TemplateRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误")) return } tpl, err := h.svc.Update(id, req) if err != nil { if service.IsNotFoundError(err) { c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "模板不存在")) return } if service.IsBadRequestError(err) { c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, err.Error())) return } c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误")) return } c.JSON(http.StatusOK, model.Success(tpl)) } func (h *TemplateHandler) AdminDelete(c *gin.Context) { id, ok := parseID(c) if !ok { return } if err := h.svc.Delete(id); err != nil { if service.IsNotFoundError(err) { c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "模板不存在")) return } c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误")) return } c.JSON(http.StatusOK, model.Success(nil)) } func parseIntQuery(c *gin.Context, key string, defaultValue int) int { v := c.Query(key) if v == "" { return defaultValue } parsed, err := strconv.Atoi(v) if err != nil { return defaultValue } return parsed } func parseUintQuery(c *gin.Context, key string, defaultValue uint) uint { v := c.Query(key) if v == "" { return defaultValue } parsed, err := strconv.ParseUint(v, 10, 64) if err != nil { return defaultValue } return uint(parsed) }