From e5a41142020cc1f8189d959dbf576b6ad6b81db2 Mon Sep 17 00:00:00 2001 From: LoveLosita <2810873701@qq.com> Date: Wed, 11 Feb 2026 18:44:13 +0800 Subject: [PATCH] =?UTF-8?q?Version:=200.2.5.dev.260211=20feat:=20?= =?UTF-8?q?=F0=9F=97=91=EF=B8=8F=20=E6=96=B0=E5=A2=9E=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=B1=BB=E6=8E=A5=E5=8F=A3=E5=B9=B6=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E7=BA=A7=E8=81=94=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 通过 task_class 与 task_item 两张表建立级联关系 🔗 - 删除 task_class 时自动删除关联的 task_item - 保证数据一致性,避免产生孤立数据 ✅ --- backend/api/task-class.go | 20 ++++++++++++++++++++ backend/dao/task-class.go | 13 +++++++++++++ backend/respond/respond.go | 10 ++++++++++ backend/routers/routers.go | 1 + backend/service/task-class.go | 26 ++++++++++++++++++++++++++ 5 files changed, 70 insertions(+) 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 +}