Skip to content

Commit

Permalink
🎨 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Feb 19, 2019
1 parent 537df9f commit 56213b9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 124 deletions.
23 changes: 0 additions & 23 deletions src/main/java/cc/ryanc/halo/repository/PostRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,6 @@ public interface PostRepository extends JpaRepository<Post, Long>, JpaSpecificat
*/
List<Post> 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<Post> findByPostTypeAndPostStatusAndPostTitleLikeOrPostTypeAndPostStatusAndPostContentLike(
String postType0,
Integer postStatus0,
String postTitle,
String postType1,
Integer postStatus1,
String postContent,
Pageable pageable
);

/**
* 根据文章的状态查询 分页
*
Expand Down
15 changes: 1 addition & 14 deletions src/main/java/cc/ryanc/halo/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,6 @@ public interface PostService {
*/
List<Post> 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<Post> searchPosts(String keyword, String postType, Integer postStatus, Pageable pageable);

/**
* 模糊查询文章
*
Expand All @@ -87,7 +74,7 @@ public interface PostService {
* @return a page of posts
*/
@NonNull
Page<Post> searchPostsBy(@Nullable String keyword, @Nullable String postType, @Nullable Integer postStatus, @NonNull Pageable pageable);
Page<Post> searchPosts(@Nullable String keyword, @Nullable String postType, @Nullable Integer postStatus, @NonNull Pageable pageable);


/**
Expand Down
95 changes: 43 additions & 52 deletions src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,36 +140,9 @@ public List<Post> findAll(String postType) {
return postRepository.findPostsByPostType(postType);
}

/**
* 模糊查询文章
*
* @param keyword 关键词
* @param postType 文章类型
* @param postStatus 文章状态
* @param pageable 分页信息
* @return Page
*/
@Override
public Page<Post> 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<Post> 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("该文章为加密文章");
Expand Down Expand Up @@ -542,50 +515,68 @@ public List<Post> getRecentPosts(int limit) {
return postRepository.getPostsByLimit(limit);
}

/**
* build Specification for post
*
* @param keyword keyword
* @param postType postType
* @param postStatus postStatus
* @return Specification
*/
@NonNull
private Specification<Post> buildSearchSepcification(@NonNull String keyword,
private Specification<Post> 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<Predicate> 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<Post> 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<Post> 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<Post> 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<Post> postStatusEqual(@NonNull Integer postStatus) {
return (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("postStatus"), postStatus);
return (root, criteriaQuery, criteriaBuilder) ->
criteriaBuilder.equal(root.get("postStatus"), postStatus);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,6 +34,7 @@
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;
Expand Down Expand Up @@ -99,40 +101,6 @@ public String posts(Model model,
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<Post> 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> post = postService.findByPostId(postId);
model.addAttribute("post", post.orElse(new Post()));
return this.render("post");
}

/**
* 处理跳转到新建文章页面
*
Expand Down Expand Up @@ -170,7 +138,9 @@ public String editPost(@RequestParam("postId") Long postId, Model model) {
public JsonResult save(@ModelAttribute Post post,
@RequestParam("cateList") List<String> cateList,
@RequestParam("tagList") String tagList,
@RequestParam("metas") List<PostMeta> metas,
HttpSession session) {
post.setPostMetas(metas);
final User user = (User) session.getAttribute(USER_SESSION_KEY);
try {
post.setPostContent(MarkdownUtils.renderMarkdown(post.getPostContentMd()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public String search(Model model,
size = Integer.parseInt(OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()));
}
final Pageable pageable = PageRequest.of(page - 1, size, sort);
final Page<Post> posts = postService.searchPostsBy(HtmlUtil.escape(keyword), PostTypeEnum.POST_TYPE_POST.getDesc(), PostStatusEnum.PUBLISHED.getCode(), pageable);
final Page<Post> 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);
Expand Down

0 comments on commit 56213b9

Please sign in to comment.