mirror of https://github.com/halo-dev/halo
Create pageBy api for comment.
parent
54b4f9da68
commit
9e9bd1a377
|
@ -0,0 +1,24 @@
|
||||||
|
package run.halo.app.model.params;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import run.halo.app.model.enums.CommentStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comment query params.
|
||||||
|
*
|
||||||
|
* @author : RYAN0UP
|
||||||
|
* @date : 2019/04/18
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class CommentQuery {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keyword.
|
||||||
|
*/
|
||||||
|
private String keyword;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comment status.
|
||||||
|
*/
|
||||||
|
private CommentStatus status;
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package run.halo.app.repository;
|
package run.halo.app.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
import run.halo.app.model.entity.Comment;
|
import run.halo.app.model.entity.Comment;
|
||||||
import run.halo.app.model.enums.CommentStatus;
|
import run.halo.app.model.enums.CommentStatus;
|
||||||
import run.halo.app.model.projection.CommentCountProjection;
|
import run.halo.app.model.projection.CommentCountProjection;
|
||||||
|
@ -9,7 +10,6 @@ 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 org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import run.halo.app.repository.base.BaseRepository;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ import java.util.List;
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @date 3/21/19
|
* @date 3/21/19
|
||||||
*/
|
*/
|
||||||
public interface CommentRepository extends BaseRepository<Comment, Long> {
|
public interface CommentRepository extends BaseRepository<Comment, Long>, JpaSpecificationExecutor<Comment> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds all comments by status.
|
* Finds all comments by status.
|
||||||
|
|
|
@ -2,6 +2,7 @@ package run.halo.app.service;
|
||||||
|
|
||||||
import run.halo.app.model.entity.Comment;
|
import run.halo.app.model.entity.Comment;
|
||||||
import run.halo.app.model.enums.CommentStatus;
|
import run.halo.app.model.enums.CommentStatus;
|
||||||
|
import run.halo.app.model.params.CommentQuery;
|
||||||
import run.halo.app.model.vo.CommentWithParentVO;
|
import run.halo.app.model.vo.CommentWithParentVO;
|
||||||
import run.halo.app.model.vo.CommentWithPostVO;
|
import run.halo.app.model.vo.CommentWithPostVO;
|
||||||
import run.halo.app.model.vo.CommentVO;
|
import run.halo.app.model.vo.CommentVO;
|
||||||
|
@ -10,7 +11,6 @@ import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import run.halo.app.service.base.CrudService;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -50,6 +50,16 @@ public interface CommentService extends CrudService<Comment, Long> {
|
||||||
@NonNull
|
@NonNull
|
||||||
Page<CommentWithPostVO> pageBy(@NonNull CommentStatus status, @NonNull Pageable pageable);
|
Page<CommentWithPostVO> pageBy(@NonNull CommentStatus status, @NonNull Pageable pageable);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pages comments.
|
||||||
|
*
|
||||||
|
* @param commentQuery comment query must not be null
|
||||||
|
* @param pageable page info must not be null
|
||||||
|
* @return a page of comment
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
Page<CommentWithPostVO> pageBy(@NonNull CommentQuery commentQuery, @NonNull Pageable pageable);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists comments by post id.
|
* Lists comments by post id.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,25 @@
|
||||||
package run.halo.app.service.impl;
|
package run.halo.app.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.URLUtil;
|
||||||
|
import cn.hutool.crypto.SecureUtil;
|
||||||
|
import cn.hutool.extra.servlet.ServletUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.data.domain.*;
|
||||||
|
import org.springframework.data.domain.Sort.Order;
|
||||||
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
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 org.springframework.web.util.HtmlUtils;
|
||||||
import run.halo.app.model.dto.post.PostMinimalOutputDTO;
|
import run.halo.app.model.dto.post.PostMinimalOutputDTO;
|
||||||
import run.halo.app.model.entity.Comment;
|
import run.halo.app.model.entity.Comment;
|
||||||
import run.halo.app.model.entity.Post;
|
import run.halo.app.model.entity.Post;
|
||||||
import run.halo.app.model.enums.CommentStatus;
|
import run.halo.app.model.enums.CommentStatus;
|
||||||
|
import run.halo.app.model.params.CommentQuery;
|
||||||
import run.halo.app.model.projection.CommentCountProjection;
|
import run.halo.app.model.projection.CommentCountProjection;
|
||||||
import run.halo.app.model.properties.CommentProperties;
|
import run.halo.app.model.properties.CommentProperties;
|
||||||
import run.halo.app.model.support.CommentPage;
|
import run.halo.app.model.support.CommentPage;
|
||||||
|
@ -19,26 +35,8 @@ import run.halo.app.service.OptionService;
|
||||||
import run.halo.app.service.base.AbstractCrudService;
|
import run.halo.app.service.base.AbstractCrudService;
|
||||||
import run.halo.app.utils.OwoUtil;
|
import run.halo.app.utils.OwoUtil;
|
||||||
import run.halo.app.utils.ServiceUtils;
|
import run.halo.app.utils.ServiceUtils;
|
||||||
import cn.hutool.core.util.URLUtil;
|
|
||||||
import cn.hutool.crypto.SecureUtil;
|
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.springframework.data.domain.*;
|
|
||||||
import org.springframework.data.domain.Sort.Order;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
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 org.springframework.web.util.HtmlUtils;
|
|
||||||
import run.halo.app.repository.CommentRepository;
|
|
||||||
import run.halo.app.repository.PostRepository;
|
|
||||||
import run.halo.app.security.authentication.Authentication;
|
|
||||||
import run.halo.app.security.context.SecurityContextHolder;
|
|
||||||
import run.halo.app.service.base.AbstractCrudService;
|
|
||||||
|
|
||||||
|
import javax.persistence.criteria.Predicate;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
@ -89,6 +87,40 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
|
||||||
return convertBy(commentPage);
|
return convertBy(commentPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<CommentWithPostVO> pageBy(CommentQuery commentQuery, Pageable pageable) {
|
||||||
|
Assert.notNull(commentQuery, "Comment query must not be null");
|
||||||
|
Assert.notNull(pageable, "Page info must not be null");
|
||||||
|
Page<Comment> commentPage = commentRepository.findAll(buildSpecByQuery(commentQuery), pageable);
|
||||||
|
return convertBy(commentPage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private Specification<Comment> buildSpecByQuery(@NonNull CommentQuery commentQuery) {
|
||||||
|
Assert.notNull(commentQuery, "Comment query must not be null");
|
||||||
|
|
||||||
|
return (Specification<Comment>) (root, query, criteriaBuilder) -> {
|
||||||
|
List<Predicate> predicates = new LinkedList<>();
|
||||||
|
|
||||||
|
if (commentQuery.getStatus() != null) {
|
||||||
|
predicates.add(criteriaBuilder.equal(root.get("status"), commentQuery.getStatus()));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (commentQuery.getKeyword() != null) {
|
||||||
|
|
||||||
|
String likeCondition = String.format("%%%s%%", StringUtils.strip(commentQuery.getKeyword()));
|
||||||
|
|
||||||
|
Predicate authorLike = criteriaBuilder.like(root.get("author"), likeCondition);
|
||||||
|
Predicate contentLike = criteriaBuilder.like(root.get("content"), likeCondition);
|
||||||
|
Predicate emailLike = criteriaBuilder.like(root.get("email"), likeCondition);
|
||||||
|
|
||||||
|
predicates.add(criteriaBuilder.or(authorLike, contentLike, emailLike));
|
||||||
|
}
|
||||||
|
|
||||||
|
return query.where(predicates.toArray(new Predicate[0])).getRestriction();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Comment> listBy(Integer postId) {
|
public List<Comment> listBy(Integer postId) {
|
||||||
Assert.notNull(postId, "Post id must not be null");
|
Assert.notNull(postId, "Post id must not be null");
|
||||||
|
|
|
@ -1,16 +1,5 @@
|
||||||
package run.halo.app.web.controller.admin.api;
|
package run.halo.app.web.controller.admin.api;
|
||||||
|
|
||||||
import run.halo.app.model.dto.CommentOutputDTO;
|
|
||||||
import run.halo.app.model.entity.Comment;
|
|
||||||
import run.halo.app.model.entity.User;
|
|
||||||
import run.halo.app.model.enums.CommentStatus;
|
|
||||||
import run.halo.app.model.params.CommentParam;
|
|
||||||
import run.halo.app.model.properties.BlogProperties;
|
|
||||||
import run.halo.app.model.vo.CommentWithPostVO;
|
|
||||||
import run.halo.app.service.CommentService;
|
|
||||||
import run.halo.app.service.OptionService;
|
|
||||||
import run.halo.app.service.PostService;
|
|
||||||
import run.halo.app.utils.ValidationUtils;
|
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
@ -22,9 +11,11 @@ import run.halo.app.model.entity.Comment;
|
||||||
import run.halo.app.model.entity.User;
|
import run.halo.app.model.entity.User;
|
||||||
import run.halo.app.model.enums.CommentStatus;
|
import run.halo.app.model.enums.CommentStatus;
|
||||||
import run.halo.app.model.params.CommentParam;
|
import run.halo.app.model.params.CommentParam;
|
||||||
|
import run.halo.app.model.params.CommentQuery;
|
||||||
import run.halo.app.model.properties.BlogProperties;
|
import run.halo.app.model.properties.BlogProperties;
|
||||||
import run.halo.app.model.vo.CommentWithPostVO;
|
import run.halo.app.model.vo.CommentWithPostVO;
|
||||||
import run.halo.app.service.CommentService;
|
import run.halo.app.service.CommentService;
|
||||||
|
import run.halo.app.service.OptionService;
|
||||||
import run.halo.app.service.PostService;
|
import run.halo.app.service.PostService;
|
||||||
import run.halo.app.utils.ValidationUtils;
|
import run.halo.app.utils.ValidationUtils;
|
||||||
|
|
||||||
|
@ -57,6 +48,13 @@ public class CommentController {
|
||||||
this.optionService = optionService;
|
this.optionService = optionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
@ApiOperation("Lists comments")
|
||||||
|
public Page<CommentWithPostVO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
|
||||||
|
CommentQuery commentQuery) {
|
||||||
|
return commentService.pageBy(commentQuery, pageable);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("latest")
|
@GetMapping("latest")
|
||||||
@ApiOperation("Pages latest comments")
|
@ApiOperation("Pages latest comments")
|
||||||
public List<CommentWithPostVO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
|
public List<CommentWithPostVO> pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {
|
||||||
|
|
Loading…
Reference in New Issue