Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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