diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java index 1be9c9f..098f6bd 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java @@ -88,4 +88,14 @@ public void incrementLikeCount(Long postId){ public void decrementLikeCount(Long postId){ postJpaRepository.decrementLikeCount(postId); } + + @Override + public void incrementCommentCount(Long postId) { + postJpaRepository.incrementCommentCount(postId); + } + + @Override + public void decrementCommentCount(Long postId) { + postJpaRepository.decrementCommentCount(postId); + } } \ No newline at end of file diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java index ceaad64..2208c5a 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java @@ -50,4 +50,12 @@ Page findAllByHashtagIdAndVisibilityPublic( @Modifying @Query("UPDATE Post p SET p.likeCount = p.likeCount - 1 WHERE p.postId = :postId AND p.likeCount > 0") void decrementLikeCount(@Param("postId") Long postId); + + @Modifying + @Query("UPDATE Post p SET p.commentCount = p.commentCount + 1 WHERE p.postId = :postId") + void incrementCommentCount(@Param("postId") Long postId); + + @Modifying + @Query("UPDATE Post p SET p.commentCount = p.commentCount - 1 WHERE p.postId = :postId AND p.commentCount > 0") + void decrementCommentCount(@Param("postId") Long postId); } \ No newline at end of file diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java index cbb7a0f..217b93f 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java @@ -32,4 +32,7 @@ public interface PostRepository { void incrementLikeCount(Long postId); void decrementLikeCount(Long postId); + + void incrementCommentCount(Long postId); + void decrementCommentCount(Long postId); } \ No newline at end of file diff --git a/prothsync/src/main/java/com/prothsync/prothsync/service/AdminService.java b/prothsync/src/main/java/com/prothsync/prothsync/service/AdminService.java index d812d5a..1e650db 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/service/AdminService.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/service/AdminService.java @@ -169,11 +169,7 @@ public void deleteComment(Long commentId, Long adminUserId) { Comment comment = commentRepository.findById(commentId) .orElseThrow(() -> new BusinessException(PostErrorCode.COMMENT_NOT_FOUND)); - Post post = postRepository.findById(comment.getPostId()) - .orElseThrow(() -> new BusinessException(PostErrorCode.POST_NOT_FOUND)); - - post.decrementCommentCount(); - postRepository.save(post); + postRepository.decrementCommentCount(comment.getPostId()); commentRepository.softDelete(comment, adminUserId); } diff --git a/prothsync/src/main/java/com/prothsync/prothsync/service/CommentService.java b/prothsync/src/main/java/com/prothsync/prothsync/service/CommentService.java index 9f2e024..bdbdbd8 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/service/CommentService.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/service/CommentService.java @@ -37,8 +37,7 @@ public CommentResponseDTO createComment(Long postId, Long userId, CommentCreateR Comment comment = Comment.createComment(postId, userId, request.content()); Comment savedComment = commentRepository.save(comment); - post.incrementCommentCount(); - postRepository.save(post); + postRepository.incrementCommentCount(postId); notificationService.send( NotificationType.COMMENT, userId, post.getUserId(), @@ -76,9 +75,7 @@ public void deleteComment(Long commentId, Long userId) { Comment comment = findCommentOrThrow(commentId); validateCommentOwner(comment, userId); - Post post = findPostOrThrow(comment.getPostId()); - post.decrementCommentCount(); - postRepository.save(post); + postRepository.decrementCommentCount(comment.getPostId()); commentRepository.softDelete(comment, userId); }