Complete page by status api

pull/137/head
johnniang 2019-03-21 13:46:44 +08:00
parent 09a91b2692
commit 24a1888247
4 changed files with 94 additions and 12 deletions

View File

@ -1,10 +1,28 @@
package cc.ryanc.halo.repository; package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.Comment; import cc.ryanc.halo.model.entity.Comment;
import cc.ryanc.halo.model.enums.CommentStatus;
import cc.ryanc.halo.repository.base.BaseRepository; 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<Comment, Long> { public interface CommentRepository extends BaseRepository<Comment, Long> {
/**
* 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<Comment> findAllByStatus(@Nullable CommentStatus status, @NonNull Pageable pageable);
} }

View File

@ -1,9 +1,12 @@
package cc.ryanc.halo.service; package cc.ryanc.halo.service;
import cc.ryanc.halo.model.entity.Comment; 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.model.vo.CommentVO;
import cc.ryanc.halo.service.base.CrudService; import cc.ryanc.halo.service.base.CrudService;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
/** /**
* Comment service. * Comment service.
@ -18,5 +21,16 @@ public interface CommentService extends CrudService<Comment, Long> {
* @param top top number must not be less than 0 * @param top top number must not be less than 0
* @return a page of comments * @return a page of comments
*/ */
@NonNull
Page<CommentVO> pageLatest(int top); Page<CommentVO> 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<CommentVO> pageBy(@NonNull CommentStatus status, @NonNull Pageable pageable);
} }

View File

@ -3,20 +3,25 @@ package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.dto.post.PostMinimalOutputDTO; import cc.ryanc.halo.model.dto.post.PostMinimalOutputDTO;
import cc.ryanc.halo.model.entity.Comment; import cc.ryanc.halo.model.entity.Comment;
import cc.ryanc.halo.model.entity.Post; 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.model.vo.CommentVO;
import cc.ryanc.halo.repository.CommentRepository; import cc.ryanc.halo.repository.CommentRepository;
import cc.ryanc.halo.service.CommentService; import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.base.AbstractCrudService; import cc.ryanc.halo.service.base.AbstractCrudService;
import cc.ryanc.halo.utils.ServiceUtils; import cc.ryanc.halo.utils.ServiceUtils;
import org.springframework.data.domain.Page; import org.springframework.data.domain.*;
import org.springframework.data.domain.PageRequest; import org.springframework.lang.NonNull;
import org.springframework.data.domain.Sort; import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; 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.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
/** /**
* CommentService implementation class * CommentService implementation class
@ -45,16 +50,52 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
// Build page request // Build page request
PageRequest latestPageable = PageRequest.of(0, top, Sort.by(Sort.Direction.DESC, "createTime")); PageRequest latestPageable = PageRequest.of(0, top, Sort.by(Sort.Direction.DESC, "createTime"));
// List all comments return convertBy(listAll(latestPageable));
Page<Comment> comments = listAll(latestPageable); }
@Override
public Page<CommentVO> 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<Comment> 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<CommentVO> convertBy(@NonNull Page<Comment> 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<CommentVO> convertBy(@Nullable List<Comment> comments) {
if (CollectionUtils.isEmpty(comments)) {
return Collections.emptyList();
}
// Fetch goods ids // Fetch goods ids
Set<Integer> postIds = ServiceUtils.fetchProperty(comments.getContent(), Comment::getPostId); Set<Integer> postIds = ServiceUtils.fetchProperty(comments, Comment::getPostId);
// Get all posts // Get all posts
Map<Integer, Post> postMap = ServiceUtils.convertToMap(postService.listAllByIds(postIds), Post::getId); Map<Integer, Post> postMap = ServiceUtils.convertToMap(postService.listAllByIds(postIds), Post::getId);
return comments.map(comment -> { return comments.stream().map(comment -> {
// Convert to vo // Convert to vo
CommentVO commentVO = new CommentVO().convertFrom(comment); CommentVO commentVO = new CommentVO().convertFrom(comment);
@ -62,6 +103,6 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
commentVO.setPost(new PostMinimalOutputDTO().convertFrom(postMap.get(comment.getPostId()))); commentVO.setPost(new PostMinimalOutputDTO().convertFrom(postMap.get(comment.getPostId())));
return commentVO; return commentVO;
}); }).collect(Collectors.toList());
} }
} }

View File

@ -1,15 +1,18 @@
package cc.ryanc.halo.web.controller.admin.api; 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.model.vo.CommentVO;
import cc.ryanc.halo.service.CommentService; import cc.ryanc.halo.service.CommentService;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
import static org.springframework.data.domain.Sort.Direction.DESC;
/** /**
* Comment controller. * Comment controller.
* *
@ -31,4 +34,10 @@ public class CommentController {
public List<CommentVO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) { public List<CommentVO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
return commentService.pageLatest(top).getContent(); return commentService.pageLatest(top).getContent();
} }
@GetMapping("status/{status}")
public Page<CommentVO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
@PathVariable("status") CommentStatus status) {
return commentService.pageBy(status, pageable);
}
} }