diff --git a/src/main/java/run/halo/app/service/PostService.java b/src/main/java/run/halo/app/service/PostService.java index beafc18fc..fbdbc0428 100755 --- a/src/main/java/run/halo/app/service/PostService.java +++ b/src/main/java/run/halo/app/service/PostService.java @@ -77,6 +77,15 @@ public interface PostService extends CrudService { @NonNull Page pageBy(@NonNull PostQuery postQuery, @NonNull Pageable pageable); + /** + * Pages post by keyword + * + * @param keyword keyword + * @param pageable pageable + * @return a page of post + */ + @NonNull + Page pageBy(@NonNull String keyword, @NonNull Pageable pageable); /** * Lists simple output dto by status. diff --git a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java index 852478e2f..de5d6bc66 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -117,6 +117,18 @@ public class PostServiceImpl extends AbstractCrudService implemen return postRepository.findAll(buildSpecByQuery(postQuery), pageable); } + @Override + public Page pageBy(String keyword, Pageable pageable) { + Assert.notNull(keyword, "keyword must not be null"); + Assert.notNull(pageable, "Page info must not be null"); + + PostQuery postQuery = new PostQuery(); + postQuery.setKeyword(keyword); + + // Build specification and find all + return postRepository.findAll(buildSpecByQuery(postQuery), pageable); + } + /** * Build specification by post query. * diff --git a/src/main/java/run/halo/app/web/controller/content/ContentSearchController.java b/src/main/java/run/halo/app/web/controller/content/ContentSearchController.java new file mode 100644 index 000000000..6c5b7a444 --- /dev/null +++ b/src/main/java/run/halo/app/web/controller/content/ContentSearchController.java @@ -0,0 +1,83 @@ +package run.halo.app.web.controller.content; + +import cn.hutool.core.util.PageUtil; +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.data.web.SortDefault; +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; +import org.springframework.web.util.HtmlUtils; +import run.halo.app.model.entity.Post; +import run.halo.app.model.vo.PostListVO; +import run.halo.app.service.OptionService; +import run.halo.app.service.PostService; +import run.halo.app.service.ThemeService; + +import static org.springframework.data.domain.Sort.Direction.DESC; + +/** + * Search Controller + * + * @author : RYAN0UP + * @date : 2019-04-21 + */ +@Controller +@RequestMapping(value = "/search") +public class ContentSearchController { + + private final PostService postService; + + private final OptionService optionService; + + private final ThemeService themeService; + + public ContentSearchController(PostService postService, OptionService optionService, ThemeService themeService) { + this.postService = postService; + this.optionService = optionService; + this.themeService = themeService; + } + + /** + * Render post search page. + * + * @param model model + * @param keyword keyword + * @return template path : themes/{theme}/search + */ + @GetMapping + public String search(Model model, + @RequestParam(value = "keyword") String keyword) { + return this.search(model, HtmlUtils.htmlEscape(keyword), 1, Sort.by(DESC, "createTime")); + } + + /** + * Render post search page. + * + * @param model model + * @param keyword keyword + * @return template path :themes/{theme}/search + */ + @GetMapping(value = "page/{page}") + public String search(Model model, + @RequestParam(value = "keyword") String keyword, + @PathVariable(value = "page") Integer page, + @SortDefault(sort = "createTime", direction = DESC) Sort sort) { + final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), sort); + final Page posts = postService.pageBy(keyword, pageable); + + final Page postPage = postService.convertToListVo(posts); + + final int[] rainbow = PageUtil.rainbow(page, postPage.getTotalPages(), 3); + model.addAttribute("is_search", true); + model.addAttribute("keyword", keyword); + model.addAttribute("posts", postPage); + model.addAttribute("rainbow", rainbow); + return themeService.render("search"); + } +} diff --git a/src/main/resources/templates/themes/anatole/theme.properties b/src/main/resources/templates/themes/anatole/theme.properties deleted file mode 100644 index a7538696b..000000000 --- a/src/main/resources/templates/themes/anatole/theme.properties +++ /dev/null @@ -1,8 +0,0 @@ -theme.id=caicai_anatole -theme.name=Anatole -theme.website=https://github.com/hi-caicai/farbox-theme-Anatole -theme.description=A other farbox theme -theme.logo=https://ryanc.cc/anatole/source/images/logo@2x.png -theme.version=1.0 -theme.author=Caicai -theme.author.website=https://www.caicai.me/ \ No newline at end of file