mirror of https://github.com/halo-dev/halo
Consider Specification to refactor search method on PostService
parent
c06fcf4f0a
commit
9585f1553f
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
||||
/**
|
||||
* 根据文章状态查询 分页,用于后台管理
|
||||
*
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -209,7 +209,6 @@ public class PostController extends BaseController {
|
|||
* @param post post
|
||||
* @param cateList 分类目录
|
||||
* @param tagList 标签
|
||||
* @param session session
|
||||
* @return JsonResult
|
||||
*/
|
||||
@PostMapping(value = "/update")
|
||||
|
|
Loading…
Reference in New Issue