From f375c28733b72f871adf53e8738990cd39dd8ba3 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Sun, 8 Sep 2019 22:18:57 +0800 Subject: [PATCH] Add comments model for post and sheet. --- .../content/ContentArchiveController.java | 12 +++++++++- .../content/ContentSheetController.java | 23 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/run/halo/app/controller/content/ContentArchiveController.java b/src/main/java/run/halo/app/controller/content/ContentArchiveController.java index 708ec6e9c..030de33c1 100644 --- a/src/main/java/run/halo/app/controller/content/ContentArchiveController.java +++ b/src/main/java/run/halo/app/controller/content/ContentArchiveController.java @@ -18,6 +18,7 @@ import run.halo.app.model.entity.Category; import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Tag; import run.halo.app.model.enums.PostStatus; +import run.halo.app.model.vo.BaseCommentVO; import run.halo.app.model.vo.PostListVO; import run.halo.app.service.*; import run.halo.app.utils.MarkdownUtils; @@ -46,6 +47,8 @@ public class ContentArchiveController { private final PostTagService postTagService; + private final PostCommentService postCommentService; + private final OptionService optionService; private final StringCacheStore cacheStore; @@ -54,12 +57,14 @@ public class ContentArchiveController { ThemeService themeService, PostCategoryService postCategoryService, PostTagService postTagService, + PostCommentService postCommentService, OptionService optionService, StringCacheStore cacheStore) { this.postService = postService; this.themeService = themeService; this.postCategoryService = postCategoryService; this.postTagService = postTagService; + this.postCommentService = postCommentService; this.optionService = optionService; this.cacheStore = cacheStore; } @@ -112,6 +117,8 @@ public class ContentArchiveController { @RequestParam(value = "preview", required = false, defaultValue = "false") boolean preview, @RequestParam(value = "intimate", required = false, defaultValue = "false") boolean intimate, @RequestParam(value = "token", required = false) String token, + @RequestParam(value = "cp", defaultValue = "1") Integer cp, + @SortDefault(sort = "createTime", direction = DESC) Sort sort, Model model) { Post post; if (preview) { @@ -150,10 +157,13 @@ public class ContentArchiveController { List categories = postCategoryService.listCategoriesBy(post.getId()); List tags = postTagService.listTagsBy(post.getId()); + Page comments = postCommentService.pageVosBy(post.getId(), PageRequest.of(cp, optionService.getCommentPageSize(), sort)); + model.addAttribute("is_post", true); - model.addAttribute("post", post); + model.addAttribute("post", postService.convertToDetailVo(post)); model.addAttribute("categories", categories); model.addAttribute("tags", tags); + model.addAttribute("comments", comments); if (preview) { // refresh timeUnit diff --git a/src/main/java/run/halo/app/controller/content/ContentSheetController.java b/src/main/java/run/halo/app/controller/content/ContentSheetController.java index 707ffeee0..e60e53f2a 100644 --- a/src/main/java/run/halo/app/controller/content/ContentSheetController.java +++ b/src/main/java/run/halo/app/controller/content/ContentSheetController.java @@ -1,5 +1,9 @@ package run.halo.app.controller.content; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +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; @@ -10,12 +14,17 @@ import run.halo.app.exception.ForbiddenException; import run.halo.app.model.entity.Sheet; import run.halo.app.model.enums.PostStatus; import run.halo.app.model.support.HaloConst; +import run.halo.app.model.vo.BaseCommentVO; +import run.halo.app.service.OptionService; +import run.halo.app.service.SheetCommentService; import run.halo.app.service.SheetService; import run.halo.app.service.ThemeService; import run.halo.app.utils.MarkdownUtils; import java.util.concurrent.TimeUnit; +import static org.springframework.data.domain.Sort.Direction.DESC; + /** * Content sheet controller. * @@ -30,13 +39,21 @@ public class ContentSheetController { private final ThemeService themeService; + private final SheetCommentService sheetCommentService; + + private final OptionService optionService; + private final StringCacheStore cacheStore; public ContentSheetController(SheetService sheetService, ThemeService themeService, + SheetCommentService sheetCommentService, + OptionService optionService, StringCacheStore cacheStore) { this.sheetService = sheetService; this.themeService = themeService; + this.sheetCommentService = sheetCommentService; + this.optionService = optionService; this.cacheStore = cacheStore; } @@ -73,6 +90,8 @@ public class ContentSheetController { public String sheet(@PathVariable(value = "url") String url, @RequestParam(value = "preview", required = false, defaultValue = "false") boolean preview, @RequestParam(value = "token", required = false) String token, + @RequestParam(value = "cp", defaultValue = "1") Integer cp, + @SortDefault(sort = "createTime", direction = DESC) Sort sort, Model model) { Sheet sheet = sheetService.getBy(preview ? PostStatus.DRAFT : PostStatus.PUBLISHED, url); @@ -88,10 +107,14 @@ public class ContentSheetController { } } + Page comments = sheetCommentService.pageVosBy(sheet.getId(), PageRequest.of(cp, optionService.getCommentPageSize(), sort)); + + // sheet and post all can use model.addAttribute("sheet", sheetService.convertToDetail(sheet)); model.addAttribute("post", sheetService.convertToDetail(sheet)); model.addAttribute("is_sheet", true); + model.addAttribute("comments", comments); if (preview) { // refresh timeUnit