Add comments model for post and sheet.

pull/296/head
ruibaby 2019-09-08 22:18:57 +08:00
parent b27123adf5
commit f375c28733
2 changed files with 34 additions and 1 deletions

View File

@ -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<Category> categories = postCategoryService.listCategoriesBy(post.getId());
List<Tag> tags = postTagService.listTagsBy(post.getId());
Page<BaseCommentVO> 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

View File

@ -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<BaseCommentVO> 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