diff --git a/src/main/java/cc/ryanc/halo/repository/PostRepository.java b/src/main/java/cc/ryanc/halo/repository/PostRepository.java index 89a39d13c..e5a10bb95 100644 --- a/src/main/java/cc/ryanc/halo/repository/PostRepository.java +++ b/src/main/java/cc/ryanc/halo/repository/PostRepository.java @@ -34,30 +34,31 @@ public interface PostRepository extends JpaRepository { * 查询所有文章 根据文章类型 * * @param postType post or page - * * @return List */ List findPostsByPostType(String postType); - /** - * 分页查询文章 - * - * @param postType post or page - * @param pageable 分页信息 - * - * @return Page - */ - Page findPostsByPostType(String postType, Pageable pageable); - /** * 模糊查询 * - * @param keyWord keyword - * @param pageable pageable - * - * @return List + * @param postType0 postType0 + * @param postStatus0 postStatus0 + * @param postTitle postTitle + * @param postType1 postType1 + * @param postStatus1 postStatus1 + * @param postContent postContent + * @param pageable pageable + * @return Page */ - List findByPostTitleLike(String keyWord, Pageable pageable); + Page findByPostTypeAndPostStatusAndPostTitleLikeOrPostTypeAndPostStatusAndPostContentLike( + String postType0, + Integer postStatus0, + String postTitle, + String postType1, + Integer postStatus1, + String postContent, + Pageable pageable + ); /** * 根据文章的状态查询 分页 @@ -65,7 +66,6 @@ public interface PostRepository extends JpaRepository { * @param status 0,1,2 * @param postType post or page * @param pageable 分页信息 - * * @return Page */ Page findPostsByPostStatusAndPostType(Integer status, String postType, Pageable pageable); @@ -75,7 +75,6 @@ public interface PostRepository extends JpaRepository { * * @param status 0,1,2 * @param postType post or page - * * @return List */ List findPostsByPostStatusAndPostType(Integer status, String postType); @@ -85,7 +84,6 @@ public interface PostRepository extends JpaRepository { * * @param postUrl 路径 * @param postType post or page - * * @return Post */ Post findPostByPostUrlAndPostType(String postUrl, String postType); @@ -95,7 +93,6 @@ public interface PostRepository extends JpaRepository { * * @param postId 文章编号 * @param postType post or page - * * @return Post */ Post findPostByPostIdAndPostType(Long postId, String postType); @@ -104,21 +101,19 @@ public interface PostRepository extends JpaRepository { * 查询指定日期之前的文章 * * @param postDate 日期 - * * @return Post */ @Query(value = "SELECT * FROM halo_post WHERE post_status = 0 AND post_type='post' AND post_date < :postDate ORDER BY post_date DESC LIMIT 1", nativeQuery = true) - Post queryPrePost(Date postDate); + Post queryPrePost(@Param("postDate") Date postDate); /** * 查询指定日期之后的文章 * * @param postDate 日期 - * * @return Post */ @Query(value = "SELECT * FROM halo_post WHERE post_status = 0 AND post_type='post' AND post_date > :postDate ORDER BY post_date ASC LIMIT 1", nativeQuery = true) - Post queryNextPost(Date postDate); + Post queryNextPost(@Param("postDate") Date postDate); /** * 查询文章归档信息 根据年份和月份 @@ -139,7 +134,6 @@ public interface PostRepository extends JpaRepository { /** * @return List - * * @Author Aquan * @Description 查询文章归档信息 查询所有文章 * @Date 2019.1.4 11:19 @@ -150,7 +144,6 @@ public interface PostRepository extends JpaRepository { /** * @return Integer - * * @Author Aquan * @Description 查询文章总数 * @Date 2019.1.4 15:03 @@ -165,7 +158,6 @@ public interface PostRepository extends JpaRepository { * * @param year year * @param month month - * * @return List */ @Query(value = "SELECT *,YEAR(post_date) AS YEAR,MONTH(post_date) AS MONTH FROM halo_post WHERE post_status=0 and post_type='post' AND YEAR(post_date)=:year AND MONTH(post_date)=:month ORDER BY post_date DESC", nativeQuery = true) @@ -175,7 +167,6 @@ public interface PostRepository extends JpaRepository { * 根据年份查询文章 * * @param year year - * * @return List */ @Query(value = "SELECT *,YEAR(post_date) AS YEAR FROM halo_post WHERE post_status=0 AND post_type='post' AND YEAR(post_date)=:year ORDER BY post_date DESC", nativeQuery = true) @@ -187,7 +178,6 @@ public interface PostRepository extends JpaRepository { * @param year year * @param month month * @param pageable pageable - * * @return Page */ @Query(value = "SELECT * FROM halo_post WHERE post_status=0 and post_type='post' AND YEAR(post_date)=:year AND MONTH(post_date)=:month ORDER BY post_date DESC", countQuery = "SELECT COUNT(*) FROM halo_post WHERE post_status=0 AND YEAR(post_date)=:year AND MONTH(post_date)=:month", nativeQuery = true) @@ -199,7 +189,6 @@ public interface PostRepository extends JpaRepository { * @param category category * @param status status * @param pageable pageable - * * @return Page */ Page findPostByCategoriesAndPostStatus(Category category, Integer status, Pageable pageable); @@ -210,7 +199,6 @@ public interface PostRepository extends JpaRepository { * @param tag tag * @param status status * @param pageable pageable - * * @return Page */ Page findPostsByTagsAndPostStatus(Tag tag, Integer status, Pageable pageable); @@ -219,27 +207,14 @@ public interface PostRepository extends JpaRepository { * 根据标签查询文章 * * @param tag tag - * * @return List */ List findPostsByTags(Tag tag); - /** - * 模糊查询文章 - * - * @param keyword 关键词 - * @param pageable 分页信息 - * - * @return Page - */ - @Query(value = "SELECT * FROM halo_post WHERE post_status = 0 AND post_type='post' AND post_title LIKE '%=:keyword%' OR post_content LIKE '%=:keyword%'", nativeQuery = true) - Page findPostByPostTitleLikeOrPostContentLikeAndPostTypeAndPostStatus(String keyword, Pageable pageable); - /** * 按热度从大到小排序 * * @param postStatus 文章状态 - * * @return List */ List findPostsByPostTypeOrderByPostViewsDesc(String postStatus); @@ -257,7 +232,6 @@ public interface PostRepository extends JpaRepository { * * @param status 文章状态 * @param postType 文章类型 - * * @return 文章数量 */ Integer countAllByPostStatusAndPostType(Integer status, String postType); @@ -266,7 +240,6 @@ public interface PostRepository extends JpaRepository { * 获取指定条数的文章 * * @param limit 条数 - * * @return List */ @Query(value = "SELECT * FROM halo_post WHERE post_status = 0 AND post_type = 'post' ORDER BY post_date DESC LIMIT :limit", nativeQuery = true) diff --git a/src/main/java/cc/ryanc/halo/service/PostService.java b/src/main/java/cc/ryanc/halo/service/PostService.java index 8fd600b61..2a5bd1131 100755 --- a/src/main/java/cc/ryanc/halo/service/PostService.java +++ b/src/main/java/cc/ryanc/halo/service/PostService.java @@ -26,7 +26,6 @@ public interface PostService { * 新增文章 * * @param post Post - * * @return Post */ Post save(Post post); @@ -35,7 +34,6 @@ public interface PostService { * 根据编号删除文章 * * @param postId postId - * * @return Post */ Post remove(Long postId); @@ -45,7 +43,6 @@ public interface PostService { * * @param postId postId * @param status status - * * @return Post */ Post updatePostStatus(Long postId, Integer status); @@ -61,7 +58,6 @@ public interface PostService { * 获取文章列表 不分页 * * @param postType post or page - * * @return List */ List findAll(String postType); @@ -69,12 +65,13 @@ public interface PostService { /** * 模糊查询文章 * - * @param keyWord keyword - * @param pageable pageable - * - * @return List + * @param keyword 关键词 + * @param postType 文章类型 + * @param postStatus 文章状态 + * @param pageable 分页信息 + * @return Page */ - List searchPosts(String keyWord, Pageable pageable); + Page searchPosts(String keyword, String postType, Integer postStatus, Pageable pageable); /** * 根据文章状态查询 分页,用于后台管理 @@ -82,7 +79,6 @@ public interface PostService { * @param status 0,1,2 * @param postType post or page * @param pageable 分页信息 - * * @return Page */ Page findPostByStatus(Integer status, String postType, Pageable pageable); @@ -91,7 +87,6 @@ public interface PostService { * 根据文章状态查询 分页,首页分页 * * @param pageable pageable - * * @return Page */ Page findPostByStatus(Pageable pageable); @@ -101,7 +96,6 @@ public interface PostService { * * @param status 0,1,2 * @param postType post or page - * * @return List */ List findPostByStatus(Integer status, String postType); @@ -110,7 +104,6 @@ public interface PostService { * 根据编号查询文章 * * @param postId postId - * * @return Post */ Optional findByPostId(Long postId); @@ -120,7 +113,6 @@ public interface PostService { * * @param postId postId * @param postType postType - * * @return Post */ Post findByPostId(Long postId, String postType); @@ -130,7 +122,6 @@ public interface PostService { * * @param postUrl 路径 * @param postType post or page - * * @return Post */ Post findByPostUrl(String postUrl, String postType); @@ -146,7 +137,6 @@ public interface PostService { * 获取下一篇文章 较新 * * @param postDate postDate - * * @return Post */ Post getNextPost(Date postDate); @@ -155,7 +145,6 @@ public interface PostService { * 获取下一篇文章 较老 * * @param postDate postDate - * * @return Post */ Post getPrePost(Date postDate); @@ -176,7 +165,6 @@ public interface PostService { /** * @return List - * * @Author Aquan * @Description 查询归档信息 查看所有文章 * @Date 2019.1.4 11:14 @@ -190,7 +178,6 @@ public interface PostService { * * @param year year * @param month month - * * @return List */ List findPostByYearAndMonth(String year, String month); @@ -201,7 +188,6 @@ public interface PostService { * @param year year * @param month month * @param pageable pageable - * * @return Page */ Page findPostByYearAndMonth(String year, String month, Pageable pageable); @@ -210,7 +196,6 @@ public interface PostService { * 根据年份查询文章 * * @param year year - * * @return List */ List findPostByYear(String year); @@ -220,7 +205,6 @@ public interface PostService { * * @param category category * @param pageable pageable - * * @return Page */ Page findPostByCategories(Category category, Pageable pageable); @@ -230,21 +214,10 @@ public interface PostService { * * @param tag tag * @param pageable pageable - * * @return Page */ Page findPostsByTags(Tag tag, Pageable pageable); - /** - * 搜索文章 - * - * @param keyword 关键词 - * @param pageable 分页信息 - * - * @return Page - */ - Page searchByKeywords(String keyword, Pageable pageable); - /** * 热门文章 * @@ -256,7 +229,6 @@ public interface PostService { * 当前文章的相似文章 * * @param post post - * * @return List */ List relatedPosts(Post post); @@ -272,7 +244,6 @@ public interface PostService { * 根据文章状态查询数量 * * @param status 文章状态 - * * @return 文章数量 */ Integer getCountByStatus(Integer status); @@ -281,7 +252,6 @@ public interface PostService { * 生成rss * * @param posts posts - * * @return String */ String buildRss(List posts); @@ -290,7 +260,6 @@ public interface PostService { * 生成sitemap * * @param posts posts - * * @return String */ String buildSiteMap(List posts); @@ -308,7 +277,6 @@ public interface PostService { * @param post post * @param cateList cateList * @param tagList tagList - * * @return Post Post */ Post buildCategoriesAndTags(Post post, List cateList, @RequestParam("tagList") String tagList); @@ -317,7 +285,6 @@ public interface PostService { * 获取最近的文章 * * @param limit 条数 - * * @return List */ List getRecentPosts(int limit); 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 2ebf9226d..41287659d 100755 --- a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java @@ -57,7 +57,6 @@ public class PostServiceImpl implements PostService { * 保存文章 * * @param post Post - * * @return Post */ @Override @@ -82,7 +81,6 @@ public class PostServiceImpl implements PostService { * 根据编号移除文章 * * @param postId postId - * * @return Post */ @Override @@ -98,7 +96,6 @@ public class PostServiceImpl implements PostService { * * @param postId postId * @param status status - * * @return Post */ @Override @@ -133,7 +130,6 @@ public class PostServiceImpl implements PostService { * 获取文章列表 不分页 * * @param postType post or page - * * @return List */ @Override @@ -145,14 +141,23 @@ public class PostServiceImpl implements PostService { /** * 模糊查询文章 * - * @param keyWord keyword - * @param pageable pageable - * - * @return List + * @param keyword 关键词 + * @param postType 文章类型 + * @param postStatus 文章状态 + * @param pageable 分页信息 + * @return Page */ @Override - public List searchPosts(String keyWord, Pageable pageable) { - return postRepository.findByPostTitleLike(keyWord, pageable); + public Page searchPosts(String keyword, String postType, Integer postStatus, Pageable pageable) { + return postRepository.findByPostTypeAndPostStatusAndPostTitleLikeOrPostTypeAndPostStatusAndPostContentLike( + postType, + postStatus, + "%" + keyword + "%", + postType, + postStatus, + "%" + keyword + "%", + pageable + ); } /** @@ -161,7 +166,6 @@ public class PostServiceImpl implements PostService { * @param status 0,1,2 * @param postType post or page * @param pageable 分页信息 - * * @return Page */ @Override @@ -173,7 +177,6 @@ public class PostServiceImpl implements PostService { * 根据文章状态查询 分页,首页分页 * * @param pageable pageable - * * @return Page */ @Override @@ -187,7 +190,6 @@ public class PostServiceImpl implements PostService { * * @param status 0,1,2 * @param postType post or page - * * @return List */ @Override @@ -200,7 +202,6 @@ public class PostServiceImpl implements PostService { * 根据编号查询文章 * * @param postId postId - * * @return Optional */ @Override @@ -212,7 +213,6 @@ public class PostServiceImpl implements PostService { * 根据编号和类型查询文章 * * @param postId postId - * * @return Post */ @Override @@ -225,7 +225,6 @@ public class PostServiceImpl implements PostService { * * @param postUrl 路径 * @param postType post or page - * * @return Post */ @Override @@ -249,7 +248,6 @@ public class PostServiceImpl implements PostService { * 获取下一篇文章 较新 * * @param postDate postDate - * * @return Post */ @Override @@ -261,7 +259,6 @@ public class PostServiceImpl implements PostService { * 获取下一篇文章 较老 * * @param postDate postDate - * * @return Post */ @Override @@ -314,7 +311,6 @@ public class PostServiceImpl implements PostService { /** * @return List - * * @Author Aquan * @Description 查询归档信息 返回所有文章 * @Date 2019.1.4 11:16 @@ -341,7 +337,6 @@ public class PostServiceImpl implements PostService { * * @param year year * @param month month - * * @return List */ @Override @@ -354,7 +349,6 @@ public class PostServiceImpl implements PostService { * 根据年份查询文章 * * @param year year - * * @return List */ @Override @@ -369,7 +363,6 @@ public class PostServiceImpl implements PostService { * @param year year year * @param month month month * @param pageable pageable pageable - * * @return Page */ @Override @@ -383,7 +376,6 @@ public class PostServiceImpl implements PostService { * @param category category * @param status status * @param pageable pageable - * * @return Page */ @Override @@ -398,7 +390,6 @@ public class PostServiceImpl implements PostService { * @param tag tag * @param status status * @param pageable pageable - * * @return Page */ @Override @@ -407,19 +398,6 @@ public class PostServiceImpl implements PostService { return postRepository.findPostsByTagsAndPostStatus(tag, PostStatusEnum.PUBLISHED.getCode(), pageable); } - /** - * 搜索文章 - * - * @param keyword 关键词 - * @param pageable 分页信息 - * - * @return Page - */ - @Override - public Page searchByKeywords(String keyword, Pageable pageable) { - return postRepository.findPostByPostTitleLikeOrPostContentLikeAndPostTypeAndPostStatus(keyword, pageable); - } - /** * 热门文章 * @@ -435,7 +413,6 @@ public class PostServiceImpl implements PostService { * 当前文章的相似文章 * * @param post post - * * @return List */ @Override @@ -473,7 +450,6 @@ public class PostServiceImpl implements PostService { * 根据文章状态查询数量 * * @param status 文章状态 - * * @return 文章数量 */ @Override @@ -485,7 +461,6 @@ public class PostServiceImpl implements PostService { * 生成rss * * @param posts posts - * * @return String */ @Override @@ -503,7 +478,6 @@ public class PostServiceImpl implements PostService { * 生成sitemap * * @param posts posts - * * @return String */ @Override @@ -531,7 +505,6 @@ public class PostServiceImpl implements PostService { * @param post post * @param cateList cateList * @param tagList tagList - * * @return Post Post */ @Override @@ -549,7 +522,6 @@ public class PostServiceImpl implements PostService { * 获取最近的文章 * * @param limit 条数 - * * @return List */ @Override 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 1ff411d6c..393f4c6ca 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 @@ -18,7 +18,6 @@ import cc.ryanc.halo.web.controller.core.BaseController; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; -import cn.hutool.http.HtmlUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; @@ -121,7 +120,7 @@ public class PostController extends BaseController { //排序规则 final Sort sort = new Sort(Sort.Direction.DESC, "postId"); final Pageable pageable = PageRequest.of(page, size, sort); - model.addAttribute("posts", postService.searchPosts(keyword, pageable)); + model.addAttribute("posts", postService.searchPosts(keyword,PostTypeEnum.POST_TYPE_POST.getDesc(),PostStatusEnum.PUBLISHED.getCode(),pageable)); } catch (Exception e) { log.error("未知错误:{}", e.getMessage()); } diff --git a/src/main/java/cc/ryanc/halo/web/controller/front/FrontIndexController.java b/src/main/java/cc/ryanc/halo/web/controller/front/FrontIndexController.java index d8d3bbaea..c7e6751cf 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/front/FrontIndexController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/front/FrontIndexController.java @@ -18,7 +18,6 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; /** *
@@ -45,7 +44,6 @@ public class FrontIndexController extends BaseController {
      */
     @GetMapping
     public String index(Model model) {
-        //调用方法渲染首页
         return this.index(model, 1);
     }
 
@@ -73,23 +71,9 @@ public class FrontIndexController extends BaseController {
             return this.renderNotFound();
         }
         final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);
-        model.addAttribute("is_index",true);
+        model.addAttribute("is_index", true);
         model.addAttribute("posts", posts);
         model.addAttribute("rainbow", rainbow);
         return this.render("index");
     }
-
-    /**
-     * 搜索文章
-     *
-     * @param keyword keyword
-     * @param model   model
-     * @return 模板路径/themes/{theme}/index
-     */
-    @GetMapping(value = "search")
-    public String search(@RequestParam("keyword") String keyword, Model model) {
-        final Page posts = postService.searchByKeywords(keyword, null);
-        model.addAttribute("posts", posts);
-        return this.render("index");
-    }
 }
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
new file mode 100644
index 000000000..17d02ab27
--- /dev/null
+++ b/src/main/java/cc/ryanc/halo/web/controller/front/FrontSearchController.java
@@ -0,0 +1,80 @@
+package cc.ryanc.halo.web.controller.front;
+
+import cc.ryanc.halo.model.domain.Post;
+import cc.ryanc.halo.model.dto.HaloConst;
+import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
+import cc.ryanc.halo.model.enums.PostStatusEnum;
+import cc.ryanc.halo.model.enums.PostTypeEnum;
+import cc.ryanc.halo.service.PostService;
+import cc.ryanc.halo.web.controller.core.BaseController;
+import cn.hutool.core.util.PageUtil;
+import cn.hutool.core.util.StrUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+/**
+ * 
+ *     文章检索
+ * 
+ * + * @author : RYAN0UP + * @date : 2019/1/11 + */ +@Slf4j +@Controller +@RequestMapping(value = "/search") +public class FrontSearchController extends BaseController { + + @Autowired + private PostService postService; + + /** + * 文章检索 + * + * @param model model + * @param keyword 关键词 + * @return 模板路径/themes/{theme}/search + */ + @GetMapping + public String search(Model model, + @RequestParam(value = "keyword") String keyword) { + return this.search(model, keyword, 1); + } + + /** + * 文章检索 分页 + * + * @param model model + * @param keyword 关键词 + * @param page 当前页码 + * @return 模板路径/themes/{theme}/search + */ + @GetMapping(value = "page/{page}") + public String search(Model model, + @RequestParam(value = "keyword") String keyword, + @PathVariable(value = "page") Integer page) { + final Sort sort = new Sort(Sort.Direction.DESC, "postDate"); + int size = 10; + if (StrUtil.isNotBlank(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp()))) { + size = Integer.parseInt(HaloConst.OPTIONS.get(BlogPropertiesEnum.INDEX_POSTS.getProp())); + } + final Pageable pageable = PageRequest.of(page - 1, size, sort); + final Page posts = postService.searchPosts(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); + model.addAttribute("posts", posts); + model.addAttribute("rainbow", rainbow); + return this.render("search"); + } +} diff --git a/src/main/resources/templates/themes/material/_partial/Paradox-search.ftl b/src/main/resources/templates/themes/material/_partial/Paradox-search.ftl index 11b4c458a..8e622d1f4 100644 --- a/src/main/resources/templates/themes/material/_partial/Paradox-search.ftl +++ b/src/main/resources/templates/themes/material/_partial/Paradox-search.ftl @@ -1,4 +1,3 @@ -