From 24a18882477f7a07cf94a5032cbab16102717f08 Mon Sep 17 00:00:00 2001 From: johnniang Date: Thu, 21 Mar 2019 13:46:44 +0800 Subject: [PATCH] Complete page by status api --- .../halo/repository/CommentRepository.java | 18 ++++++ .../cc/ryanc/halo/service/CommentService.java | 14 +++++ .../halo/service/impl/CommentServiceImpl.java | 57 ++++++++++++++++--- .../admin/api/CommentController.java | 17 ++++-- 4 files changed, 94 insertions(+), 12 deletions(-) diff --git a/src/main/java/cc/ryanc/halo/repository/CommentRepository.java b/src/main/java/cc/ryanc/halo/repository/CommentRepository.java index 40f77ade2..1364fd8c3 100644 --- a/src/main/java/cc/ryanc/halo/repository/CommentRepository.java +++ b/src/main/java/cc/ryanc/halo/repository/CommentRepository.java @@ -1,10 +1,28 @@ package cc.ryanc.halo.repository; import cc.ryanc.halo.model.entity.Comment; +import cc.ryanc.halo.model.enums.CommentStatus; import cc.ryanc.halo.repository.base.BaseRepository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; /** + * Comment repository. * + * @author johnniang + * @date 3/21/19 */ public interface CommentRepository extends BaseRepository { + + /** + * Finds all comments by status. + * + * @param status status must not be null + * @param pageable page info must not be null + * @return a page of comment + */ + @NonNull + Page findAllByStatus(@Nullable CommentStatus status, @NonNull Pageable pageable); } diff --git a/src/main/java/cc/ryanc/halo/service/CommentService.java b/src/main/java/cc/ryanc/halo/service/CommentService.java index 0712bf1c6..1036131fb 100644 --- a/src/main/java/cc/ryanc/halo/service/CommentService.java +++ b/src/main/java/cc/ryanc/halo/service/CommentService.java @@ -1,9 +1,12 @@ package cc.ryanc.halo.service; import cc.ryanc.halo.model.entity.Comment; +import cc.ryanc.halo.model.enums.CommentStatus; import cc.ryanc.halo.model.vo.CommentVO; import cc.ryanc.halo.service.base.CrudService; import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.lang.NonNull; /** * Comment service. @@ -18,5 +21,16 @@ public interface CommentService extends CrudService { * @param top top number must not be less than 0 * @return a page of comments */ + @NonNull Page pageLatest(int top); + + /** + * Pages comments. + * + * @param status comment status must not be null + * @param pageable page info must not be null + * @return a page of comment + */ + @NonNull + Page pageBy(@NonNull CommentStatus status, @NonNull Pageable pageable); } diff --git a/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java index 6f33789d8..18bec7885 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java @@ -3,20 +3,25 @@ package cc.ryanc.halo.service.impl; 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.vo.CommentVO; import cc.ryanc.halo.repository.CommentRepository; 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.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Sort; +import org.springframework.data.domain.*; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; 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.stream.Collectors; /** * CommentService implementation class @@ -45,16 +50,52 @@ public class CommentServiceImpl extends AbstractCrudService imple // Build page request PageRequest latestPageable = PageRequest.of(0, top, Sort.by(Sort.Direction.DESC, "createTime")); - // List all comments - Page comments = listAll(latestPageable); + return convertBy(listAll(latestPageable)); + } + + @Override + public Page pageBy(CommentStatus status, Pageable pageable) { + Assert.notNull(status, "Comment status must not be null"); + Assert.notNull(pageable, "Page info must not be null"); + + // Find all + Page commentPage = commentRepository.findAllByStatus(status, pageable); + + return convertBy(commentPage); + } + + /** + * Converts to comment vo page. + * + * @param commentPage comment page must not be null + * @return a page of comment vo + */ + @NonNull + private Page convertBy(@NonNull Page commentPage) { + Assert.notNull(commentPage, "Comment page must not be null"); + + return new PageImpl<>(convertBy(commentPage.getContent()), commentPage.getPageable(), commentPage.getTotalElements()); + } + + /** + * Converts to comment vo list. + * + * @param comments comment list + * @return a list of comment vo + */ + @NonNull + private List convertBy(@Nullable List comments) { + if (CollectionUtils.isEmpty(comments)) { + return Collections.emptyList(); + } // Fetch goods ids - Set postIds = ServiceUtils.fetchProperty(comments.getContent(), Comment::getPostId); + Set postIds = ServiceUtils.fetchProperty(comments, Comment::getPostId); // Get all posts Map postMap = ServiceUtils.convertToMap(postService.listAllByIds(postIds), Post::getId); - return comments.map(comment -> { + return comments.stream().map(comment -> { // Convert to vo CommentVO commentVO = new CommentVO().convertFrom(comment); @@ -62,6 +103,6 @@ public class CommentServiceImpl extends AbstractCrudService imple commentVO.setPost(new PostMinimalOutputDTO().convertFrom(postMap.get(comment.getPostId()))); return commentVO; - }); + }).collect(Collectors.toList()); } } diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/api/CommentController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/api/CommentController.java index 2a1157a5f..a39b3c6e1 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/api/CommentController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/api/CommentController.java @@ -1,15 +1,18 @@ package cc.ryanc.halo.web.controller.admin.api; +import cc.ryanc.halo.model.enums.CommentStatus; import cc.ryanc.halo.model.vo.CommentVO; import cc.ryanc.halo.service.CommentService; import io.swagger.annotations.ApiOperation; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; +import org.springframework.web.bind.annotation.*; import java.util.List; +import static org.springframework.data.domain.Sort.Direction.DESC; + /** * Comment controller. * @@ -31,4 +34,10 @@ public class CommentController { public List pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) { return commentService.pageLatest(top).getContent(); } + + @GetMapping("status/{status}") + public Page pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable, + @PathVariable("status") CommentStatus status) { + return commentService.pageBy(status, pageable); + } }