🎨 代码优化

pull/98/head
ruibaby 2019-02-19 18:45:33 +08:00
parent 537df9f59a
commit 56213b9cdb
5 changed files with 49 additions and 124 deletions

View File

@ -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
);
/**
*
*

View File

@ -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);
/**
*
*
@ -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);
/**

View File

@ -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<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("该文章为加密文章");
@ -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<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);
}
}

View File

@ -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<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");
}
/**
*
*
@ -170,7 +138,9 @@ public class PostController extends BaseController {
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()));

View File

@ -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<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);