Skip to content

Commit c3f6a22

Browse files
authored
feat: community-board 삭제 기능 (#55)
* test(community): communityBoard 삭제 기능 테스트 작성 * feat(community): CommunityBoard 삭제 Usecase 추가 및 Service 구현 * feat(community): CommunityBoard 삭제 ExceptionMessage 추가 * test(community): 삭제 메서드 변경 * refactor(community): findById 네이밍 변경 사항 반영 * refactor(community): 코드 리뷰 사항 반영 - findById 메서드로 추출(추상화 레벨 일관성) - ServiceTest 중복되는 부분 @beforeeach로 추출 - CommunityBoard 메서드에 isWriter 추가
1 parent 7c4d9f1 commit c3f6a22

File tree

6 files changed

+155
-2
lines changed

6 files changed

+155
-2
lines changed

src/main/java/com/somemore/community/domain/CommunityBoard.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ public CommunityBoard(UUID writerId, String title, String content, String imgUrl
4848
this.content = content;
4949
this.imgUrl = imgUrl;
5050
}
51+
52+
public boolean isWriter(UUID writerId) {
53+
return this.writerId.equals(writerId);
54+
}
5155
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.somemore.community.service.command;
2+
3+
import com.somemore.community.domain.CommunityBoard;
4+
import com.somemore.community.repository.CommunityBoardRepository;
5+
import com.somemore.community.usecase.command.DeleteCommunityBoardUseCase;
6+
import com.somemore.global.exception.BadRequestException;
7+
import lombok.RequiredArgsConstructor;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.transaction.annotation.Transactional;
10+
11+
import java.util.UUID;
12+
13+
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD;
14+
import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD;
15+
16+
@RequiredArgsConstructor
17+
@Transactional
18+
@Service
19+
class DeleteCommunityBoardService implements DeleteCommunityBoardUseCase {
20+
21+
private final CommunityBoardRepository communityBoardRepository;
22+
23+
@Override
24+
public void deleteCommunityBoard(UUID writerId, Long id) {
25+
CommunityBoard communityBoard = getCommunityBoardById(id);
26+
27+
validateWriter(communityBoard, writerId);
28+
29+
communityBoard.markAsDeleted();
30+
31+
communityBoardRepository.save(communityBoard);
32+
}
33+
34+
private CommunityBoard getCommunityBoardById(Long id) {
35+
return communityBoardRepository.findById(id)
36+
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage()));
37+
}
38+
39+
private void validateWriter(CommunityBoard communityBoard, UUID writerId) {
40+
if (communityBoard.isWriter(writerId)) {
41+
return;
42+
}
43+
44+
throw new BadRequestException(UNAUTHORIZED_COMMUNITY_BOARD.getMessage());
45+
}
46+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.somemore.community.usecase.command;
2+
3+
import java.util.UUID;
4+
5+
public interface DeleteCommunityBoardUseCase {
6+
void deleteCommunityBoard(UUID writerId, Long id);
7+
}

