From 56213b9cdba7fd4a20a349c5af1e1457c700623e Mon Sep 17 00:00:00 2001 From: ruibaby Date: Tue, 19 Feb 2019 18:45:33 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ryanc/halo/repository/PostRepository.java | 23 ----- .../cc/ryanc/halo/service/PostService.java | 15 +-- .../halo/service/impl/PostServiceImpl.java | 95 +++++++++---------- .../web/controller/admin/PostController.java | 38 +------- .../front/FrontSearchController.java | 2 +- 5 files changed, 49 insertions(+), 124 deletions(-) diff --git a/src/main/java/cc/ryanc/halo/repository/PostRepository.java b/src/main/java/cc/ryanc/halo/repository/PostRepository.java index 9dd9edc57..0d376c226 100644 --- a/src/main/java/cc/ryanc/halo/repository/PostRepository.java +++ b/src/main/java/cc/ryanc/halo/repository/PostRepository.java @@ -39,29 +39,6 @@ public interface PostRepository extends JpaRepository, JpaSpecificat */ List findPostsByPostType(String postType); - /** - * 模糊查询 - * - * @param postType0 postType0 - * @param postStatus0 postStatus0 - * @param postTitle postTitle - * @param postType1 postType1 - * @param postStatus1 postStatus1 - * @param postContent postContent - * @param pageable pageable - * @return Page - */ - @Deprecated - Page findByPostTypeAndPostStatusAndPostTitleLikeOrPostTypeAndPostStatusAndPostContentLike( - String postType0, - Integer postStatus0, - String postTitle, - String postType1, - Integer postStatus1, - String postContent, - Pageable pageable - ); - /** * 根据文章的状态查询 分页 * diff --git a/src/main/java/cc/ryanc/halo/service/PostService.java b/src/main/java/cc/ryanc/halo/service/PostService.java index c83ab0329..7e9a2e20f 100755 --- a/src/main/java/cc/ryanc/halo/service/PostService.java +++ b/src/main/java/cc/ryanc/halo/service/PostService.java @@ -64,19 +64,6 @@ public interface PostService { */ List findAll(String postType); - /** - * 模糊查询文章 - * - * @param keyword 关键词 - * @param postType 文章类型 - * @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 searchPosts(String keyword, String postType, Integer postStatus, Pageable pageable); - /** * 模糊查询文章 * @@ -87,7 +74,7 @@ public interface PostService { * @return a page of posts */ @NonNull - Page searchPostsBy(@Nullable String keyword, @Nullable String postType, @Nullable Integer postStatus, @NonNull Pageable pageable); + Page searchPosts(@Nullable String keyword, @Nullable String postType, @Nullable Integer postStatus, @NonNull Pageable pageable); /** diff --git a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java index 978c20926..99a75d87b 100755 --- a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java @@ -140,36 +140,9 @@ public class PostServiceImpl implements PostService { return postRepository.findPostsByPostType(postType); } - /** - * 模糊查询文章 - * - * @param keyword 关键词 - * @param postType 文章类型 - * @param postStatus 文章状态 - * @param pageable 分页信息 - * @return Page - */ @Override public Page searchPosts(String keyword, String postType, Integer postStatus, Pageable pageable) { - return postRepository.findByPostTypeAndPostStatusAndPostTitleLikeOrPostTypeAndPostStatusAndPostContentLike( - postType, - postStatus, - "%" + keyword + "%", - postType, - postStatus, - "%" + keyword + "%", - pageable - ).map(post -> { - if (StrUtil.isNotEmpty(post.getPostPassword())) { - post.setPostSummary("该文章为加密文章"); - } - return post; - }); - } - - @Override - public Page searchPostsBy(String keyword, String postType, Integer postStatus, Pageable pageable) { - return postRepository.findAll(buildSearchSepcification(keyword, postType, postStatus), pageable) + return postRepository.findAll(buildSearchSpecification(keyword, postType, postStatus), pageable) .map(post -> { if (StrUtil.isNotEmpty(post.getPostPassword())) { post.setPostSummary("该文章为加密文章"); @@ -542,50 +515,68 @@ public class PostServiceImpl implements PostService { return postRepository.getPostsByLimit(limit); } + /** + * build Specification for post + * + * @param keyword keyword + * @param postType postType + * @param postStatus postStatus + * @return Specification + */ @NonNull - private Specification buildSearchSepcification(@NonNull String keyword, + private Specification buildSearchSpecification(@NonNull String keyword, @NonNull String postType, @NonNull Integer postStatus) { - return Specification.where(postTitleLike(keyword)).or(postContentLike(keyword)).and(postTypeEqual(postType)).and(postStatusEqual(postStatus)); -// return (root, criteriaQuery, criteriaBuilder) -> { -// List predicates = new LinkedList<>(); -// -// if (StringUtils.hasText(keyword)) { -// predicates.add(criteriaBuilder.like(root.get("postContent"), keyword)); -// predicates.add(criteriaBuilder.or(criteriaBuilder.like(root.get("postTitle"), keyword))); -// } -// -// if (StringUtils.hasText(postType)) { -// predicates.add(criteriaBuilder.equal(root.get("postType"), postType)); -// } -// -// if (postStatus != null) { -// predicates.add(criteriaBuilder.equal(root.get("postStatus"), postStatus)); -// } -// -// return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); -// }; + return Specification + .where(postTitleLike(keyword)) + .or(postContentLike(keyword)) + .and(postTypeEqual(postType)) + .and(postStatusEqual(postStatus)); } + /** + * build with postContent + * + * @param keyword keyword + * @return Specification + */ private Specification postContentLike(@NonNull String keyword) { Assert.hasText(keyword, "Keyword must not be blank"); - return (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.like(criteriaBuilder.lower(root.get("postContent")), "%" + keyword.toLowerCase() + "%"); } + /** + * build with postTitle + * + * @param keyword keyword + * @return Specification + */ private Specification postTitleLike(@NonNull String keyword) { Assert.hasText(keyword, "Keyword must not be blank"); - return (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.like(criteriaBuilder.lower(root.get("postTitle")), "%" + keyword.toLowerCase() + "%"); } + /** + * build with postType + * + * @param postType postType + * @return Specification + */ private Specification postTypeEqual(@NonNull String postType) { - return (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("postType"), postType); + return (root, criteriaQuery, criteriaBuilder) -> + criteriaBuilder.equal(root.get("postType"), postType); } + /** + * build with postStatus + * + * @param postStatus postStatus + * @return Specification + */ private Specification postStatusEqual(@NonNull Integer postStatus) { - return (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("postStatus"), postStatus); + return (root, criteriaQuery, criteriaBuilder) -> + criteriaBuilder.equal(root.get("postStatus"), postStatus); } } diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java index d4485fbf3..c81597b88 100755 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/PostController.java @@ -1,6 +1,7 @@ package cc.ryanc.halo.web.controller.admin; import cc.ryanc.halo.model.domain.Post; +import cc.ryanc.halo.model.domain.PostMeta; import cc.ryanc.halo.model.domain.User; import cc.ryanc.halo.model.dto.JsonResult; import cc.ryanc.halo.model.dto.LogsRecord; @@ -33,6 +34,7 @@ import javax.servlet.http.HttpSession; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import java.util.Map; import java.util.Optional; import static cc.ryanc.halo.model.dto.HaloConst.OPTIONS; @@ -99,40 +101,6 @@ public class PostController extends BaseController { return "admin/admin_post"; } - /** - * 模糊查询文章 - * - * @param model Model - * @param keyword keyword 关键字 - * @return 模板路径admin/admin_post - */ - @PostMapping(value = "/search") - public String searchPost(Model model, - @RequestParam(value = "keyword") String keyword, - @PageableDefault(sort = "postId", direction = DESC) Pageable pageable) { - try { - Page posts = postService.searchPostsBy(keyword, PostTypeEnum.POST_TYPE_POST.getDesc(), PostStatusEnum.PUBLISHED.getCode(), pageable); - model.addAttribute("posts", posts); - } catch (Exception e) { - log.error("未知错误:{}", e.getMessage()); - } - return "admin/admin_post"; - } - - /** - * 处理预览文章的请求 - * - * @param postId 文章编号 - * @param model model - * @return 模板路径/themes/{theme}/post - */ - @GetMapping(value = "/view") - public String viewPost(@RequestParam("postId") Long postId, Model model) { - final Optional post = postService.findByPostId(postId); - model.addAttribute("post", post.orElse(new Post())); - return this.render("post"); - } - /** * 处理跳转到新建文章页面 * @@ -170,7 +138,9 @@ public class PostController extends BaseController { public JsonResult save(@ModelAttribute Post post, @RequestParam("cateList") List cateList, @RequestParam("tagList") String tagList, + @RequestParam("metas") List metas, HttpSession session) { + post.setPostMetas(metas); final User user = (User) session.getAttribute(USER_SESSION_KEY); try { post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd())); diff --git a/src/main/java/cc/ryanc/halo/web/controller/front/FrontSearchController.java b/src/main/java/cc/ryanc/halo/web/controller/front/FrontSearchController.java index d3bd61672..a8f910a68 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/front/FrontSearchController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/front/FrontSearchController.java @@ -73,7 +73,7 @@ public class FrontSearchController extends BaseController { size = Integer.parseInt(OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp())); } final Pageable pageable = PageRequest.of(page - 1, size, sort); - final Page posts = postService.searchPostsBy(HtmlUtil.escape(keyword), PostTypeEnum.POST_TYPE_POST.getDesc(), PostStatusEnum.PUBLISHED.getCode(), pageable); + final Page posts = postService.searchPosts(HtmlUtil.escape(keyword), PostTypeEnum.POST_TYPE_POST.getDesc(), PostStatusEnum.PUBLISHED.getCode(), pageable); final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3); model.addAttribute("is_search", true); model.addAttribute("keyword", keyword);