Files
wx_service/internal/marketing/handler/user_logo_handler.go
T
nepiedg b4170b4863 feat(marketing): add user logo management module
Users can now save uploaded logos to the backend (marketing_user_logos table),
avoiding repeated uploads. Includes CRUD endpoints: list, save, delete with
a per-user limit of 10 logos.

Made-with: Cursor
2026-04-04 03:46:57 +08:00

74 lines
2.0 KiB
Go

package handler
import (
"errors"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"wx_service/internal/marketing/repository"
"wx_service/internal/marketing/service"
"wx_service/internal/middleware"
"wx_service/internal/model"
)
type UserLogoHandler struct {
svc *service.UserLogoService
}
func NewUserLogoHandler(svc *service.UserLogoService) *UserLogoHandler {
return &UserLogoHandler{svc: svc}
}
func (h *UserLogoHandler) List(c *gin.Context) {
user := middleware.MustCurrentUser(c)
logos, err := h.svc.List(user.ID)
if err != nil {
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取 Logo 列表失败"))
return
}
c.JSON(http.StatusOK, model.Success(logos))
}
func (h *UserLogoHandler) Save(c *gin.Context) {
user := middleware.MustCurrentUser(c)
var req service.SaveLogoRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误"))
return
}
logo, err := h.svc.Save(user.ID, req)
if err != nil {
if errors.Is(err, service.ErrLogoLimitReached) {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, err.Error()))
return
}
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "保存 Logo 失败"))
return
}
c.JSON(http.StatusOK, model.Success(logo))
}
func (h *UserLogoHandler) Delete(c *gin.Context) {
user := middleware.MustCurrentUser(c)
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "无效的 ID"))
return
}
if err := h.svc.Delete(uint(id), user.ID); err != nil {
if errors.Is(err, repository.ErrUserLogoNotFound) {
c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "Logo 不存在"))
return
}
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "删除失败"))
return
}
c.JSON(http.StatusOK, model.Success(nil))
}