feat: 🏗️ 完成任务分类创建与列表查询接口并通过测试 - 历经复杂嵌套逻辑处理 🌀 - 实现创建任务分类接口 ✅ - 实现获取任务分类列表接口 📋 - 接口测试全部通过 🧪 perf: 🚀 下个版本将为任务分类列表接口加入 Redis 缓存以提升查询速度 ⚡
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package api
|
||
|
||
import (
|
||
"errors"
|
||
"net/http"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/model"
|
||
"github.com/LoveLosita/smartflow/backend/respond"
|
||
"github.com/LoveLosita/smartflow/backend/service"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type TaskClassHandler struct {
|
||
svc *service.TaskClassService
|
||
}
|
||
|
||
// NewTaskClassHandler:组装 Handler 的“工厂”
|
||
func NewTaskClassHandler(svc *service.TaskClassService) *TaskClassHandler {
|
||
return &TaskClassHandler{
|
||
svc: svc, // 把传进来的 Service 揣进口袋里
|
||
}
|
||
}
|
||
|
||
func (api *TaskClassHandler) UserAddTaskClass(c *gin.Context) {
|
||
var req model.UserAddTaskClassRequest
|
||
err := c.ShouldBindJSON(&req)
|
||
if err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
userIDInterface := c.GetInt("user_id")
|
||
err = api.svc.AddTaskClass(&req, userIDInterface)
|
||
if err != nil {
|
||
if errors.Is(err, respond.WrongParamType) {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||
}
|
||
c.JSON(http.StatusOK, respond.Ok)
|
||
}
|
||
|
||
func (api *TaskClassHandler) UserGetTaskClassInfos(c *gin.Context) {
|
||
userIDInterface := c.GetInt("user_id")
|
||
resp, err := api.svc.GetUserTaskClassInfos(userIDInterface)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|