Add comment count support in PostListVO

pull/137/head
johnniang 2019-03-22 11:12:02 +08:00
parent 1721bb4e15
commit 47a0f40ce1
4 changed files with 67 additions and 34 deletions

View File

@ -18,7 +18,10 @@ import java.util.List;
@Data
public class PostListVO extends PostSimpleOutputDTO {
private Long commentCount;
private List<TagOutputDTO> tags;
private List<CategoryOutputDTO> categories;
}

View File

@ -2,12 +2,16 @@ package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.Comment;
import cc.ryanc.halo.model.enums.CommentStatus;
import cc.ryanc.halo.model.projection.CommentCountProjection;
import cc.ryanc.halo.repository.base.BaseRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import java.util.List;
/**
* Comment repository.
*
@ -25,4 +29,26 @@ public interface CommentRepository extends BaseRepository<Comment, Long> {
*/
@NonNull
Page<Comment> findAllByStatus(@Nullable CommentStatus status, @NonNull Pageable pageable);
/**
* Finds all comments by post ids.
*
* @param postIds post ids must not be null
* @return a list of comment
*/
@NonNull
List<Comment> findAllByPostIdIn(@NonNull Iterable<Integer> postIds);
/**
* Finds all comments by post id.
*
* @param postId post id must not be null
* @return a list of comment
*/
@NonNull
List<Comment> findAllByPostId(@NonNull Integer postId);
@Query("select new cc.ryanc.halo.model.projection.CommentCountProjection(count(comment.id), comment.postId) from Comment comment where comment.postId in ?1 group by comment.postId")
@NonNull
List<CommentCountProjection> countByPostIds(@NonNull Iterable<Integer> postIds);
}

View File

@ -4,10 +4,11 @@ import cc.ryanc.halo.model.dto.post.PostMinimalOutputDTO;
import cc.ryanc.halo.model.entity.Comment;
import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.model.enums.CommentStatus;
import cc.ryanc.halo.model.projection.CommentCountProjection;
import cc.ryanc.halo.model.vo.CommentVO;
import cc.ryanc.halo.repository.CommentRepository;
import cc.ryanc.halo.repository.PostRepository;
import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.base.AbstractCrudService;
import cc.ryanc.halo.utils.ServiceUtils;
import org.springframework.data.domain.*;
@ -17,10 +18,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -34,13 +32,13 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
private final CommentRepository commentRepository;
private final PostService postService;
private final PostRepository postRepository;
public CommentServiceImpl(CommentRepository commentRepository,
PostService postService) {
PostRepository postRepository) {
super(commentRepository);
this.commentRepository = commentRepository;
this.postService = postService;
this.postRepository = postRepository;
}
@Override
@ -64,6 +62,25 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
return convertBy(commentPage);
}
@Override
public List<Comment> listBy(Integer postId) {
Assert.notNull(postId, "Post id must not be null");
return commentRepository.findAllByPostId(postId);
}
@Override
public Map<Integer, Long> countByPostIds(Collection<Integer> postIds) {
if (CollectionUtils.isEmpty(postIds)) {
return Collections.emptyMap();
}
// Get all comment counts
List<CommentCountProjection> commentCountProjections = commentRepository.countByPostIds(postIds);
return ServiceUtils.convertToMap(commentCountProjections, CommentCountProjection::getPostId, CommentCountProjection::getCount);
}
/**
* Converts to comment vo page.
*
@ -93,7 +110,7 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
Set<Integer> postIds = ServiceUtils.fetchProperty(comments, Comment::getPostId);
// Get all posts
Map<Integer, Post> postMap = ServiceUtils.convertToMap(postService.listAllByIds(postIds), Post::getId);
Map<Integer, Post> postMap = ServiceUtils.convertToMap(postRepository.findAllById(postIds), Post::getId);
return comments.stream().map(comment -> {
// Convert to vo

View File

@ -62,37 +62,15 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
TagService tagService,
CategoryService categoryService,
PostTagService postTagService,
PostCategoryService postCategoryService) {
PostCategoryService postCategoryService,
CommentService commentService) {
super(postRepository);
this.postRepository = postRepository;
this.tagService = tagService;
this.categoryService = categoryService;
this.postTagService = postTagService;
this.postCategoryService = postCategoryService;
}
/**
* Save post with tags and categories
*
* @param post post
* @param tags tags
* @param categories categories
* @return saved post
*/
@Override
public Post save(Post post, List<Tag> tags, List<Category> categories) {
// TODO 保存文章以及对应标签和分类
return null;
}
/**
* Remove post and relationship
*
* @param id id
*/
@Override
public void remove(Integer id) {
// TODO 删除文章以及关联关系
this.commentService = commentService;
}
@Override
@ -144,10 +122,16 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
Set<Integer> postIds = ServiceUtils.fetchProperty(posts, Post::getId);
// Get tag list map
Map<Integer, List<Tag>> tagListMap = postTagService.listTagListMapBy(postIds);
// Get category list map
Map<Integer, List<Category>> categoryListMap = postCategoryService.listCategoryListMap(postIds);
// Get comment count
Map<Integer, Long> commentCountMap = commentService.countByPostIds(postIds);
return postPage.map(post -> {
PostListVO postListVO = new PostListVO().convertFrom(post);
@ -159,6 +143,9 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
List<CategoryOutputDTO> categoryOutputDTOS = categoryListMap.get(post.getId()).stream().map(category -> (CategoryOutputDTO) new CategoryOutputDTO().convertFrom(category)).collect(Collectors.toList());
postListVO.setCategories(categoryOutputDTOS);
// Set comment count
postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L));
return postListVO;
});
}