src/main/java/com/somemore/global/exception/ExceptionMessage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
public enum ExceptionMessage {
1010

1111
NOT_EXISTS_CENTER("존재하지 않는 기관 ID 입니다."),
12-
NOT_EXISTS_COMMUNITY_BOARD("삭제된 게시글 입니다."),
12+
NOT_EXISTS_COMMUNITY_BOARD("존재하지 않는 게시글 입니다."),
13+
UNAUTHORIZED_COMMUNITY_BOARD("게시글 삭제 권한이 없습니다."),
1314
NOT_EXISTS_LOCATION("존재하지 않는 위치 ID 입니다."),
1415
NOT_EXISTS_RECRUIT_BOARD("존재하지 않는 봉사 모집글 ID 입니다."),
1516
UNAUTHORIZED_RECRUIT_BOARD("자신이 작성한 봉사 모집글이 아닙니다."),
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.somemore.community.service.command;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5+
6+
import com.somemore.IntegrationTestSupport;
7+
import com.somemore.community.dto.request.CommunityBoardCreateRequestDto;
8+
import com.somemore.community.repository.CommunityBoardRepository;
9+
import com.somemore.community.usecase.command.CreateCommunityBoardUseCase;
10+
import com.somemore.community.usecase.query.CommunityBoardQueryUseCase;
11+
import com.somemore.global.exception.BadRequestException;
12+
import com.somemore.global.exception.ExceptionMessage;
13+
import org.assertj.core.api.ThrowableAssert;
14+
import org.junit.jupiter.api.AfterEach;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.DisplayName;
17+
import org.junit.jupiter.api.Test;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
20+
import java.util.UUID;
21+
22+
class DeleteCommunityBoardServiceTest extends IntegrationTestSupport {
23+
@Autowired
24+
private DeleteCommunityBoardService deleteCommunityBoardService;
25+
@Autowired
26+
private CreateCommunityBoardUseCase createCommunityBoardUseCase;
27+
@Autowired
28+
private CommunityBoardQueryUseCase communityBoardQueryUseCase;
29+
@Autowired
30+
private CommunityBoardRepository communityBoardRepository;
31+
32+
private UUID writerId;
33+
private Long communityId;
34+
private String imgUrl;
35+
36+
@BeforeEach
37+
void setUp() {
38+
CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder()
39+
.title("커뮤니티 테스트 제목")
40+
.content("커뮤니티 테스트 내용")
41+
.build();
42+
43+
writerId = UUID.randomUUID();
44+
imgUrl = "https://image.test.url/123";
45+
46+
communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl);
47+
}
48+
49+
@AfterEach
50+
void tearDown() {
51+
communityBoardRepository.deleteAllInBatch();
52+
}
53+
54+
@DisplayName("커뮤니티 게시글 id로 게시글을 삭제한다.")
55+
@Test
56+
void deleteCommunityBoardWithId() {
57+
//given
58+
//when
59+
deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId);
60+
61+
//then
62+
assertThat(communityBoardQueryUseCase.getCommunityBoards()).isEmpty();
63+
}
64+
65+
@DisplayName("삭제된 커뮤니티 게시글의 id로 게시글을 삭제할 때 예외를 던진다.")
66+
@Test
67+
void deleteCommunityBoardWithDeletedId() {
68+
//given
69+
deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId);
70+
71+
//when
72+
ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId);
73+
74+
//then
75+
assertThatExceptionOfType(BadRequestException.class)
76+
.isThrownBy(callable)
77+
.withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD.getMessage());
78+
}
79+
80+
@DisplayName("작성자가 아닌 id로 커뮤니티 게시글을 삭제하고자 할 때 예외를 던진다.")
81+
@Test
82+
void deleteCommunityBoardWithNotWriterId() {
83+
//given
84+
//when
85+
ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(UUID.randomUUID(), communityId);
86+
87+
//then
88+
assertThatExceptionOfType(BadRequestException.class)
89+
.isThrownBy(callable)
90+
.withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD.getMessage());
91+
}
92+
}

src/test/java/com/somemore/community/service/query/CommunityBoardQueryServiceTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.somemore.community.dto.response.CommunityBoardGetResponseDto;
1111
import com.somemore.community.repository.CommunityBoardRepository;
1212
import com.somemore.community.usecase.command.CreateCommunityBoardUseCase;
13+
import com.somemore.community.usecase.command.DeleteCommunityBoardUseCase;
1314
import com.somemore.global.exception.BadRequestException;
1415
import com.somemore.global.exception.ExceptionMessage;
1516
import com.somemore.volunteer.domain.Volunteer;
@@ -37,6 +38,8 @@ class CommunityBoardQueryServiceTest extends IntegrationTestSupport {
3738
@Autowired
3839
CreateCommunityBoardUseCase createCommunityBoardUseCase;
3940
@Autowired
41+
DeleteCommunityBoardUseCase deleteCommunityBoardUseCase;
42+
@Autowired
4043
CommunityBoardQueryService communityBoardQueryService;
4144

4245
@AfterEach
@@ -209,7 +212,7 @@ void getCommunityBoardDetailWithDeletedId() {
209212

210213
Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto1, savedVolunteer.getId(), imgUrl);
211214

212-
communityBoardRepository.deleteAllInBatch();
215+
deleteCommunityBoardUseCase.deleteCommunityBoard(savedVolunteer.getId(), communityId);
213216

214217
//when
215218
ThrowableAssert.ThrowingCallable callable = () -> communityBoardQueryService.getCommunityBoardDetail(communityId);

0 commit comments

Comments
 (0)