fd097729d7
- 成就系统、连续打卡天数计算、管理后台成就 CRUD - 梦想目标图标预设 DreamPreset 与用户端 dream-presets 接口 - 管理后台梦想图标 CRUD;戒烟打卡 summary 修正 - 忽略根目录编译产物 /api Made-with: Cursor
84 lines
2.6 KiB
Go
84 lines
2.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"wx_service/internal/model"
|
|
)
|
|
|
|
type dreamPresetRequest struct {
|
|
Title string `json:"title"`
|
|
CoverImage string `json:"cover_image"`
|
|
SortOrder int `json:"sort_order"`
|
|
IsActive *bool `json:"is_active"`
|
|
}
|
|
|
|
func (h *Handler) ListDreamPresets(c *gin.Context) {
|
|
presets, err := h.svc.ListDreamPresets(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取预设目标失败"))
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.Success(gin.H{"items": presets}))
|
|
}
|
|
|
|
func (h *Handler) CreateDreamPreset(c *gin.Context) {
|
|
var req dreamPresetRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "参数错误"))
|
|
return
|
|
}
|
|
if strings.TrimSpace(req.CoverImage) == "" {
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "图标不能为空"))
|
|
return
|
|
}
|
|
preset, err := h.svc.CreateDreamPreset(c.Request.Context(), req.Title, req.CoverImage, req.SortOrder, req.IsActive)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "创建失败"))
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.Success(preset))
|
|
}
|
|
|
|
type dreamPresetUpdateRequest struct {
|
|
Title *string `json:"title"`
|
|
CoverImage *string `json:"cover_image"`
|
|
SortOrder *int `json:"sort_order"`
|
|
IsActive *bool `json:"is_active"`
|
|
}
|
|
|
|
func (h *Handler) UpdateDreamPreset(c *gin.Context) {
|
|
id, err := parseUintID(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "id 参数错误"))
|
|
return
|
|
}
|
|
var req dreamPresetUpdateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "参数错误"))
|
|
return
|
|
}
|
|
preset, err := h.svc.UpdateDreamPreset(c.Request.Context(), id, req.Title, req.CoverImage, req.SortOrder, req.IsActive)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "更新失败"))
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.Success(preset))
|
|
}
|
|
|
|
func (h *Handler) DeleteDreamPreset(c *gin.Context) {
|
|
id, err := parseUintID(c.Param("id"))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "id 参数错误"))
|
|
return
|
|
}
|
|
if err := h.svc.DeleteDreamPreset(c.Request.Context(), id); err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "删除失败"))
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.Success(gin.H{"deleted": true}))
|
|
}
|