Skip to content

Commit

Permalink
fix: posts in the recycle are not excluded when calculating posts und…
Browse files Browse the repository at this point in the history
…er category (#1822)

* fix: posts in the recycle are not excluded when calculating posts under category

* refactor: post status filtering under category
  • Loading branch information
guqing authored Apr 7, 2022
1 parent bdb1eeb commit 7367556
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions src/main/java/run/halo/app/repository/PostCategoryRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -121,4 +122,14 @@ Set<Integer> findAllPostIdsByCategoryId(
@Query("select pc from PostCategory pc where pc.categoryId in (?1)")
@NonNull
List<PostCategory> findAllByCategoryIdList(List<Integer> 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<CategoryIdPostStatusProjection> findAllWithPostStatus();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -308,8 +310,7 @@ public List<CategoryWithPostCountDTO> listCategoryWithPostCountDto(@NonNull Sort
Assert.notNull(sort, "Sort info must not be null");
List<Category> categories = categoryService.listAll(sort);
List<CategoryVO> categoryTreeVo = categoryService.listToTree(categories);
populatePostIds(categoryTreeVo);

populatePostIds(categoryTreeVo, postStatus -> !PostStatus.RECYCLE.equals(postStatus));
// Convert and return
return flatTreeToList(categoryTreeVo);
}
Expand All @@ -332,12 +333,16 @@ private List<CategoryWithPostCountDTO> flatTreeToList(List<CategoryVO> categoryT
return result;
}

private void populatePostIds(List<CategoryVO> categoryTree) {
private void populatePostIds(List<CategoryVO> categoryTree,
Predicate<PostStatus> statusFilter) {
Assert.notNull(categoryTree, "The categoryTree must not be null.");
Map<Integer, Set<Integer>> categoryPostIdsMap = postCategoryRepository.findAll()
.stream()
.collect(Collectors.groupingBy(PostCategory::getCategoryId,
Collectors.mapping(PostCategory::getPostId, Collectors.toSet())));
Map<Integer, Set<Integer>> 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
Expand Down

0 comments on commit 7367556

Please sign in to comment.