// 分类接口 package app import ( "gorm.io/gorm" "simple-memo/global" "simple-memo/models" "simple-memo/utils" "github.com/gin-gonic/gin" ) // -------------------------- 1. 定义 Handler 接口 -------------------------- // CategoryHandler 分类模块接口定义 type CategoryHandler interface { CategoryList(c *gin.Context) // 获取分类列表 AddCategory(c *gin.Context) // 添加分类 EditCategory(c *gin.Context) // 编辑分类 DeleteCategory(c *gin.Context) // 删除分类 } // -------------------------- 2. 实现结构体 -------------------------- type categoryHandler struct { db *gorm.DB } func NewCategoryHandler() CategoryHandler { return &categoryHandler{ db: global.DB, } } // -------------------------- 3. 请求结构体 -------------------------- // CategoryListReq 获取分类列表 type CategoryListReq struct { Type int `form:"type"` // 1-支出 2-收入 } // AddCategoryReq 添加分类 type AddCategoryReq struct { Name string `json:"name"` // 分类名称 Type int `json:"type"` // 1-支出 2-收入 Icon string `json:"icon"` // 图标 Color string `json:"color"` // 颜色 } // EditCategoryReq 编辑分类 type EditCategoryReq struct { ID uint `json:"id"` Name string `json:"name"` Type int `json:"type"` Icon string `json:"icon"` Color string `json:"color"` } // DeleteCategoryReq 删除分类 type DeleteCategoryReq struct { ID uint `json:"id"` } // -------------------------- 4. 接口实现 -------------------------- // CategoryList 获取分类列表(系统分类 + 当前用户自定义) func (h *categoryHandler) CategoryList(c *gin.Context) { userID := c.GetUint("userID") var req CategoryListReq if err := c.ShouldBindQuery(&req); err != nil { utils.Fail(c, "参数解析失败") return } var list []models.Category err := h.db.Where("user_id = 0 OR user_id = ?", userID). Where("type = ?", req.Type). Order("id ASC"). Find(&list).Error if err != nil { utils.Fail(c, "获取失败") return } utils.Ok(c, list) } // AddCategory 添加自定义分类 func (h *categoryHandler) AddCategory(c *gin.Context) { userID := c.GetUint("userID") var req AddCategoryReq if err := c.ShouldBindJSON(&req); err != nil { utils.Fail(c, "参数解析失败") return } category := models.Category{ UserID: userID, Name: req.Name, Type: req.Type, Icon: req.Icon, Color: req.Color, } if err := h.db.Create(&category).Error; err != nil { utils.Fail(c, "添加失败") return } utils.Ok(c, nil) } // EditCategory 编辑分类 func (h *categoryHandler) EditCategory(c *gin.Context) { userID := c.GetUint("userID") var req EditCategoryReq if err := c.ShouldBindJSON(&req); err != nil { utils.Fail(c, "参数解析失败") return } data := map[string]any{ "name": req.Name, "type": req.Type, "icon": req.Icon, "color": req.Color, } err := h.db.Model(&models.Category{}). Where("id = ? AND user_id = ?", req.ID, userID). Updates(data).Error if err != nil { utils.Fail(c, "编辑失败") return } utils.Ok(c, nil) } // DeleteCategory 删除分类 func (h *categoryHandler) DeleteCategory(c *gin.Context) { userID := c.GetUint("userID") var req DeleteCategoryReq if err := c.ShouldBindJSON(&req); err != nil { utils.Fail(c, "参数解析失败") return } err := h.db.Where("id = ? AND user_id = ?", req.ID, userID). Delete(&models.Category{}).Error if err != nil { utils.Fail(c, "删除失败") return } utils.Ok(c, nil) }