-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/47 커뮤니티 게시글 삭제 기능 #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
4706e8c
e0d6b16
0dd592c
62692be
7013ef9
8bdaaff
00e5980
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 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 = communityBoardRepository.findById(id) | ||
| .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage())); | ||
|
|
||
| validateWriter(communityBoard, writerId); | ||
|
|
||
| communityBoard.markAsDeleted(); | ||
| } | ||
|
|
||
| private void validateWriter(CommunityBoard communityBoard, UUID writerId) { | ||
| if (communityBoard.getWriterId().equals(writerId)) { | ||
| return; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. communityBoard.getWriterId().equals(writerId) 이 부분은 CommunityBoard 메서드 호출해서 하는거 어떻게 생각하세요??
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TDA 감사합니다 |
||
|
|
||
| throw new BadRequestException(UNAUTHORIZED_COMMUNITY_BOARD.getMessage()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.somemore.community.usecase.command; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public interface DeleteCommunityBoardUseCase { | ||
| void deleteCommunityBoard(UUID writerId, Long id); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| 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.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; | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| communityBoardRepository.deleteAllInBatch(); | ||
| } | ||
|
|
||
| @DisplayName("커뮤니티 게시글 id로 게시글을 삭제한다.") | ||
| @Test | ||
| void deleteCommunityBoardWithId() { | ||
| //given | ||
| CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() | ||
| .title("커뮤니티 테스트 제목") | ||
| .content("커뮤니티 테스트 내용") | ||
| .build(); | ||
|
|
||
|
||
| UUID writerId = UUID.randomUUID(); | ||
| String imgUrl = "https://image.test.url/123"; | ||
|
|
||
| Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); | ||
|
|
||
| //when | ||
| deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId); | ||
|
|
||
| //then | ||
| assertThat(communityBoardQueryUseCase.getCommunityBoards()).isEmpty(); | ||
| } | ||
|
|
||
| @DisplayName("삭제된 커뮤니티 게시글의 id로 게시글을 삭제할 때 예외를 던진다.") | ||
| @Test | ||
| void deleteCommunityBoardWithDeletedId() { | ||
| //given | ||
| CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() | ||
| .title("커뮤니티 테스트 제목") | ||
| .content("커뮤니티 테스트 내용") | ||
| .build(); | ||
|
|
||
| UUID writerId = UUID.randomUUID(); | ||
| String imgUrl = "https://image.test.url/123"; | ||
|
|
||
| Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); | ||
| 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 | ||
| CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() | ||
| .title("커뮤니티 테스트 제목") | ||
| .content("커뮤니티 테스트 내용") | ||
| .build(); | ||
|
|
||
| UUID writerId = UUID.randomUUID(); | ||
| String imgUrl = "https://image.test.url/123"; | ||
|
|
||
| Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); | ||
|
|
||
| //when | ||
| ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(UUID.randomUUID(), communityId); | ||
|
|
||
| //then | ||
| assertThatExceptionOfType(BadRequestException.class) | ||
| .isThrownBy(callable) | ||
| .withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD.getMessage()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아래에 valiadteWriter처럼 메서드로 빼서 추상화 레벨 맞춰주는것도 좋아보여요!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵! 수정하겠습니다