Consider Specification to refactor search method on PostService

pull/87/head
johnniang 2019-01-30 21:37:17 +08:00
parent c06fcf4f0a
commit 9585f1553f
4 changed files with 61 additions and 9 deletions

View File

@ -5,7 +5,9 @@ import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
@ -20,7 +22,7 @@ import java.util.List;
* @author : RYAN0UP
* @date : 2017/11/14
*/
public interface PostRepository extends JpaRepository<Post, Long> {
public interface PostRepository extends JpaRepository<Post, Long>, JpaSpecificationExecutor<Post> {
/**
*
@ -50,6 +52,7 @@ public interface PostRepository extends JpaRepository<Post, Long> {
* @param pageable pageable
* @return Page
*/
@Deprecated
Page<Post> findByPostTypeAndPostStatusAndPostTitleLikeOrPostTypeAndPostStatusAndPostContentLike(
String postType0,
Integer postStatus0,

View File

@ -6,6 +6,7 @@ import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.model.dto.Archive;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Date;
@ -70,9 +71,24 @@ public interface PostService {
* @param postStatus
* @param pageable
* @return Page
* @see PostService#searchPostsBy(java.lang.String, java.lang.String, java.lang.Integer, org.springframework.data.domain.Pageable)
*/
@Deprecated
Page<Post> searchPosts(String keyword, String postType, Integer postStatus, Pageable pageable);
/**
*
*
* @param keyword
* @param postType
* @param postStatus
* @param pageable
* @return a page of posts
*/
@NonNull
Page<Post> searchPostsBy(String keyword, String postType, Integer postStatus, @NonNull Pageable pageable);
/**
*
*

View File

@ -13,7 +13,6 @@ import cc.ryanc.halo.service.CategoryService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.TagService;
import cc.ryanc.halo.utils.HaloUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
@ -22,12 +21,14 @@ import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.persistence.criteria.Predicate;
import java.util.*;
/**
* <pre>
@ -165,6 +166,17 @@ public class PostServiceImpl implements PostService {
return posts;
}
@Override
public Page<Post> searchPostsBy(String keyword, String postType, Integer postStatus, Pageable pageable) {
return postRepository.findAll(buildSearchSepcification(keyword, postType, postStatus), pageable)
.map(post -> {
if (StrUtil.isNotEmpty(post.getPostPassword())) {
post.setPostSummary("该文章为加密文章");
}
return post;
});
}
/**
*
*
@ -397,7 +409,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param category category
* @param status status
* @param pageable pageable
* @return Page
*/
@ -417,7 +428,6 @@ public class PostServiceImpl implements PostService {
*
*
* @param tag tag
* @param status status
* @param pageable pageable
* @return Page
*/
@ -563,4 +573,28 @@ public class PostServiceImpl implements PostService {
public List<Post> getRecentPosts(int limit) {
return postRepository.getPostsByLimit(limit);
}
@NonNull
private Specification<Post> buildSearchSepcification(@Nullable String keyword, @Nullable String postType, @Nullable Integer postStatus) {
return (Specification<Post>) (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new LinkedList<>();
if (StringUtils.hasText(keyword)) {
predicates.add(criteriaBuilder.like(root.get("postContent"), keyword));
}
if (StringUtils.hasText(postType)) {
predicates.add(criteriaBuilder.equal(root.get("postType"), postType));
predicates.add(criteriaBuilder.or(criteriaBuilder.like(root.get("postType"), postType)));
}
if (postStatus != null) {
predicates.add(criteriaBuilder.equal(root.get("postStatus"), postStatus));
predicates.add(criteriaBuilder.or(criteriaBuilder.like(root.get("postStatus"), postType)));
}
return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction();
};
}
}

View File

@ -209,7 +209,6 @@ public class PostController extends BaseController {
* @param post post
* @param cateList
* @param tagList
* @param session session
* @return JsonResult
*/
@PostMapping(value = "/update")