Skip to content
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 @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ Page<Post> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ public interface PostRepository {

void incrementLikeCount(Long postId);
void decrementLikeCount(Long postId);

void incrementCommentCount(Long postId);
void decrementCommentCount(Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
}
Expand Down
Loading