From 44d740b76088cf1256a66346ba77b5161e1a3e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=B6=E9=86=89?= <73400@163.com> Date: Fri, 29 Oct 2021 19:36:09 +0800 Subject: [PATCH] Fix number of comment in detail of post and sheet and add it into detail of journal (#1503) --- .../app/controller/content/api/JournalController.java | 2 +- .../app/repository/base/BaseCommentRepository.java | 9 +++++++++ src/main/java/run/halo/app/service/JournalService.java | 6 +++--- .../run/halo/app/service/base/BaseCommentService.java | 9 +++++++++ .../halo/app/service/impl/BaseCommentServiceImpl.java | 6 ++++++ .../run/halo/app/service/impl/JournalServiceImpl.java | 10 ++++++++-- .../run/halo/app/service/impl/PostServiceImpl.java | 3 ++- .../run/halo/app/service/impl/SheetServiceImpl.java | 3 ++- 8 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/main/java/run/halo/app/controller/content/api/JournalController.java b/src/main/java/run/halo/app/controller/content/api/JournalController.java index 2a1da53f..fdecedd2 100644 --- a/src/main/java/run/halo/app/controller/content/api/JournalController.java +++ b/src/main/java/run/halo/app/controller/content/api/JournalController.java @@ -71,7 +71,7 @@ public class JournalController { @GetMapping("{journalId:\\d+}") @ApiOperation("Gets a journal detail") - public JournalDTO getBy(@PathVariable("journalId") Integer journalId) { + public JournalWithCmtCountDTO getBy(@PathVariable("journalId") Integer journalId) { Journal journal = journalService.getById(journalId); return journalService.convertTo(journal); } 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 aeb42981..e283af62 100644 --- a/src/main/java/run/halo/app/repository/base/BaseCommentRepository.java +++ b/src/main/java/run/halo/app/repository/base/BaseCommentRepository.java @@ -99,6 +99,15 @@ public interface BaseCommentRepository */ long countByPostId(@NonNull Integer postId); + /** + * Count comments by comment status and post id. + * + * @param status status must not be null + * @param postId post id must not be null. + * @return comments count + */ + long countByStatusAndPostId(@NonNull CommentStatus status, @NonNull Integer postId); + /** * Counts by comment status. * diff --git a/src/main/java/run/halo/app/service/JournalService.java b/src/main/java/run/halo/app/service/JournalService.java index c8809457..9b7a561e 100644 --- a/src/main/java/run/halo/app/service/JournalService.java +++ b/src/main/java/run/halo/app/service/JournalService.java @@ -68,13 +68,13 @@ public interface JournalService extends CrudService { Page pageBy(@NonNull JournalType type, @NonNull Pageable pageable); /** - * Converts to journal dto. + * Converts to journal with comment count dto. * * @param journal journal must not be null - * @return journal dto + * @return journal with comment count dto */ @NonNull - JournalDTO convertTo(@NonNull Journal journal); + JournalWithCmtCountDTO convertTo(@NonNull Journal journal); /** * Converts to journal with comment count dto list. 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 0aab6c6c..275df663 100644 --- a/src/main/java/run/halo/app/service/base/BaseCommentService.java +++ b/src/main/java/run/halo/app/service/base/BaseCommentService.java @@ -144,6 +144,15 @@ public interface BaseCommentService */ long countByPostId(@NonNull Integer postId); + /** + * Count comments by comment status and post id. + * + * @param status status must not be null. + * @param postId post id must not be null. + * @return comments count + */ + long countByStatusAndPostId(@NonNull CommentStatus status, @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 1ab2ec54..39e47c6b 100644 --- a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java @@ -290,6 +290,12 @@ public abstract class BaseCommentServiceImpl return baseCommentRepository.countByPostId(postId); } + @Override + public long countByStatusAndPostId(@NonNull CommentStatus status, @NonNull Integer postId) { + Assert.notNull(postId, "Post id must not be null"); + return baseCommentRepository.countByStatusAndPostId(status, postId); + } + @Override public long countByStatus(@NonNull CommentStatus status) { return baseCommentRepository.countByStatus(status); diff --git a/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java b/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java index 4316cc67..392ba208 100644 --- a/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java @@ -107,10 +107,16 @@ public class JournalServiceImpl extends AbstractCrudService } @Override - public JournalDTO convertTo(Journal journal) { + public JournalWithCmtCountDTO convertTo(Journal journal) { Assert.notNull(journal, "Journal must not be null"); - return new JournalDTO().convertFrom(journal); + JournalWithCmtCountDTO journalWithCmtCountDto = new JournalWithCmtCountDTO() + .convertFrom(journal); + + journalWithCmtCountDto.setCommentCount(journalCommentService.countByStatusAndPostId( + CommentStatus.PUBLISHED, journal.getId())); + + return journalWithCmtCountDto; } @Override 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 f432f0b5..8b67f6f0 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -787,7 +787,8 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe postDetailVO.setMetaIds(metaIds); postDetailVO.setMetas(postMetaService.convertTo(postMetaList)); - postDetailVO.setCommentCount(postCommentService.countByPostId(post.getId())); + postDetailVO.setCommentCount(postCommentService.countByStatusAndPostId( + CommentStatus.PUBLISHED, post.getId())); postDetailVO.setFullPath(buildFullPath(post)); 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 0878d7d5..c41b3243 100644 --- a/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/SheetServiceImpl.java @@ -341,7 +341,8 @@ public class SheetServiceImpl extends BasePostServiceImpl implements Shee sheetDetailVO.setSummary(generateSummary(sheet.getFormatContent())); } - sheetDetailVO.setCommentCount(sheetCommentService.countByPostId(sheet.getId())); + sheetDetailVO.setCommentCount(sheetCommentService.countByStatusAndPostId( + CommentStatus.PUBLISHED, sheet.getId())); sheetDetailVO.setFullPath(buildFullPath(sheet)); -- GitLab