diff --git a/src/main/java/run/halo/app/model/params/CommentQuery.java b/src/main/java/run/halo/app/model/params/CommentQuery.java new file mode 100644 index 000000000..ab86fec7b --- /dev/null +++ b/src/main/java/run/halo/app/model/params/CommentQuery.java @@ -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; +} diff --git a/src/main/java/run/halo/app/repository/CommentRepository.java b/src/main/java/run/halo/app/repository/CommentRepository.java index 370483564..9f8eef6da 100644 --- a/src/main/java/run/halo/app/repository/CommentRepository.java +++ b/src/main/java/run/halo/app/repository/CommentRepository.java @@ -1,5 +1,6 @@ package run.halo.app.repository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import run.halo.app.model.entity.Comment; import run.halo.app.model.enums.CommentStatus; 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.lang.NonNull; import org.springframework.lang.Nullable; -import run.halo.app.repository.base.BaseRepository; import java.util.List; @@ -19,7 +19,7 @@ import java.util.List; * @author johnniang * @date 3/21/19 */ -public interface CommentRepository extends BaseRepository { +public interface CommentRepository extends BaseRepository, JpaSpecificationExecutor { /** * Finds all comments by status. diff --git a/src/main/java/run/halo/app/service/CommentService.java b/src/main/java/run/halo/app/service/CommentService.java index 9a03c971f..21e787d72 100644 --- a/src/main/java/run/halo/app/service/CommentService.java +++ b/src/main/java/run/halo/app/service/CommentService.java @@ -2,6 +2,7 @@ package run.halo.app.service; import run.halo.app.model.entity.Comment; 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.CommentWithPostVO; 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.lang.NonNull; import org.springframework.lang.Nullable; -import run.halo.app.service.base.CrudService; import javax.servlet.http.HttpServletRequest; import java.util.Collection; @@ -50,6 +50,16 @@ public interface CommentService extends CrudService { @NonNull Page 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 pageBy(@NonNull CommentQuery commentQuery, @NonNull Pageable pageable); + /** * Lists comments by post id. * diff --git a/src/main/java/run/halo/app/service/impl/CommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/CommentServiceImpl.java index c32f5a437..b910e0f60 100644 --- a/src/main/java/run/halo/app/service/impl/CommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/CommentServiceImpl.java @@ -1,9 +1,25 @@ 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.entity.Comment; import run.halo.app.model.entity.Post; 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.properties.CommentProperties; 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.utils.OwoUtil; 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 java.util.*; import java.util.stream.Collectors; @@ -89,6 +87,40 @@ public class CommentServiceImpl extends AbstractCrudService imple return convertBy(commentPage); } + @Override + public Page pageBy(CommentQuery commentQuery, Pageable pageable) { + Assert.notNull(commentQuery, "Comment query must not be null"); + Assert.notNull(pageable, "Page info must not be null"); + Page commentPage = commentRepository.findAll(buildSpecByQuery(commentQuery), pageable); + return convertBy(commentPage); + } + + @NonNull + private Specification buildSpecByQuery(@NonNull CommentQuery commentQuery) { + Assert.notNull(commentQuery, "Comment query must not be null"); + + return (Specification) (root, query, criteriaBuilder) -> { + List 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 public List listBy(Integer postId) { Assert.notNull(postId, "Post id must not be null"); diff --git a/src/main/java/run/halo/app/web/controller/admin/api/CommentController.java b/src/main/java/run/halo/app/web/controller/admin/api/CommentController.java index 92f3d5d47..6872a4e6d 100644 --- a/src/main/java/run/halo/app/web/controller/admin/api/CommentController.java +++ b/src/main/java/run/halo/app/web/controller/admin/api/CommentController.java @@ -1,16 +1,5 @@ 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 org.apache.commons.lang3.StringUtils; 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.enums.CommentStatus; 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.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; @@ -57,6 +48,13 @@ public class CommentController { this.optionService = optionService; } + @GetMapping + @ApiOperation("Lists comments") + public Page pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable, + CommentQuery commentQuery) { + return commentService.pageBy(commentQuery, pageable); + } + @GetMapping("latest") @ApiOperation("Pages latest comments") public List pageLatest(@RequestParam(name = "top", defaultValue = "10") int top) {