-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4706e8c
test(community): communityBoard 삭제 기능 테스트 작성
ayoung-dev e0d6b16
feat(community): CommunityBoard 삭제 Usecase 추가 및 Service 구현
ayoung-dev 0dd592c
feat(community): CommunityBoard 삭제 ExceptionMessage 추가
ayoung-dev 62692be
Merge branch 'main' of https://github.com/prgrms-web-devcourse-final-…
ayoung-dev 7013ef9
test(community): 삭제 메서드 변경
ayoung-dev 8bdaaff
refactor(community): findById 네이밍 변경 사항 반영
ayoung-dev 00e5980
refactor(community): 코드 리뷰 사항 반영
ayoung-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()); | ||
| } | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
src/main/java/com/somemore/community/usecase/command/DeleteCommunityBoardUseCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
communityBoard.getWriterId().equals(writerId)
이 부분은 CommunityBoard 메서드 호출해서 하는거 어떻게 생각하세요??
communityBoard.isWriter(writerId); 이런식으로요
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.
TDA 감사합니다
수정하겠습니다!