diff --git a/backend/api/task-class.go b/backend/api/task-class.go index 56528b8..74ab7a6 100644 --- a/backend/api/task-class.go +++ b/backend/api/task-class.go @@ -151,3 +151,23 @@ func (api *TaskClassHandler) DeleteTaskClassItem(c *gin.Context) { } 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) +} diff --git a/backend/dao/task-class.go b/backend/dao/task-class.go index 0be5339..e9040e1 100644 --- a/backend/dao/task-class.go +++ b/backend/dao/task-class.go @@ -218,3 +218,16 @@ func (dao *TaskClassDAO) DeleteTaskClassItemByID(ctx context.Context, id int) er Delete(&model.TaskClassItem{}).Error return err } + +func (dao *TaskClassDAO) DeleteTaskClassByID(ctx context.Context, id int) error { + res := dao.db.WithContext(ctx). + Where("id = ?", id). + Delete(&model.TaskClass{}) + if res.Error != nil { + return res.Error + } + if res.RowsAffected == 0 { + return respond.WrongTaskClassID + } + return nil +} diff --git a/backend/respond/respond.go b/backend/respond/respond.go index 334c3f0..ff888a2 100644 --- a/backend/respond/respond.go +++ b/backend/respond/respond.go @@ -250,4 +250,14 @@ var ( //请求相关的响应 Status: "40038", Info: "request is processing, please do not repeat click", } + + TaskClassNotBelongToUser = Response{ //任务类不属于用户 + Status: "40039", + Info: "task class does not belong to user", + } + + WrongTaskClassID = Response{ //任务类ID错误 + Status: "40040", + Info: "wrong task class id", + } ) diff --git a/backend/routers/routers.go b/backend/routers/routers.go index 0eed8c1..65eda15 100644 --- a/backend/routers/routers.go +++ b/backend/routers/routers.go @@ -70,6 +70,7 @@ func RegisterRouters(handlers *api.ApiHandlers, cache *dao.CacheDAO, limiter *pk taskClassGroup.PUT("/update", middleware.IdempotencyMiddleware(cache), handlers.TaskClassHandler.UserUpdateTaskClass) taskClassGroup.POST("/insert-into-schedule", middleware.IdempotencyMiddleware(cache), handlers.TaskClassHandler.UserAddTaskClassItemIntoSchedule) taskClassGroup.DELETE("/delete-item", middleware.IdempotencyMiddleware(cache), handlers.TaskClassHandler.DeleteTaskClassItem) + taskClassGroup.DELETE("/delete-class", middleware.IdempotencyMiddleware(cache), handlers.TaskClassHandler.DeleteTaskClass) } scheduleGroup := apiGroup.Group("/schedule") { diff --git a/backend/service/task-class.go b/backend/service/task-class.go index 1839e24..d26b67b 100644 --- a/backend/service/task-class.go +++ b/backend/service/task-class.go @@ -10,6 +10,7 @@ import ( "github.com/LoveLosita/smartflow/backend/model" "github.com/LoveLosita/smartflow/backend/respond" "github.com/go-redis/redis/v8" + "gorm.io/gorm" ) type TaskClassService struct { @@ -301,3 +302,28 @@ func (sv *TaskClassService) DeleteTaskClassItem(ctx context.Context, userID int, } return nil } + +func (sv *TaskClassService) DeleteTaskClass(ctx context.Context, userID int, taskClassID int) error { + //1.先验证任务类归属 + ownerID, err := sv.taskClassRepo.GetTaskClassUserIDByID(ctx, taskClassID) //通过任务类ID获取所属用户ID + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return respond.WrongTaskClassID + } + return err + } + if ownerID != userID { + return respond.TaskClassNotBelongToUser + } + //2.删除任务类(事务) + err = sv.taskClassRepo.DeleteTaskClassByID(ctx, taskClassID) + if err != nil { + return err + } + //3.事务提交成功后,清除相关缓存(如果有的话),以保证数据一致性 + err = sv.cacheRepo.DeleteTaskClassList(ctx, userID) + if err != nil { + log.Printf("Failed to delete task class list cache for userID %d: %v", userID, err) + } + return nil +}