Skip to content

Commit 7f481bc

Browse files
committed
fix(分类仓库): 修复批量删除分类时的校验逻辑
之前使用过滤方式移除不允许删除的分类,现在改为在发现不允许删除的分类时直接返回错误,避免误删默认分类和包含子分类的父分类。
1 parent 8ab30a4 commit 7f481bc

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

internal/app/repository/category_repository.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,16 @@ func (cr CategoryRepository) BatchDeleteCategoryByIds(categoryIds []string) erro
8484
if err != nil {
8585
return err
8686
}
87-
j := 0
87+
// 校验分类是否可以删除
8888
for _, category := range categorys {
89-
if category.ID != known.DEFAULT_ID && !isPID(int64((category.ID))) {
90-
categorys[j] = category
91-
j++
89+
if category.ID == known.DEFAULT_ID {
90+
return errors.New("默认分类不允许删除")
91+
}
92+
if isPID(int64(category.ID)) {
93+
return errors.New("该分类下包含子分类,请先删除子分类")
9294
}
9395
}
94-
// Slice categorys to new size.
95-
categorys = categorys[:j]
96+
9697
err = common.DB.Unscoped().Delete(&categorys).Error
9798
return err
9899
}

0 commit comments

Comments
 (0)