From d3ef9c4a7a909c281c3a15a574e7211b758afc9b Mon Sep 17 00:00:00 2001 From: ruibaby Date: Wed, 1 Jan 2020 22:42:50 +0800 Subject: [PATCH] pref: remove useless code. --- .../content/ContentArchiveController.java | 14 ++++---------- .../content/ContentJournalController.java | 7 +------ .../content/ContentSheetController.java | 15 ++++----------- .../app/model/dto/post/BasePostDetailDTO.java | 2 ++ .../repository/base/BaseCommentRepository.java | 8 ++++++++ .../halo/app/service/base/BaseCommentService.java | 8 ++++++++ .../app/service/impl/BaseCommentServiceImpl.java | 6 ++++++ .../halo/app/service/impl/PostServiceImpl.java | 2 ++ .../halo/app/service/impl/SheetServiceImpl.java | 2 ++ 9 files changed, 37 insertions(+), 27 deletions(-) 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 1abbef877..d7ae1bc41 100644 --- a/src/main/java/run/halo/app/controller/content/ContentArchiveController.java +++ b/src/main/java/run/halo/app/controller/content/ContentArchiveController.java @@ -21,7 +21,6 @@ import run.halo.app.model.entity.PostMeta; import run.halo.app.model.entity.Tag; 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.model.vo.PostListVO; import run.halo.app.service.*; import run.halo.app.utils.MarkdownUtils; @@ -114,17 +113,14 @@ public class ContentArchiveController { /** * Render post page. * - * @param url post slug url. - * @param preview preview - * @param token preview token - * @param model model + * @param url post slug url. + * @param token view token. + * @param model model * @return template path: themes/{theme}/post.ftl */ @GetMapping("{url}") public String post(@PathVariable("url") String url, @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 = postService.getByUrl(url); @@ -151,14 +147,12 @@ public class ContentArchiveController { List tags = postTagService.listTagsBy(post.getId()); List metas = postMetaService.listBy(post.getId()); - Page comments = postCommentService.pageVosBy(post.getId(), PageRequest.of(cp, optionService.getCommentPageSize(), sort)); - model.addAttribute("is_post", true); model.addAttribute("post", postService.convertToDetailVo(post)); model.addAttribute("categories", categories); model.addAttribute("tags", tags); model.addAttribute("metas", postMetaService.convertToMap(metas)); - model.addAttribute("comments", comments); + model.addAttribute("comments", Page.empty()); if (themeService.templateExists(ThemeService.CUSTOM_POST_PREFIX + post.getTemplate() + HaloConst.SUFFIX_FTL)) { return themeService.render(ThemeService.CUSTOM_POST_PREFIX + post.getTemplate()); diff --git a/src/main/java/run/halo/app/controller/content/ContentJournalController.java b/src/main/java/run/halo/app/controller/content/ContentJournalController.java index 839d3b4ae..955d36608 100644 --- a/src/main/java/run/halo/app/controller/content/ContentJournalController.java +++ b/src/main/java/run/halo/app/controller/content/ContentJournalController.java @@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import run.halo.app.model.entity.Journal; import run.halo.app.model.enums.JournalType; -import run.halo.app.service.JournalCommentService; import run.halo.app.service.JournalService; import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; @@ -34,18 +33,14 @@ public class ContentJournalController { private final JournalService journalService; - private final JournalCommentService journalCommentService; - private final OptionService optionService; private final ThemeService themeService; public ContentJournalController(JournalService journalService, - JournalCommentService journalCommentService, OptionService optionService, ThemeService themeService) { this.journalService = journalService; - this.journalCommentService = journalCommentService; this.optionService = optionService; this.themeService = themeService; } @@ -84,7 +79,7 @@ public class ContentJournalController { int[] rainbow = PageUtil.rainbow(page, journals.getTotalPages(), 3); model.addAttribute("is_journal", true); - model.addAttribute("journals", journals); + model.addAttribute("journals", journalService.convertToCmtCountDto(journals)); model.addAttribute("rainbow", rainbow); return themeService.render("journals"); } 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 5935dabce..7de2a93dc 100644 --- a/src/main/java/run/halo/app/controller/content/ContentSheetController.java +++ b/src/main/java/run/halo/app/controller/content/ContentSheetController.java @@ -5,7 +5,6 @@ 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; @@ -17,7 +16,6 @@ import run.halo.app.model.dto.PhotoDTO; 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.model.vo.SheetDetailVO; import run.halo.app.service.*; import run.halo.app.utils.MarkdownUtils; @@ -102,17 +100,14 @@ public class ContentSheetController { /** * Render custom sheet * - * @param url sheet url - * @param preview preview - * @param token token - * @param model model + * @param url sheet url + * @param token view token + * @param model model * @return template path: themes/{theme}/sheet.ftl */ @GetMapping(value = "/s/{url}") public String sheet(@PathVariable(value = "url") String url, @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.getByUrl(url); @@ -131,15 +126,13 @@ public class ContentSheetController { } } - Page comments = sheetCommentService.pageVosBy(sheet.getId(), PageRequest.of(cp, optionService.getCommentPageSize(), sort)); - SheetDetailVO sheetDetailVO = sheetService.convertToDetailVo(sheet); // sheet and post all can use model.addAttribute("sheet", sheetDetailVO); model.addAttribute("post", sheetDetailVO); model.addAttribute("is_sheet", true); - model.addAttribute("comments", comments); + model.addAttribute("comments", Page.empty()); if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate()); diff --git a/src/main/java/run/halo/app/model/dto/post/BasePostDetailDTO.java b/src/main/java/run/halo/app/model/dto/post/BasePostDetailDTO.java index ceb7dc1f0..90cffa9e7 100644 --- a/src/main/java/run/halo/app/model/dto/post/BasePostDetailDTO.java +++ b/src/main/java/run/halo/app/model/dto/post/BasePostDetailDTO.java @@ -17,4 +17,6 @@ public class BasePostDetailDTO extends BasePostSimpleDTO { private String originalContent; private String formatContent; + + private Long commentCount; } diff --git a/src/main/java/run/halo/app/repository/base/BaseCommentRepository.java b/src/main/java/run/halo/app/repository/base/BaseCommentRepository.java index 0cbb97f10..5b31c52ff 100644 --- a/src/main/java/run/halo/app/repository/base/BaseCommentRepository.java +++ b/src/main/java/run/halo/app/repository/base/BaseCommentRepository.java @@ -67,6 +67,14 @@ public interface BaseCommentRepository extends Base @NonNull List countByPostIds(@NonNull Collection postIds); + /** + * Count comments by post id. + * + * @param postId post id must not be null. + * @return comments count + */ + long countByPostId(@NonNull Integer postId); + /** * Counts by comment status. * diff --git a/src/main/java/run/halo/app/service/base/BaseCommentService.java b/src/main/java/run/halo/app/service/base/BaseCommentService.java index a3500a77e..890304c10 100644 --- a/src/main/java/run/halo/app/service/base/BaseCommentService.java +++ b/src/main/java/run/halo/app/service/base/BaseCommentService.java @@ -134,6 +134,14 @@ public interface BaseCommentService extends CrudSer @NonNull Map countByPostIds(@Nullable Collection postIds); + /** + * Count comments by post id. + * + * @param postId post id must not be null. + * @return comments count + */ + long countByPostId(@NonNull Integer postId); + /** * Counts by comment status. * diff --git a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java index 6bb5ee7ff..05e86334a 100644 --- a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java @@ -233,6 +233,12 @@ public abstract class BaseCommentServiceImpl extend return ServiceUtils.convertToMap(commentCountProjections, CommentCountProjection::getPostId, CommentCountProjection::getCount); } + @Override + public long countByPostId(Integer postId) { + Assert.notNull(postId, "Post id must not be null"); + return baseCommentRepository.countByPostId(postId); + } + @Override public long countByStatus(CommentStatus status) { return baseCommentRepository.countByStatus(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 ce753fd34..1392f47c2 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -549,6 +549,8 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe postDetailVO.setPostMetaIds(postMetaIds); postDetailVO.setPostMetas(postMetaService.convertTo(postMetaList)); + postDetailVO.setCommentCount(postCommentService.countByPostId(post.getId())); + return postDetailVO; } diff --git a/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java b/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java index 9d60bcff6..6dd7fd9c2 100644 --- a/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java @@ -289,6 +289,8 @@ public class SheetServiceImpl extends BasePostServiceImpl implements Shee if (StringUtils.isBlank(sheetDetailVO.getSummary())) { sheetDetailVO.setSummary(generateSummary(sheet.getFormatContent())); } + + sheetDetailVO.setCommentCount(sheetCommentService.countByPostId(sheet.getId())); return sheetDetailVO; }