diff --git a/src/main/java/com/somemore/community/domain/CommunityComment.java b/src/main/java/com/somemore/community/domain/CommunityComment.java index 8e92e1884..42acddc2d 100644 --- a/src/main/java/com/somemore/community/domain/CommunityComment.java +++ b/src/main/java/com/somemore/community/domain/CommunityComment.java @@ -38,4 +38,8 @@ public CommunityComment(UUID writerId, String content, Long parentCommentId) { this.content = content; this.parentCommentId = parentCommentId; } + + public boolean isWriter(UUID writerId) { + return this.writerId.equals(writerId); + } } \ No newline at end of file diff --git a/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java b/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java new file mode 100644 index 000000000..6072c4e4f --- /dev/null +++ b/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java @@ -0,0 +1,46 @@ +package com.somemore.community.service.comment; + +import com.somemore.community.domain.CommunityComment; +import com.somemore.community.repository.comment.CommunityCommentRepository; +import com.somemore.community.usecase.comment.DeleteCommunityCommentUseCase; +import com.somemore.global.exception.BadRequestException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.UUID; + +import static com.somemore.global.exception.ExceptionMessage.*; + +@RequiredArgsConstructor +@Transactional +@Service +public class DeleteCommunityCommentService implements DeleteCommunityCommentUseCase { + + private final CommunityCommentRepository communityCommentRepository; + + @Override + public void deleteCommunityComment(UUID writerId, Long id) { + + CommunityComment communityComment = getCommunityCommentById(id); + + validateWriter(communityComment, writerId); + + communityComment.markAsDeleted(); + + communityCommentRepository.save(communityComment); + } + + private CommunityComment getCommunityCommentById(Long id) { + return communityCommentRepository.findById(id) + .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage())); + } + + private void validateWriter(CommunityComment communityComment, UUID writerId) { + if (communityComment.isWriter(writerId)) { + return; + } + + throw new BadRequestException(UNAUTHORIZED_COMMUNITY_COMMENT.getMessage()); + } +} diff --git a/src/main/java/com/somemore/community/usecase/comment/DeleteCommunityCommentUseCase.java b/src/main/java/com/somemore/community/usecase/comment/DeleteCommunityCommentUseCase.java new file mode 100644 index 000000000..c5e1f5d38 --- /dev/null +++ b/src/main/java/com/somemore/community/usecase/comment/DeleteCommunityCommentUseCase.java @@ -0,0 +1,7 @@ +package com.somemore.community.usecase.comment; + +import java.util.UUID; + +public interface DeleteCommunityCommentUseCase { + void deleteCommunityComment(UUID writerId, Long id); +} diff --git a/src/main/java/com/somemore/global/exception/ExceptionMessage.java b/src/main/java/com/somemore/global/exception/ExceptionMessage.java index 1e5b3f8b2..9a2f41794 100644 --- a/src/main/java/com/somemore/global/exception/ExceptionMessage.java +++ b/src/main/java/com/somemore/global/exception/ExceptionMessage.java @@ -12,6 +12,7 @@ public enum ExceptionMessage { NOT_EXISTS_COMMUNITY_BOARD("존재하지 않는 게시글 입니다."), UNAUTHORIZED_COMMUNITY_BOARD("해당 게시글에 권한이 없습니다."), NOT_EXISTS_COMMUNITY_COMMENT("존재하지 않는 댓글 입니다."), + UNAUTHORIZED_COMMUNITY_COMMENT("해당 댓글에 권한이 없습니다."), NOT_EXISTS_LOCATION("존재하지 않는 위치 ID 입니다."), NOT_EXISTS_RECRUIT_BOARD("존재하지 않는 봉사 모집글 ID 입니다."), UNAUTHORIZED_RECRUIT_BOARD("자신이 작성한 봉사 모집글이 아닙니다."), diff --git a/src/test/java/com/somemore/community/service/comment/DeleteCommunityCommentServiceTest.java b/src/test/java/com/somemore/community/service/comment/DeleteCommunityCommentServiceTest.java new file mode 100644 index 000000000..0b5df5d2c --- /dev/null +++ b/src/test/java/com/somemore/community/service/comment/DeleteCommunityCommentServiceTest.java @@ -0,0 +1,88 @@ +package com.somemore.community.service.comment; + +import com.somemore.IntegrationTestSupport; +import com.somemore.community.dto.request.CommunityCommentCreateRequestDto; +import com.somemore.community.repository.comment.CommunityCommentRepository; +import com.somemore.community.usecase.comment.CreateCommunityCommentUseCase; +import com.somemore.global.exception.BadRequestException; +import com.somemore.global.exception.ExceptionMessage; +import org.assertj.core.api.ThrowableAssert; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +class DeleteCommunityCommentServiceTest extends IntegrationTestSupport { + + @Autowired + private DeleteCommunityCommentService deleteCommunityCommentService; + @Autowired + private CreateCommunityCommentUseCase createCommunityCommentUseCase; + @Autowired + private CommunityCommentRepository communityCommentRepository; + + private UUID writerId; + private Long commentId; + + @BeforeEach + void setUp() { + CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder() + .content("커뮤니티 댓글 테스트 내용") + .parentCommentId(null) + .build(); + + writerId = UUID.randomUUID(); + + commentId = createCommunityCommentUseCase.createCommunityComment(dto, writerId); + } + + @AfterEach + void tearDown() { + communityCommentRepository.deleteAllInBatch(); + } + + @DisplayName("댓글 id로 댓글을 삭제한다.") + @Test + void deleteCommunityCommentWithId() { + //given + //when + deleteCommunityCommentService.deleteCommunityComment(writerId, commentId); + + //then + assertThat(communityCommentRepository.existsById(commentId)).isFalse(); + } + + @DisplayName("삭제된 댓글의 id로 댓글을 삭제할 때 예외를 던진다.") + @Test + void deleteCommunityCommentWithDeletedId() { + //given + deleteCommunityCommentService.deleteCommunityComment(writerId, commentId); + + //when + ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityCommentService.deleteCommunityComment(writerId, commentId); + + //then + assertThatExceptionOfType(BadRequestException.class) + .isThrownBy(callable) + .withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_COMMENT.getMessage()); + } + + @DisplayName("작성자가 아닌 id로 댓글을 삭제하고자 할 때 예외를 던진다.") + @Test + void deleteCommunityCommentWithNotWriterId() { + //given + //when + ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityCommentService.deleteCommunityComment(UUID.randomUUID(), commentId); + + //then + assertThatExceptionOfType(BadRequestException.class) + .isThrownBy(callable) + .withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_COMMENT.getMessage()); + } +}