From 4b627d7600c995964fc2bdd87d7f4291d01eacd6 Mon Sep 17 00:00:00 2001 From: kdkdhoho Date: Fri, 11 Oct 2024 23:13:36 +0900 Subject: [PATCH] =?UTF-8?q?=20feat:=20=EB=A9=98=EC=85=98=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20(#308)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/service/CommentService.java | 7 +++-- .../application/service/ReplyService.java | 8 +++-- .../listywave/mention/MentionRepository.java | 4 +++ .../listywave/mention/MentionServiceTest.java | 31 +++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/listywave/list/application/service/CommentService.java b/src/main/java/com/listywave/list/application/service/CommentService.java index abf31e83..038cddba 100644 --- a/src/main/java/com/listywave/list/application/service/CommentService.java +++ b/src/main/java/com/listywave/list/application/service/CommentService.java @@ -16,6 +16,7 @@ import com.listywave.list.repository.list.ListRepository; import com.listywave.list.repository.reply.ReplyRepository; import com.listywave.mention.Mention; +import com.listywave.mention.MentionRepository; import com.listywave.user.application.domain.User; import com.listywave.user.repository.user.UserRepository; import jakarta.transaction.Transactional; @@ -34,6 +35,7 @@ public class CommentService { private final ListRepository listRepository; private final UserRepository userRepository; private final ReplyRepository replyRepository; + private final MentionRepository mentionRepository; private final CommentRepository commentRepository; private final ApplicationEventPublisher applicationEventPublisher; @@ -81,9 +83,9 @@ public CommentFindResponse findCommentBy(Long listId, int size, Long cursorId) { return CommentFindResponse.from(totalCount, newCursorId, hasNext, result); } - public void delete(Long listId, Long commentId, Long loginUserId) { + public void delete(Long listId, Long commentId, Long userId) { listRepository.getById(listId); - User user = userRepository.getById(loginUserId); + User user = userRepository.getById(userId); Comment comment = commentRepository.getById(commentId); if (!comment.isOwner(user)) { @@ -95,6 +97,7 @@ public void delete(Long listId, Long commentId, Long loginUserId) { return; } commentRepository.delete(comment); + mentionRepository.deleteAllByComment(comment); } public void update(Long listId, Long writerId, Long commentId, String content, List mentionIds) { diff --git a/src/main/java/com/listywave/list/application/service/ReplyService.java b/src/main/java/com/listywave/list/application/service/ReplyService.java index cc6c3e78..d3b53443 100644 --- a/src/main/java/com/listywave/list/application/service/ReplyService.java +++ b/src/main/java/com/listywave/list/application/service/ReplyService.java @@ -13,6 +13,7 @@ import com.listywave.list.repository.list.ListRepository; import com.listywave.list.repository.reply.ReplyRepository; import com.listywave.mention.Mention; +import com.listywave.mention.MentionRepository; import com.listywave.user.application.domain.User; import com.listywave.user.repository.user.UserRepository; import java.util.List; @@ -29,6 +30,7 @@ public class ReplyService { private final ListRepository listRepository; private final UserRepository userRepository; private final ReplyRepository replyRepository; + private final MentionRepository mentionRepository; private final CommentRepository commentRepository; private final ApplicationEventPublisher applicationEventPublisher; @@ -58,9 +60,9 @@ private List toMentions(List mentionedIds) { }).toList(); } - public void delete(ReplyDeleteCommand command, Long loginUserId) { + public void delete(ReplyDeleteCommand command, Long userId) { listRepository.getById(command.listId()); - User user = userRepository.getById(loginUserId); + User user = userRepository.getById(userId); Comment comment = commentRepository.getById(command.commentId()); Reply reply = replyRepository.getById(command.replyId()); @@ -69,8 +71,10 @@ public void delete(ReplyDeleteCommand command, Long loginUserId) { } replyRepository.deleteById(command.replyId()); + mentionRepository.deleteAllByReply(reply); if (!replyRepository.existsByComment(comment) && comment.isDeleted()) { commentRepository.delete(comment); + mentionRepository.deleteAllByComment(comment); } } diff --git a/src/main/java/com/listywave/mention/MentionRepository.java b/src/main/java/com/listywave/mention/MentionRepository.java index 31ce706f..2d37c332 100644 --- a/src/main/java/com/listywave/mention/MentionRepository.java +++ b/src/main/java/com/listywave/mention/MentionRepository.java @@ -10,4 +10,8 @@ public interface MentionRepository extends JpaRepository { List findAllByComment(Comment comment); List findAllByReply(Reply reply); + + void deleteAllByComment(Comment comment); + + void deleteAllByReply(Reply reply); } diff --git a/src/test/java/com/listywave/mention/MentionServiceTest.java b/src/test/java/com/listywave/mention/MentionServiceTest.java index 8699dd67..07c6adf6 100644 --- a/src/test/java/com/listywave/mention/MentionServiceTest.java +++ b/src/test/java/com/listywave/mention/MentionServiceTest.java @@ -7,6 +7,7 @@ import com.listywave.common.IntegrationTest; import com.listywave.list.application.domain.comment.Comment; import com.listywave.list.application.domain.reply.Reply; +import com.listywave.list.application.dto.ReplyDeleteCommand; import com.listywave.list.application.dto.ReplyUpdateCommand; import com.listywave.list.application.dto.response.CommentFindResponse; import com.listywave.list.application.dto.response.CommentFindResponse.CommentDto; @@ -251,4 +252,34 @@ class 멘션_수정 { assertThat(result).isEmpty(); } } + + @Nested + class 멘션_삭제 { + + @Test + void 멘션을_한_댓글_삭제_시_함께_삭제한다() { + // given + Long commentId = commentService.create(list.getId(), dh.getId(), "댓글이요", List.of(js.getId())).id(); + + // when + commentService.delete(list.getId(), commentId, dh.getId()); + + // then + assertThat(mentionRepository.findAll()).isEmpty(); + } + + @Test + void 멘션을_한_답글_삭제_시_함께_삭제한다() { + // given + Long commentId = commentService.create(list.getId(), dh.getId(), "댓글이요", EMPTY_LIST).id(); + Long replyId = replyService.create(list.getId(), commentId, js.getId(), "답글이요", List.of(dh.getId())).id(); + + // when + ReplyDeleteCommand command = new ReplyDeleteCommand(list.getId(), commentId, replyId); + replyService.delete(command, js.getId()); + + // then + assertThat(mentionRepository.findAll()).isEmpty(); + } + } }