Extract pageListVoBy to convertToListVo in PostService

pull/146/head
johnniang 2019-04-10 17:42:16 +08:00
parent 5765f1a8af
commit cad85c0bc7
4 changed files with 77 additions and 52 deletions

View File

@ -24,15 +24,15 @@ public class PostSimpleOutputDTO extends PostMinimalOutputDTO {
private String thumbnail;
private Long visits;
private Long visits = 0L;
private Boolean disallowComment;
private String template;
private Integer topPriority;
private Integer topPriority = 0;
private PostCreateFrom createFrom;
private Long likes;
private Long likes = 0L;
}

View File

@ -183,5 +183,14 @@ public interface PostService extends CrudService<Post, Integer> {
* @return a page of post simple output dto
*/
@NonNull
Page<PostSimpleOutputDTO> convertTo(@NonNull Page<Post> postPage);
Page<PostSimpleOutputDTO> convertToSimpleDto(@NonNull Page<Post> postPage);
/**
* Converts to a page of post list vo.
*
* @param postPage post page must not be null
* @return a page of post list vo
*/
@NonNull
Page<PostListVO> convertToListVo(@NonNull Page<Post> postPage);
}

View File

@ -116,6 +116,13 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
return postRepository.findAll(buildSpecByQuery(postQuery), pageable);
}
/**
* Build specification by post query.
*
* @param postQuery post query must not be null
* @return a post specification
*/
@NonNull
private Specification<Post> buildSpecByQuery(@NonNull PostQuery postQuery) {
Assert.notNull(postQuery, "Post query must not be null");
@ -167,44 +174,7 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
public Page<PostListVO> pageListVoBy(PostStatus status, Pageable pageable) {
Page<Post> postPage = pageBy(status, pageable);
List<Post> posts = postPage.getContent();
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);
Optional.ofNullable(tagListMap.get(post.getId())).orElseGet(LinkedList::new);
// Set tags
postListVO.setTags(Optional.ofNullable(tagListMap.get(post.getId()))
.orElseGet(LinkedList::new)
.stream()
.map(tag -> new TagOutputDTO().<TagOutputDTO>convertFrom(tag))
.collect(Collectors.toList()));
// Set categories
postListVO.setCategories(Optional.ofNullable(categoryListMap.get(post.getId()))
.orElseGet(LinkedList::new)
.stream()
.map(category -> new CategoryOutputDTO().<CategoryOutputDTO>convertFrom(category))
.collect(Collectors.toList()));
// Set comment count
postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L));
return postListVO;
});
return convertToListVo(postPage);
}
/**
@ -393,6 +363,58 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
return super.removeById(postId);
}
@Override
public Page<PostSimpleOutputDTO> convertToSimpleDto(@NonNull Page<Post> postPage) {
Assert.notNull(postPage, "Post page must not be null");
return postPage.map(post -> new PostSimpleOutputDTO().convertFrom(post));
}
@Override
public Page<PostListVO> convertToListVo(Page<Post> postPage) {
Assert.notNull(postPage, "Post page must not be null");
List<Post> posts = postPage.getContent();
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);
Optional.ofNullable(tagListMap.get(post.getId())).orElseGet(LinkedList::new);
// Set tags
postListVO.setTags(Optional.ofNullable(tagListMap.get(post.getId()))
.orElseGet(LinkedList::new)
.stream()
.map(tag -> new TagOutputDTO().<TagOutputDTO>convertFrom(tag))
.collect(Collectors.toList()));
// Set categories
postListVO.setCategories(Optional.ofNullable(categoryListMap.get(post.getId()))
.orElseGet(LinkedList::new)
.stream()
.map(category -> new CategoryOutputDTO().<CategoryOutputDTO>convertFrom(category))
.collect(Collectors.toList()));
// Set comment count
postListVO.setCommentCount(commentCountMap.getOrDefault(post.getId(), 0L));
return postListVO;
});
}
/**
* Converts to post minimal output dto.
*
@ -411,13 +433,6 @@ public class PostServiceImpl extends AbstractCrudService<Post, Integer> implemen
.collect(Collectors.toList());
}
@Override
public Page<PostSimpleOutputDTO> convertTo(@NonNull Page<Post> postPage) {
Assert.notNull(postPage, "Post page must not be null");
return postPage.map(post -> new PostSimpleOutputDTO().convertFrom(post));
}
/**
* Converts to post detail vo.
*

View File

@ -12,6 +12,7 @@ import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.PostParam;
import run.halo.app.model.params.PostQuery;
import run.halo.app.model.vo.PostDetailVO;
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.*;
import javax.validation.Valid;
@ -53,10 +54,10 @@ public class PostController {
@GetMapping
@ApiOperation("Lists posts")
public Page<PostSimpleOutputDTO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
PostQuery postQuery) {
public Page<PostListVO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
PostQuery postQuery) {
Page<Post> postPage = postService.pageBy(postQuery, pageable);
return postService.convertTo(postPage);
return postService.convertToListVo(postPage);
}
@GetMapping("latest")