Skip to content

Commit

Permalink
Add comments model for post and sheet.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Sep 8, 2019
1 parent b27123a commit f375c28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -46,6 +47,8 @@ public class ContentArchiveController {

private final PostTagService postTagService;

private final PostCommentService postCommentService;

private final OptionService optionService;

private final StringCacheStore cacheStore;
Expand All @@ -54,12 +57,14 @@ public ContentArchiveController(PostService postService,
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;
}
Expand Down Expand Up @@ -112,6 +117,8 @@ public String post(@PathVariable("url") String url,
@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) {
Expand Down Expand Up @@ -150,10 +157,13 @@ public String post(@PathVariable("url") String url,
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,12 +14,17 @@
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.
*
Expand All @@ -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;
}

Expand Down Expand Up @@ -73,6 +90,8 @@ public String links() {
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);

Expand All @@ -88,10 +107,14 @@ public String sheet(@PathVariable(value = "url") String url,
}
}

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
Expand Down

0 comments on commit f375c28

Please sign in to comment.