diff --git a/src/main/java/com/somemore/community/domain/CommunityBoard.java b/src/main/java/com/somemore/community/domain/CommunityBoard.java index 457957dd9..16e667158 100644 --- a/src/main/java/com/somemore/community/domain/CommunityBoard.java +++ b/src/main/java/com/somemore/community/domain/CommunityBoard.java @@ -48,4 +48,8 @@ public CommunityBoard(UUID writerId, String title, String content, String imgUrl this.content = content; this.imgUrl = imgUrl; } + + 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/command/DeleteCommunityBoardService.java b/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java new file mode 100644 index 000000000..0e0fe857a --- /dev/null +++ b/src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java @@ -0,0 +1,46 @@ +package com.somemore.community.service.command; + +import com.somemore.community.domain.CommunityBoard; +import com.somemore.community.repository.CommunityBoardRepository; +import com.somemore.community.usecase.command.DeleteCommunityBoardUseCase; +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.NOT_EXISTS_COMMUNITY_BOARD; +import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD; + +@RequiredArgsConstructor +@Transactional +@Service +class DeleteCommunityBoardService implements DeleteCommunityBoardUseCase { + + private final CommunityBoardRepository communityBoardRepository; + + @Override + public void deleteCommunityBoard(UUID writerId, Long id) { + CommunityBoard communityBoard = getCommunityBoardById(id); + + validateWriter(communityBoard, writerId); + + communityBoard.markAsDeleted(); + + communityBoardRepository.save(communityBoard); + } + + private CommunityBoard getCommunityBoardById(Long id) { + return communityBoardRepository.findById(id) + .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage())); + } + + private void validateWriter(CommunityBoard communityBoard, UUID writerId) { + if (communityBoard.isWriter(writerId)) { + return; + } + + throw new BadRequestException(UNAUTHORIZED_COMMUNITY_BOARD.getMessage()); + } +} \ No newline at end of file diff --git a/src/main/java/com/somemore/community/usecase/command/DeleteCommunityBoardUseCase.java b/src/main/java/com/somemore/community/usecase/command/DeleteCommunityBoardUseCase.java new file mode 100644 index 000000000..88bca4548 --- /dev/null +++ b/src/main/java/com/somemore/community/usecase/command/DeleteCommunityBoardUseCase.java @@ -0,0 +1,7 @@ +package com.somemore.community.usecase.command; + +import java.util.UUID; + +public interface DeleteCommunityBoardUseCase { + void deleteCommunityBoard(UUID writerId, Long id); +} \ 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 9035b71da..9f92eefcb 100644 --- a/src/main/java/com/somemore/global/exception/ExceptionMessage.java +++ b/src/main/java/com/somemore/global/exception/ExceptionMessage.java @@ -9,7 +9,8 @@ public enum ExceptionMessage { NOT_EXISTS_CENTER("존재하지 않는 기관 ID 입니다."), - NOT_EXISTS_COMMUNITY_BOARD("삭제된 게시글 입니다."), + NOT_EXISTS_COMMUNITY_BOARD("존재하지 않는 게시글 입니다."), + UNAUTHORIZED_COMMUNITY_BOARD("게시글 삭제 권한이 없습니다."), NOT_EXISTS_LOCATION("존재하지 않는 위치 ID 입니다."), NOT_EXISTS_RECRUIT_BOARD("존재하지 않는 봉사 모집글 ID 입니다."), UNAUTHORIZED_RECRUIT_BOARD("자신이 작성한 봉사 모집글이 아닙니다."), diff --git a/src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java b/src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java new file mode 100644 index 000000000..578bec665 --- /dev/null +++ b/src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java @@ -0,0 +1,92 @@ +package com.somemore.community.service.command; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +import com.somemore.IntegrationTestSupport; +import com.somemore.community.dto.request.CommunityBoardCreateRequestDto; +import com.somemore.community.repository.CommunityBoardRepository; +import com.somemore.community.usecase.command.CreateCommunityBoardUseCase; +import com.somemore.community.usecase.query.CommunityBoardQueryUseCase; +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; + +class DeleteCommunityBoardServiceTest extends IntegrationTestSupport { + @Autowired + private DeleteCommunityBoardService deleteCommunityBoardService; + @Autowired + private CreateCommunityBoardUseCase createCommunityBoardUseCase; + @Autowired + private CommunityBoardQueryUseCase communityBoardQueryUseCase; + @Autowired + private CommunityBoardRepository communityBoardRepository; + + private UUID writerId; + private Long communityId; + private String imgUrl; + + @BeforeEach + void setUp() { + CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() + .title("커뮤니티 테스트 제목") + .content("커뮤니티 테스트 내용") + .build(); + + writerId = UUID.randomUUID(); + imgUrl = "https://image.test.url/123"; + + communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); + } + + @AfterEach + void tearDown() { + communityBoardRepository.deleteAllInBatch(); + } + + @DisplayName("커뮤니티 게시글 id로 게시글을 삭제한다.") + @Test + void deleteCommunityBoardWithId() { + //given + //when + deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId); + + //then + assertThat(communityBoardQueryUseCase.getCommunityBoards()).isEmpty(); + } + + @DisplayName("삭제된 커뮤니티 게시글의 id로 게시글을 삭제할 때 예외를 던진다.") + @Test + void deleteCommunityBoardWithDeletedId() { + //given + deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId); + + //when + ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId); + + //then + assertThatExceptionOfType(BadRequestException.class) + .isThrownBy(callable) + .withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD.getMessage()); + } + + @DisplayName("작성자가 아닌 id로 커뮤니티 게시글을 삭제하고자 할 때 예외를 던진다.") + @Test + void deleteCommunityBoardWithNotWriterId() { + //given + //when + ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(UUID.randomUUID(), communityId); + + //then + assertThatExceptionOfType(BadRequestException.class) + .isThrownBy(callable) + .withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD.getMessage()); + } +} diff --git a/src/test/java/com/somemore/community/service/query/CommunityBoardQueryServiceTest.java b/src/test/java/com/somemore/community/service/query/CommunityBoardQueryServiceTest.java index 068e3b8d1..797edc4b8 100644 --- a/src/test/java/com/somemore/community/service/query/CommunityBoardQueryServiceTest.java +++ b/src/test/java/com/somemore/community/service/query/CommunityBoardQueryServiceTest.java @@ -10,6 +10,7 @@ import com.somemore.community.dto.response.CommunityBoardGetResponseDto; import com.somemore.community.repository.CommunityBoardRepository; import com.somemore.community.usecase.command.CreateCommunityBoardUseCase; +import com.somemore.community.usecase.command.DeleteCommunityBoardUseCase; import com.somemore.global.exception.BadRequestException; import com.somemore.global.exception.ExceptionMessage; import com.somemore.volunteer.domain.Volunteer; @@ -37,6 +38,8 @@ class CommunityBoardQueryServiceTest extends IntegrationTestSupport { @Autowired CreateCommunityBoardUseCase createCommunityBoardUseCase; @Autowired + DeleteCommunityBoardUseCase deleteCommunityBoardUseCase; + @Autowired CommunityBoardQueryService communityBoardQueryService; @AfterEach @@ -209,7 +212,7 @@ void getCommunityBoardDetailWithDeletedId() { Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto1, savedVolunteer.getId(), imgUrl); - communityBoardRepository.deleteAllInBatch(); + deleteCommunityBoardUseCase.deleteCommunityBoard(savedVolunteer.getId(), communityId); //when ThrowableAssert.ThrowingCallable callable = () -> communityBoardQueryService.getCommunityBoardDetail(communityId);