Skip to content

Commit

Permalink
feat: 멘션 삭제 기능 구현 (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdkdhoho committed Oct 11, 2024
1 parent 819f700 commit 4b627d7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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)) {
Expand All @@ -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<Long> mentionIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -58,9 +60,9 @@ private List<Mention> toMentions(List<Long> 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());

Expand All @@ -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);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/listywave/mention/MentionRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public interface MentionRepository extends JpaRepository<Mention, Long> {
List<Mention> findAllByComment(Comment comment);

List<Mention> findAllByReply(Reply reply);

void deleteAllByComment(Comment comment);

void deleteAllByReply(Reply reply);
}
31 changes: 31 additions & 0 deletions src/test/java/com/listywave/mention/MentionServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}

0 comments on commit 4b627d7

Please sign in to comment.