feat: 🏗️ 在 sv/dao 层引入 ctx 支持链路追踪与超时控制(预留扩展) - 架构改造:在 sv 与 dao 层统一引入 ctx 🧩 - 为后续链路追踪、超时控制等能力提供支持 ⏱️(未来开发) perf: 🚀 为获取任务类列表接口引入 Redis 缓存并保证一致性 - 获取任务类列表接口新增 Redis 缓存提升访问速度 ⚡ - 通过新增任务类时主动删除缓存,确保主从一致性 ✅ - 防止缓存与数据库列表不一致问题 🛡️
60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"net/http"
|
||
"time"
|
||
|
||
"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")
|
||
// 创建一个带 1 秒超时的上下文
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
||
defer cancel() // 记得释放资源
|
||
err = api.svc.AddTaskClass(ctx, &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")
|
||
// 创建一个带 1 秒超时的上下文
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
||
defer cancel() // 记得释放资源
|
||
resp, err := api.svc.GetUserTaskClassInfos(ctx, userIDInterface)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(err))
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|