mirror of https://github.com/halo-dev/halo
Complete get journal page api
parent
25a8d663db
commit
f5ebfbe8dc
|
@ -1,6 +1,8 @@
|
||||||
package run.halo.app.controller.admin.api;
|
package run.halo.app.controller.admin.api;
|
||||||
|
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import run.halo.app.model.dto.BaseCommentDTO;
|
import run.halo.app.model.dto.BaseCommentDTO;
|
||||||
import run.halo.app.model.entity.Journal;
|
import run.halo.app.model.entity.Journal;
|
||||||
|
@ -25,6 +27,13 @@ public class JournalController {
|
||||||
this.journalService = journalService;
|
this.journalService = journalService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
@ApiOperation("Gets latest journals")
|
||||||
|
public Page<BaseCommentDTO> pageBy(Pageable pageable) {
|
||||||
|
Page<Journal> journalPage = journalService.pageBy(pageable);
|
||||||
|
return journalService.convertTo(journalPage);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("latest")
|
@GetMapping("latest")
|
||||||
@ApiOperation("Gets latest journals")
|
@ApiOperation("Gets latest journals")
|
||||||
public List<BaseCommentDTO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
|
public List<BaseCommentDTO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package run.halo.app.repository;
|
package run.halo.app.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import run.halo.app.model.entity.Journal;
|
import run.halo.app.model.entity.Journal;
|
||||||
|
@ -25,4 +27,13 @@ public interface JournalRepository extends BaseCommentRepository<Journal> {
|
||||||
@Query("select new run.halo.app.model.projection.CommentCountProjection(count(comment.id), comment.postId) from Journal comment where comment.postId in ?1 group by comment.postId")
|
@Query("select new run.halo.app.model.projection.CommentCountProjection(count(comment.id), comment.postId) from Journal comment where comment.postId in ?1 group by comment.postId")
|
||||||
@NonNull
|
@NonNull
|
||||||
List<CommentCountProjection> countByPostIds(@NonNull Iterable<Integer> postIds);
|
List<CommentCountProjection> countByPostIds(@NonNull Iterable<Integer> postIds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds all journals by parent id.
|
||||||
|
*
|
||||||
|
* @param parentId parent id must not be null
|
||||||
|
* @param pageable page info must not be null
|
||||||
|
* @return a page of journal
|
||||||
|
*/
|
||||||
|
Page<Journal> findAllByParentId(@NonNull Long parentId, @NonNull Pageable pageable);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package run.halo.app.service;
|
package run.halo.app.service;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import run.halo.app.model.entity.Journal;
|
import run.halo.app.model.entity.Journal;
|
||||||
import run.halo.app.model.params.JournalParam;
|
import run.halo.app.model.params.JournalParam;
|
||||||
|
@ -22,4 +24,11 @@ public interface JournalService extends BaseCommentService<Journal> {
|
||||||
@NonNull
|
@NonNull
|
||||||
Journal createBy(@NonNull JournalParam journalParam);
|
Journal createBy(@NonNull JournalParam journalParam);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a page of journal
|
||||||
|
*
|
||||||
|
* @param pageable page info must not be null
|
||||||
|
* @return a page of journal
|
||||||
|
*/
|
||||||
|
Page<Journal> pageBy(@NonNull Pageable pageable);
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,4 +133,13 @@ public interface BaseCommentService<COMMENT extends BaseComment> extends CrudSer
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
List<BaseCommentDTO> convertTo(@NonNull List<COMMENT> comments);
|
List<BaseCommentDTO> convertTo(@NonNull List<COMMENT> comments);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts to base comment dto page.
|
||||||
|
*
|
||||||
|
* @param commentPage comment page must not be null
|
||||||
|
* @return a page of base comment dto
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
Page<BaseCommentDTO> convertTo(@NonNull Page<COMMENT> commentPage);
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,12 +78,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<COMMENT> pageLatest(int top) {
|
public Page<COMMENT> pageLatest(int top) {
|
||||||
Assert.isTrue(top > 0, "Top number must not be less than 0");
|
return listAll(buildLatestPageable(top));
|
||||||
|
|
||||||
// Build page request
|
|
||||||
PageRequest latestPageable = PageRequest.of(0, top, Sort.by(Sort.Direction.DESC, "createTime"));
|
|
||||||
|
|
||||||
return listAll(latestPageable);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -293,6 +288,13 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<BaseCommentDTO> convertTo(Page<COMMENT> commentPage) {
|
||||||
|
Assert.notNull(commentPage, "Comment page must not be null");
|
||||||
|
|
||||||
|
return commentPage.map(this::convertTo);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseCommentDTO convertTo(COMMENT comment) {
|
public BaseCommentDTO convertTo(COMMENT comment) {
|
||||||
Assert.notNull(comment, "Comment must not be null");
|
Assert.notNull(comment, "Comment must not be null");
|
||||||
|
@ -301,7 +303,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private Specification<COMMENT> buildSpecByQuery(@NonNull CommentQuery commentQuery) {
|
protected Specification<COMMENT> buildSpecByQuery(@NonNull CommentQuery commentQuery) {
|
||||||
Assert.notNull(commentQuery, "Comment query must not be null");
|
Assert.notNull(commentQuery, "Comment query must not be null");
|
||||||
|
|
||||||
return (Specification<COMMENT>) (root, query, criteriaBuilder) -> {
|
return (Specification<COMMENT>) (root, query, criteriaBuilder) -> {
|
||||||
|
@ -332,7 +334,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
* @param sort sort info
|
* @param sort sort info
|
||||||
* @return comment comparator
|
* @return comment comparator
|
||||||
*/
|
*/
|
||||||
private Comparator<BaseCommentVO> buildCommentComparator(Sort sort) {
|
protected Comparator<BaseCommentVO> buildCommentComparator(Sort sort) {
|
||||||
return (currentComment, toCompareComment) -> {
|
return (currentComment, toCompareComment) -> {
|
||||||
Assert.notNull(currentComment, "Current comment must not be null");
|
Assert.notNull(currentComment, "Current comment must not be null");
|
||||||
Assert.notNull(toCompareComment, "Comment to compare must not be null");
|
Assert.notNull(toCompareComment, "Comment to compare must not be null");
|
||||||
|
@ -358,7 +360,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
* @param comments comment list must not null
|
* @param comments comment list must not null
|
||||||
* @param commentComparator comment vo comparator
|
* @param commentComparator comment vo comparator
|
||||||
*/
|
*/
|
||||||
private void concreteTree(@NonNull BaseCommentVO parentComment,
|
protected void concreteTree(@NonNull BaseCommentVO parentComment,
|
||||||
@Nullable Collection<COMMENT> comments,
|
@Nullable Collection<COMMENT> comments,
|
||||||
@NonNull Comparator<BaseCommentVO> commentComparator) {
|
@NonNull Comparator<BaseCommentVO> commentComparator) {
|
||||||
Assert.notNull(parentComment, "Parent comment must not be null");
|
Assert.notNull(parentComment, "Parent comment must not be null");
|
||||||
|
@ -403,4 +405,17 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
|
||||||
parentComment.getChildren().sort(commentComparator);
|
parentComment.getChildren().sort(commentComparator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds latest page request.
|
||||||
|
*
|
||||||
|
* @param top top must not be less than 1
|
||||||
|
* @return latest page request
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
Pageable buildLatestPageable(int top) {
|
||||||
|
Assert.isTrue(top > 0, "Top number must not be less than 0");
|
||||||
|
|
||||||
|
return PageRequest.of(0, top, Sort.by(Sort.Direction.DESC, "createTime"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package run.halo.app.service.impl;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import run.halo.app.model.entity.Journal;
|
import run.halo.app.model.entity.Journal;
|
||||||
import run.halo.app.model.entity.User;
|
import run.halo.app.model.entity.User;
|
||||||
|
@ -57,4 +59,17 @@ public class JournalServiceImpl extends BaseCommentServiceImpl<Journal> implemen
|
||||||
// Convert, create and return
|
// Convert, create and return
|
||||||
return createBy(journalParam.convertTo());
|
return createBy(journalParam.convertTo());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<Journal> pageBy(Pageable pageable) {
|
||||||
|
Assert.notNull(pageable, "Page info must not be null");
|
||||||
|
|
||||||
|
return journalRepository.findAllByParentId(0L, pageable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<Journal> pageLatest(int top) {
|
||||||
|
return pageBy(buildLatestPageable(top));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue