<aside> <img src="/icons/exclamation-mark_red.svg" alt="/icons/exclamation-mark_red.svg" width="40px" />
As-Is
//특정 게시글의 전체 댓글 삭제
public void deleteComments(String postId, String userId){
List<Comment> comments = commentRepository.findCommentAll(postId);
if(comments.isEmpty()){
throw new BaseException(NO_COMMENT_IN_POST);
}
for(Comment comment : comments) {
comment.softDelete(userId);
commentRepository.save(comment);
}
}
</aside>
<aside> ✅
To-Be
clearAutomatically = true 를 통해서 clear()를 해주었습니다.
flushAutomatically = true 를 통해서 flush() 도 가능합니다.@Modifying(clearAutomatically = true)
@Query("UPDATE Comment c set c.deletedBy = :userId, c.deletedAt = CURRENT_TIMESTAMP WHERE c.postId = :postId")
void softDeleteByPostId(@Param("postId") String postId, @Param("userId") String userId);
@Transactional
public boolean deleteComments(String postId, String userId){
List<Comment> comments = commentRepository.findCommentAll(postId);
//댓글이 없을 경우, 삭제할 댓글이 없는 경우 이므로, true 반환
if(comments.isEmpty()){
return true;
}
//댓글 삭제가 성공할 경우, true 반환. 실패하면, false 반환
try{
commentRepository.softDeleteByPostId(postId, userId); //코드 간결해짐
return true;
}catch(Exception e){
log.error("댓글 삭제를 실패했습니다. postId: {}, userId: {}", postId, userId, e);
return false;
}
}

</aside>
<aside> 🔥
개선점