diff --git a/module-domain/src/main/java/com/mile/comment/service/CommentService.java b/module-domain/src/main/java/com/mile/comment/service/CommentService.java index 7d089beb..f7e960a9 100644 --- a/module-domain/src/main/java/com/mile/comment/service/CommentService.java +++ b/module-domain/src/main/java/com/mile/comment/service/CommentService.java @@ -20,6 +20,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @Service @@ -142,10 +143,15 @@ private List findByPostId( return commentRepository.findByPostId(postId); } - public int findCommentCountByPost( + + private int findCommentReplyByPost( final Post post ) { - return findByPostId(post.getId()).size(); + AtomicInteger result = new AtomicInteger(); + findByPostId(post.getId()).iterator().forEachRemaining( + c -> result.addAndGet(commentReplyService.findRepliesCountByComment(c)) + ); + return result.intValue(); } @@ -165,6 +171,6 @@ public void deleteAllCommentByWriterNameId( public int countByPost( final Post post ) { - return commentRepository.countByPost(post); + return commentRepository.countByPost(post) + findCommentReplyByPost(post); } } diff --git a/module-domain/src/main/java/com/mile/commentreply/service/CommentReplyService.java b/module-domain/src/main/java/com/mile/commentreply/service/CommentReplyService.java index 74afe1d9..95a3422d 100644 --- a/module-domain/src/main/java/com/mile/commentreply/service/CommentReplyService.java +++ b/module-domain/src/main/java/com/mile/commentreply/service/CommentReplyService.java @@ -48,7 +48,7 @@ private void authenticateReplyWithUserId( final Long userId, final CommentReply commentReply ) { - if(!commentReply.getWriterName().getWriter().getId().equals(userId)) { + if (!commentReply.getWriterName().getWriter().getId().equals(userId)) { throw new UnauthorizedException(ErrorMessage.REPLY_USER_FORBIDDEN); } } @@ -58,6 +58,7 @@ public void deleteRepliesByComment( ) { commentReplyRepository.deleteAll(commentReplyRepository.findByComment(comment)); } + public List findRepliesByComment( final Comment comment, final Long writerNameId @@ -65,6 +66,12 @@ public List findRepliesByComment( return commentReplyRepository.findByComment(comment).stream().map(c -> ReplyResponse.of(c, writerNameId, isWriterOfPost(c))).collect(Collectors.toList()); } + public int findRepliesCountByComment( + final Comment comment + ) { + return commentReplyRepository.findByComment(comment).size(); + } + private boolean isWriterOfPost(final CommentReply commentReply) { return commentReply.getComment().getPost().getWriterName().getId().equals(commentReply.getWriterName().getId()); } diff --git a/module-domain/src/main/java/com/mile/topic/service/TopicService.java b/module-domain/src/main/java/com/mile/topic/service/TopicService.java index 95985f2e..bbe4e236 100644 --- a/module-domain/src/main/java/com/mile/topic/service/TopicService.java +++ b/module-domain/src/main/java/com/mile/topic/service/TopicService.java @@ -152,7 +152,7 @@ public PostListInTopicResponse getPostListByTopic( Slice posts = postGetService.findByTopicAndLastPostId(topic, secureUrlUtil.decodeIfNotNull(lastPostId)); return PostListInTopicResponse.of(TopicOfMoimResponse.of(topic), posts.stream().sorted(Comparator.comparing(BaseTimeEntity::getCreatedAt).reversed()) - .map(p -> PostListResponse.of(p, commentService.findCommentCountByPost(p))).toList(), + .map(p -> PostListResponse.of(p, commentService.countByPost(p))).toList(), posts.hasNext() ); }