From 37132816df13470e80e195e56fac04128d3b88fb Mon Sep 17 00:00:00 2001 From: guqing <1484563614@qq.com> Date: Wed, 6 Apr 2022 16:11:01 +0800 Subject: [PATCH 1/2] fix: posts in the recycle are not excluded when calculating posts under category --- .../run/halo/app/service/impl/PostCategoryServiceImpl.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java index bb2cda77ca..fc27f33865 100644 --- a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java @@ -336,6 +336,11 @@ private void populatePostIds(List categoryTree) { Assert.notNull(categoryTree, "The categoryTree must not be null."); Map> categoryPostIdsMap = postCategoryRepository.findAll() .stream() + .filter(postCategory -> { + // Filter posts in the recycle + Post post = postRepository.findById(postCategory.getPostId()).orElseThrow(); + return !PostStatus.RECYCLE.equals(post.getStatus()); + }) .collect(Collectors.groupingBy(PostCategory::getCategoryId, Collectors.mapping(PostCategory::getPostId, Collectors.toSet()))); From b029148ed5bc6e590439e01248e964abcc4f90d9 Mon Sep 17 00:00:00 2001 From: guqing <1484563614@qq.com> Date: Thu, 7 Apr 2022 16:05:23 +0800 Subject: [PATCH 2/2] refactor: post status filtering under category --- .../CategoryIdPostStatusProjection.java | 33 +++++++++++++++++++ .../repository/PostCategoryRepository.java | 11 +++++++ .../service/impl/PostCategoryServiceImpl.java | 24 +++++++------- 3 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/main/java/run/halo/app/model/projection/CategoryIdPostStatusProjection.java diff --git a/src/main/java/run/halo/app/model/projection/CategoryIdPostStatusProjection.java b/src/main/java/run/halo/app/model/projection/CategoryIdPostStatusProjection.java new file mode 100644 index 0000000000..b2d3c238c5 --- /dev/null +++ b/src/main/java/run/halo/app/model/projection/CategoryIdPostStatusProjection.java @@ -0,0 +1,33 @@ +package run.halo.app.model.projection; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import run.halo.app.model.enums.PostStatus; + +/** + * Category id and post id with status projection. + * + * @author guqing + * @date 2022-04-07 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class CategoryIdPostStatusProjection { + /** + * category id. + */ + private Integer categoryId; + + /** + * post id. + */ + private Integer postId; + + + /** + * post status. + */ + private PostStatus postStatus; +} diff --git a/src/main/java/run/halo/app/repository/PostCategoryRepository.java b/src/main/java/run/halo/app/repository/PostCategoryRepository.java index 52828baa40..3b0b401f1a 100644 --- a/src/main/java/run/halo/app/repository/PostCategoryRepository.java +++ b/src/main/java/run/halo/app/repository/PostCategoryRepository.java @@ -7,6 +7,7 @@ import org.springframework.lang.NonNull; import run.halo.app.model.entity.PostCategory; import run.halo.app.model.enums.PostStatus; +import run.halo.app.model.projection.CategoryIdPostStatusProjection; import run.halo.app.repository.base.BaseRepository; @@ -121,4 +122,14 @@ Set findAllPostIdsByCategoryId( @Query("select pc from PostCategory pc where pc.categoryId in (?1)") @NonNull List findAllByCategoryIdList(List categoryIdList); + + /** + * Finds all category ids with post id and status. + * + * @return a list of category id and post status + */ + @NonNull + @Query("select new run.halo.app.model.projection.CategoryIdPostStatusProjection(pc.categoryId," + + " pc.postId, p.status) from PostCategory pc inner join Post p on p.id=pc.postId") + List findAllWithPostStatus(); } diff --git a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java index fc27f33865..ab4a499296 100644 --- a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java @@ -12,6 +12,7 @@ import java.util.Queue; import java.util.Set; import java.util.function.Consumer; +import java.util.function.Predicate; import java.util.stream.Collectors; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -29,6 +30,7 @@ import run.halo.app.model.entity.Post; import run.halo.app.model.entity.PostCategory; import run.halo.app.model.enums.PostStatus; +import run.halo.app.model.projection.CategoryIdPostStatusProjection; import run.halo.app.model.vo.CategoryVO; import run.halo.app.repository.PostCategoryRepository; import run.halo.app.repository.PostRepository; @@ -308,8 +310,7 @@ public List listCategoryWithPostCountDto(@NonNull Sort Assert.notNull(sort, "Sort info must not be null"); List categories = categoryService.listAll(sort); List categoryTreeVo = categoryService.listToTree(categories); - populatePostIds(categoryTreeVo); - + populatePostIds(categoryTreeVo, postStatus -> !PostStatus.RECYCLE.equals(postStatus)); // Convert and return return flatTreeToList(categoryTreeVo); } @@ -332,17 +333,16 @@ private List flatTreeToList(List categoryT return result; } - private void populatePostIds(List categoryTree) { + private void populatePostIds(List categoryTree, + Predicate statusFilter) { Assert.notNull(categoryTree, "The categoryTree must not be null."); - Map> categoryPostIdsMap = postCategoryRepository.findAll() - .stream() - .filter(postCategory -> { - // Filter posts in the recycle - Post post = postRepository.findById(postCategory.getPostId()).orElseThrow(); - return !PostStatus.RECYCLE.equals(post.getStatus()); - }) - .collect(Collectors.groupingBy(PostCategory::getCategoryId, - Collectors.mapping(PostCategory::getPostId, Collectors.toSet()))); + Map> categoryPostIdsMap = + postCategoryRepository.findAllWithPostStatus() + .stream() + .filter(record -> statusFilter.test(record.getPostStatus())) + .collect(Collectors.groupingBy(CategoryIdPostStatusProjection::getCategoryId, + Collectors.mapping(CategoryIdPostStatusProjection::getPostId, + Collectors.toSet()))); walkCategoryTree(categoryTree, category -> { // Set post count