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

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

* refactor: post status filtering under category
pull/1827/head
guqing 2022-04-07 19:33:02 +08:00 committed by GitHub
parent bdb1eeb80e
commit 7367556c12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 7 deletions

View File

@ -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;
}

View File

@ -7,6 +7,7 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import run.halo.app.model.entity.PostCategory; import run.halo.app.model.entity.PostCategory;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.projection.CategoryIdPostStatusProjection;
import run.halo.app.repository.base.BaseRepository; import run.halo.app.repository.base.BaseRepository;
@ -121,4 +122,14 @@ public interface PostCategoryRepository extends BaseRepository<PostCategory, Int
@Query("select pc from PostCategory pc where pc.categoryId in (?1)") @Query("select pc from PostCategory pc where pc.categoryId in (?1)")
@NonNull @NonNull
List<PostCategory> findAllByCategoryIdList(List<Integer> categoryIdList); 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();
} }

View File

@ -12,6 +12,7 @@ import java.util.Objects;
import java.util.Queue; import java.util.Queue;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -29,6 +30,7 @@ import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostCategory; import run.halo.app.model.entity.PostCategory;
import run.halo.app.model.enums.PostStatus; 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.model.vo.CategoryVO;
import run.halo.app.repository.PostCategoryRepository; import run.halo.app.repository.PostCategoryRepository;
import run.halo.app.repository.PostRepository; import run.halo.app.repository.PostRepository;
@ -308,8 +310,7 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
Assert.notNull(sort, "Sort info must not be null"); Assert.notNull(sort, "Sort info must not be null");
List<Category> categories = categoryService.listAll(sort); List<Category> categories = categoryService.listAll(sort);
List<CategoryVO> categoryTreeVo = categoryService.listToTree(categories); List<CategoryVO> categoryTreeVo = categoryService.listToTree(categories);
populatePostIds(categoryTreeVo); populatePostIds(categoryTreeVo, postStatus -> !PostStatus.RECYCLE.equals(postStatus));
// Convert and return // Convert and return
return flatTreeToList(categoryTreeVo); return flatTreeToList(categoryTreeVo);
} }
@ -332,12 +333,16 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
return result; 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."); Assert.notNull(categoryTree, "The categoryTree must not be null.");
Map<Integer, Set<Integer>> categoryPostIdsMap = postCategoryRepository.findAll() Map<Integer, Set<Integer>> categoryPostIdsMap =
postCategoryRepository.findAllWithPostStatus()
.stream() .stream()
.collect(Collectors.groupingBy(PostCategory::getCategoryId, .filter(record -> statusFilter.test(record.getPostStatus()))
Collectors.mapping(PostCategory::getPostId, Collectors.toSet()))); .collect(Collectors.groupingBy(CategoryIdPostStatusProjection::getCategoryId,
Collectors.mapping(CategoryIdPostStatusProjection::getPostId,
Collectors.toSet())));
walkCategoryTree(categoryTree, category -> { walkCategoryTree(categoryTree, category -> {
// Set post count // Set post count