Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#377 [feat] 대댓글 count 반영 #379

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -142,10 +143,15 @@ private List<Comment> 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();
}


Expand All @@ -165,6 +171,6 @@ public void deleteAllCommentByWriterNameId(
public int countByPost(
final Post post
) {
return commentRepository.countByPost(post);
return commentRepository.countByPost(post) + findCommentReplyByPost(post);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -58,13 +58,20 @@ public void deleteRepliesByComment(
) {
commentReplyRepository.deleteAll(commentReplyRepository.findByComment(comment));
}

public List<ReplyResponse> findRepliesByComment(
final Comment comment,
final Long writerNameId
) {
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public PostListInTopicResponse getPostListByTopic(
Slice<Post> 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()
);
}
Expand Down
Loading