Bläddra i källkod

修改标签删除,添加

yusm 7 månader sedan
förälder
incheckning
4b8b294333

+ 1 - 2
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/controller/ArticleController.java

@@ -104,8 +104,7 @@ public class ArticleController
 
     @GetMapping ("/deleteById")
     public Result deleteById(@RequestParam("id") Integer id) {
-        articleService.deleteById(id);
-        return Result.success();
+        return articleService.deleteById(id);
     }
 
     @GetMapping ("/dictProduction")

+ 5 - 2
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/controller/CategoryController.java

@@ -24,8 +24,7 @@ public class CategoryController
     @PostMapping
     public Result add(@RequestBody @Validated Category category)
     {
-        categoryService.add(category);
-        return Result.success("新增分类成功!");
+        return categoryService.add(category);
     }
 
     /**
@@ -59,6 +58,10 @@ public class CategoryController
     @DeleteMapping
     public Result delete(Integer id)
     {
+        Integer count= categoryService.selectArticleCountByCategoryId(id.toString());
+        if(count>0){
+            return Result.error("存在文章关联该标签,不能删除");
+        }
         categoryService.delete(id);
         return Result.success("文章删除成功!");
     }

+ 6 - 0
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/mapper/CategoryMapper.java

@@ -21,4 +21,10 @@ public interface CategoryMapper
 
     @Delete("delete from category where id=#{id}")
     void delete(Integer id);
+
+    @Select("select count(*) from article where JSON_CONTAINS(category_ids, #{id})")
+    Integer selectArticleCountByCategoryId(String id);
+
+    @Select("select count(*) from category where category_name=#{categoryName}")
+    Integer selectCountByName(String categoryName);
 }

+ 2 - 1
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/ArticleService.java

@@ -2,6 +2,7 @@ package com.my.bigevent.service;
 
 import com.my.bigevent.pojo.Article;
 import com.my.bigevent.pojo.PageBean;
+import com.my.bigevent.pojo.Result;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
@@ -24,5 +25,5 @@ public interface ArticleService
 
     List<Article> relatedList(Integer articleId);
 
-    void deleteById(Integer articleId);
+    Result deleteById(Integer articleId);
 }

+ 5 - 1
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/CategoryService.java

@@ -1,16 +1,20 @@
 package com.my.bigevent.service;
 
 import com.my.bigevent.pojo.Category;
+import com.my.bigevent.pojo.Result;
 
 import java.util.List;
 
 public interface CategoryService
 {
-    void add(Category category);
+    Result add(Category category);
 
     List<Category> list();
 
     void update(Category category);
 
     void delete(Integer id);
+
+
+    Integer selectArticleCountByCategoryId(String id);
 }

+ 3 - 1
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/impl/ArticleServiceImpl.java

@@ -9,6 +9,7 @@ import com.my.bigevent.mapper.CategoryMapper;
 import com.my.bigevent.pojo.Article;
 import com.my.bigevent.pojo.ArticleCoverImg;
 import com.my.bigevent.pojo.PageBean;
+import com.my.bigevent.pojo.Result;
 import com.my.bigevent.service.ArticleService;
 import com.my.bigevent.utils.ThreadLocalUtil;
 import org.junit.platform.commons.util.StringUtils;
@@ -226,9 +227,10 @@ public class ArticleServiceImpl implements ArticleService
     }
 
     @Override
-    public void deleteById(Integer articleId) {
+    public Result deleteById(Integer articleId) {
         articleMapper.deleteById(articleId);
         coverImgMapper.deleteByArticleId(articleId);
+        return Result.success();
     }
 
     private void handleCoverImage(MultipartFile coverImage, Integer articleId) {

+ 19 - 1
fhKeeper/formulahousekeeper/ArticleOperation/src/main/java/com/my/bigevent/service/impl/CategoryServiceImpl.java

@@ -3,6 +3,7 @@ package com.my.bigevent.service.impl;
 import com.my.bigevent.mapper.ArticleMapper;
 import com.my.bigevent.mapper.CategoryMapper;
 import com.my.bigevent.pojo.Category;
+import com.my.bigevent.pojo.Result;
 import com.my.bigevent.service.CategoryService;
 import com.my.bigevent.utils.ThreadLocalUtil;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -21,8 +22,18 @@ public class CategoryServiceImpl implements CategoryService
     @Autowired
     ArticleMapper articleMapper;
     @Override
-    public void add(Category category)
+    public Result add(Category category)
     {
+        if(null==category.getCategoryName()||category.getCategoryName().isEmpty()){
+            return Result.error("分类名称不能为空");
+        }
+        if(null==category.getCategoryAlias()||category.getCategoryAlias().isEmpty()){
+            return Result.error("分类别名不能为空");
+        }
+        Integer count=categoryMapper.selectCountByName(category.getCategoryName());
+        if(count>0){
+            return Result.error("标签名称重复");
+        }
         // 补充属性值,数据库里规定 category 的字段都不为 null
         category.setCreateTime(LocalDateTime.now());
         category.setUpdateTime(LocalDateTime.now());
@@ -32,6 +43,8 @@ public class CategoryServiceImpl implements CategoryService
         category.setCreateUser(userId);
 
         categoryMapper.add(category);
+
+        return Result.success("新增分类成功!");
     }
 
     @Override
@@ -54,4 +67,9 @@ public class CategoryServiceImpl implements CategoryService
     {
         categoryMapper.delete(id);
     }
+
+    @Override
+    public Integer selectArticleCountByCategoryId(String id) {
+        return categoryMapper.selectArticleCountByCategoryId(id);
+    }
 }