Skip to content

Commit

Permalink
Fix: 삭제로직 재수정 (#185)
Browse files Browse the repository at this point in the history
* fix: jpa type 에러, 엔티티 lazy 조회 이슈 해결

* fix: 원 댓글조회 쿼리 변경, 투표 총댓글 수 감소 로직변경
  • Loading branch information
kimJH47 authored Feb 25, 2024
1 parent 59f7705 commit 56c441f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.salmalteam.salmal.vote.dto.request.VoteCommentUpdateRequest;
import com.salmalteam.salmal.vote.entity.Vote;
import com.salmalteam.salmal.vote.entity.VoteRepository;
import com.salmalteam.salmal.vote.exception.VoteException;
import com.salmalteam.salmal.vote.exception.VoteExceptionType;

import lombok.RequiredArgsConstructor;

Expand All @@ -50,20 +52,30 @@ public void save(final String content, final Vote vote, final Member member) {

@Transactional
public void deleteComment(final Long memberId, final Long commentId) {
final Comment comment = getCommentById(commentId);

final Comment comment = commentRepository.findByIdFetchJoinCommenter(commentId)
.orElseThrow(() -> new CommentException(CommentExceptionType.NOT_FOUND));

validateDeleteAuthority(comment.getCommenter().getId(), memberId);

if (comment.isComment()) {
long deleteComment = commentRepository.countByParentComment(comment) + 1;
Vote vote = comment.getVote();
Vote vote = voteRepository.findById(comment.getVote().getId())
.orElseThrow(() -> new VoteException(VoteExceptionType.NOT_FOUND));

vote.decreaseCommentCount(Math.toIntExact(deleteComment));
commentRepository.deleteAllRepliesByParentCommentId(commentId);
commentRepository.delete(comment);
return;
}

Comment parentComment = comment.getParentComment();
commentRepository.decreaseReplyCount(commentId);
voteRepository.decreaseCommentCount(parentComment.getVote().getId());
Comment parentComment = commentRepository.findByIdFetchJoinVote(comment.getParentComment().getId())
.orElseThrow(() -> new CommentException(CommentExceptionType.NOT_FOUND));

parentComment.decreaseReplyCount(1);
Vote vote = parentComment.getVote();
vote.decreaseCommentCount(1);

commentRepository.delete(comment);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ public interface CommentRepository extends JpaRepository<Comment, Long>, Comment
void increaseReplyCount(Long id);

Long countByParentComment(Comment comment);

@Query("select c from Comment c join fetch c.vote where c.id=:id")
Optional<Comment> findByIdFetchJoinVote(@Param("id") Long id);
}

0 comments on commit 56c441f

Please sign in to comment.