mirror of https://github.com/halo-dev/halo
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 categorypull/1827/head
parent
bdb1eeb80e
commit
7367556c12
|
@ -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;
|
||||
}
|
|
@ -7,6 +7,7 @@ import org.springframework.data.jpa.repository.Query;
|
|||
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 @@ public interface PostCategoryRepository extends BaseRepository<PostCategory, Int
|
|||
@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();
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.util.Objects;
|
|||
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.Category;
|
|||
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 class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
|
|||
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);
|
||||
}
|
||||
|
@ -332,12 +333,16 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue