From 370911c05a3a631d861408cef0c26af6527a1c9a Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Thu, 28 Nov 2024 23:02:54 +0900 Subject: [PATCH 1/4] =?UTF-8?q?test(community):=20CommunityComment=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DeleteCommunityCommentServiceTest.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/test/java/com/somemore/community/service/comment/DeleteCommunityCommentServiceTest.java 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()); + } +} From 9f3333b03c063f301af70f78fca31155689e88b3 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Thu, 28 Nov 2024 23:03:52 +0900 Subject: [PATCH 2/4] =?UTF-8?q?test(community):=20CommunityComment=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20Usecase=20=EC=B6=94=EA=B0=80=20=EB=B0=8F?= =?UTF-8?q?=20Service=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DeleteCommunityCommentService.java | 46 +++++++++++++++++++ .../DeleteCommunityCommentUseCase.java | 7 +++ 2 files changed, 53 insertions(+) create mode 100644 src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java create mode 100644 src/main/java/com/somemore/community/usecase/comment/DeleteCommunityCommentUseCase.java 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..f2124be54 --- /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 = getCommunityBoardById(id); + + validateWriter(communityComment, writerId); + + communityComment.markAsDeleted(); + + communityCommentRepository.save(communityComment); + } + + private CommunityComment getCommunityBoardById(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); +} From 20bc66f03012e93c0f64ab869a7274b55f4da612 Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Thu, 28 Nov 2024 23:04:20 +0900 Subject: [PATCH 3/4] =?UTF-8?q?test(community):=20CommunityComment=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=EC=9E=90=20=ED=99=95=EC=9D=B8=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=B0=8F=20=EA=B6=8C=ED=95=9C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EB=A9=94=EC=84=B8=EC=A7=80=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/somemore/community/domain/CommunityComment.java | 4 ++++ .../java/com/somemore/global/exception/ExceptionMessage.java | 1 + 2 files changed, 5 insertions(+) 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/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("자신이 작성한 봉사 모집글이 아닙니다."), From 818b92ab0e491aa51ab5b509a0fdfddd8d94c14f Mon Sep 17 00:00:00 2001 From: ayoung-dev Date: Fri, 29 Nov 2024 10:06:53 +0900 Subject: [PATCH 4/4] =?UTF-8?q?refactor(community):=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EB=A6=AC=EB=B7=B0=20=EC=82=AC=ED=95=AD=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - getCommunityBoardById -> getCommunityCommentById --- .../service/comment/DeleteCommunityCommentService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java b/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java index f2124be54..6072c4e4f 100644 --- a/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java +++ b/src/main/java/com/somemore/community/service/comment/DeleteCommunityCommentService.java @@ -22,7 +22,7 @@ public class DeleteCommunityCommentService implements DeleteCommunityCommentUseC @Override public void deleteCommunityComment(UUID writerId, Long id) { - CommunityComment communityComment = getCommunityBoardById(id); + CommunityComment communityComment = getCommunityCommentById(id); validateWriter(communityComment, writerId); @@ -31,7 +31,7 @@ public void deleteCommunityComment(UUID writerId, Long id) { communityCommentRepository.save(communityComment); } - private CommunityComment getCommunityBoardById(Long id) { + private CommunityComment getCommunityCommentById(Long id) { return communityCommentRepository.findById(id) .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage())); }