diff --git a/src/main/java/cc/ryanc/halo/repository/PostCategoryRepository.java b/src/main/java/cc/ryanc/halo/repository/PostCategoryRepository.java index 3c8ef925a..70c1c6ef7 100644 --- a/src/main/java/cc/ryanc/halo/repository/PostCategoryRepository.java +++ b/src/main/java/cc/ryanc/halo/repository/PostCategoryRepository.java @@ -5,6 +5,7 @@ import cc.ryanc.halo.repository.base.BaseRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.lang.NonNull; +import java.util.Collection; import java.util.List; import java.util.Set; @@ -44,4 +45,13 @@ public interface PostCategoryRepository extends BaseRepository findAllByPostIdIn(@NonNull Iterable postIds); + + /** + * Finds all post categories by post id. + * + * @param postId post id must not be null + * @return a list of post categories + */ + @NonNull + List findAllByPostId(@NonNull Integer postId); } diff --git a/src/main/java/cc/ryanc/halo/service/impl/PostCategoryServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/PostCategoryServiceImpl.java index de4ba3519..ed03ab8cc 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/PostCategoryServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostCategoryServiceImpl.java @@ -96,12 +96,15 @@ public class PostCategoryServiceImpl extends AbstractCrudService postCategories = categoryIds.stream().map(categoryId -> { + Set postCategories = categoryIds.stream().map(categoryId -> { PostCategory postCategory = new PostCategory(); postCategory.setPostId(postId); postCategory.setCategoryId(categoryId); return postCategory; - }).collect(Collectors.toList()); + }).collect(Collectors.toSet()); + + // List all post categories and remove them + postCategories.removeAll(postCategoryRepository.findAllByPostId(postId)); // Create them return createInBatch(postCategories); diff --git a/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java index eb42852c8..8d6f8590e 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java @@ -120,6 +120,9 @@ public class PostTagServiceImpl extends AbstractCrudService im return postTag; }).collect(Collectors.toSet()); + // Get post tag exist and remove them + postTags.removeAll(postTagRepository.findAllByPostId(postId)); + // Create in batch return createInBatch(postTags); } diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java index cb25de366..c5f8309af 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/PostController.java @@ -31,24 +31,8 @@ public class PostController { private final PostService postService; - private final TagService tagService; - - private final CategoryService categoryService; - - private final PostTagService postTagService; - - private final PostCategoryService postCategoryService; - - public PostController(PostService postService, - TagService tagService, - CategoryService categoryService, - PostTagService postTagService, - PostCategoryService postCategoryService) { + public PostController(PostService postService) { this.postService = postService; - this.tagService = tagService; - this.categoryService = categoryService; - this.postTagService = postTagService; - this.postCategoryService = postCategoryService; } @GetMapping("latest")