后端: 1. 阶段 4 active-scheduler 服务边界落地,新增 `cmd/active-scheduler`、`services/active_scheduler`、`shared/contracts/activescheduler` 和 active-scheduler port,迁移 dry-run、trigger、preview、confirm zrpc 能力 2. active-scheduler outbox consumer、relay、retry loop 和 due job scanner 迁入独立服务入口,gateway `/active-schedule/*` 改为通过 zrpc client 调用 3. gateway 目录收口为 `gateway/api` + `gateway/client`,统一归档 userauth、notification、active-scheduler 的 HTTP 门面和 zrpc client 4. 将旧 `backend/active_scheduler` 领域核心下沉到 `services/active_scheduler/core`,清退旧根目录活跃实现,并补充 active-scheduler 启动期跨域依赖表检查 5. 调整单体启动与 outbox 归属,`cmd/all` 不再启动 active-scheduler workflow、scanner 或 handler 文档: 1. 更新微服务迁移计划,将阶段 4 active-scheduler 标记为首轮收口完成,并明确下一阶段进入 schedule / task / course / task-class
193 lines
5.6 KiB
Go
193 lines
5.6 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"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 揣进口袋里
|
|
}
|
|
}
|
|
|
|
const (
|
|
create = 0
|
|
update = 1
|
|
)
|
|
|
|
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.AddOrUpdateTaskClass(ctx, &req, userIDInterface, create, 0)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
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 {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
|
}
|
|
|
|
func (api *TaskClassHandler) UserGetCompleteTaskClass(c *gin.Context) {
|
|
taskClassID := c.Query("task_class_id")
|
|
//将taskClassID转换为int
|
|
intTaskClassID, err := strconv.Atoi(taskClassID)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
if taskClassID == "" {
|
|
c.JSON(http.StatusBadRequest, respond.MissingParam)
|
|
return
|
|
}
|
|
userIDInterface := c.GetInt("user_id")
|
|
// 创建一个带 1 秒超时的上下文
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
|
defer cancel() // 记得释放资源
|
|
resp, err := api.svc.GetUserCompleteTaskClass(ctx, userIDInterface, intTaskClassID)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
|
}
|
|
|
|
func (api *TaskClassHandler) UserUpdateTaskClass(c *gin.Context) {
|
|
var req model.UserAddTaskClassRequest
|
|
err := c.ShouldBindJSON(&req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
taskClassID := c.Query("task_class_id")
|
|
//将taskClassID转换为int
|
|
intTaskClassID, err := strconv.Atoi(taskClassID)
|
|
userIDInterface := c.GetInt("user_id")
|
|
// 创建一个带 1 秒超时的上下文
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
|
defer cancel() // 记得释放资源
|
|
err = api.svc.AddOrUpdateTaskClass(ctx, &req, userIDInterface, update, intTaskClassID)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.Ok)
|
|
}
|
|
|
|
func (api *TaskClassHandler) UserAddTaskClassItemIntoSchedule(c *gin.Context) {
|
|
var req model.UserInsertTaskClassItemToScheduleRequest
|
|
err := c.ShouldBindJSON(&req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
taskID := c.Query("task_item_id")
|
|
//将taskID转换为int
|
|
intTaskID, err := strconv.Atoi(taskID)
|
|
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.AddTaskClassItemIntoSchedule(ctx, &req, userIDInterface, intTaskID)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.Ok)
|
|
}
|
|
|
|
func (api *TaskClassHandler) DeleteTaskClassItem(c *gin.Context) {
|
|
taskID := c.Query("task_item_id")
|
|
//将taskID转换为int
|
|
intTaskID, err := strconv.Atoi(taskID)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
userID := c.GetInt("user_id")
|
|
// 创建一个带 1 秒超时的上下文
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
|
defer cancel() // 记得释放资源
|
|
err = api.svc.DeleteTaskClassItem(ctx, userID, intTaskID)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.Ok)
|
|
}
|
|
|
|
func (api *TaskClassHandler) DeleteTaskClass(c *gin.Context) {
|
|
taskClassID := c.Query("task_class_id")
|
|
//将taskClassID转换为int
|
|
intTaskClassID, err := strconv.Atoi(taskClassID)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
userID := c.GetInt("user_id")
|
|
// 创建一个带 1 秒超时的上下文
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
|
defer cancel() // 记得释放资源
|
|
err = api.svc.DeleteTaskClass(ctx, userID, intTaskClassID)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.Ok)
|
|
}
|
|
|
|
func (api *TaskClassHandler) UserInsertBatchTaskClassItemsIntoSchedule(c *gin.Context) {
|
|
var req model.UserInsertTaskClassItemToScheduleRequestBatch
|
|
err := c.ShouldBindJSON(&req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
userID := c.GetInt("user_id")
|
|
// 创建一个带 1 秒超时的上下文
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 1*time.Second)
|
|
defer cancel() // 记得释放资源
|
|
err = api.svc.BatchApplyPlans(ctx, req.TaskClassID, userID, &req)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.Ok)
|
|
}
|