Skip to content

Commit 00e5980

Browse files
committed
refactor(community): 코드 리뷰 사항 반영
- findById 메서드로 추출(추상화 레벨 일관성) - ServiceTest 중복되는 부분 @beforeeach로 추출 - CommunityBoard 메서드에 isWriter 추가
1 parent 8bdaaff commit 00e5980

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
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
}

src/main/java/com/somemore/community/service/command/DeleteCommunityBoardService.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@ class DeleteCommunityBoardService implements DeleteCommunityBoardUseCase {
2222

2323
@Override
2424
public void deleteCommunityBoard(UUID writerId, Long id) {
25-
CommunityBoard communityBoard = communityBoardRepository.findById(id)
26-
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage()));
25+
CommunityBoard communityBoard = getCommunityBoardById(id);
2726

2827
validateWriter(communityBoard, writerId);
2928

3029
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()));
3137
}
3238

3339
private void validateWriter(CommunityBoard communityBoard, UUID writerId) {
34-
if (communityBoard.getWriterId().equals(writerId)) {
40+
if (communityBoard.isWriter(writerId)) {
3541
return;
3642
}
3743

src/test/java/com/somemore/community/service/command/DeleteCommunityBoardServiceTest.java

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.somemore.global.exception.ExceptionMessage;
1313
import org.assertj.core.api.ThrowableAssert;
1414
import org.junit.jupiter.api.AfterEach;
15+
import org.junit.jupiter.api.BeforeEach;
1516
import org.junit.jupiter.api.DisplayName;
1617
import org.junit.jupiter.api.Test;
1718
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,6 +29,23 @@ class DeleteCommunityBoardServiceTest extends IntegrationTestSupport {
2829
@Autowired
2930
private CommunityBoardRepository communityBoardRepository;
3031

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+
3149
@AfterEach
3250
void tearDown() {
3351
communityBoardRepository.deleteAllInBatch();
@@ -37,16 +55,6 @@ void tearDown() {
3755
@Test
3856
void deleteCommunityBoardWithId() {
3957
//given
40-
CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder()
41-
.title("커뮤니티 테스트 제목")
42-
.content("커뮤니티 테스트 내용")
43-
.build();
44-
45-
UUID writerId = UUID.randomUUID();
46-
String imgUrl = "https://image.test.url/123";
47-
48-
Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl);
49-
5058
//when
5159
deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId);
5260

@@ -58,15 +66,6 @@ void deleteCommunityBoardWithId() {
5866
@Test
5967
void deleteCommunityBoardWithDeletedId() {
6068
//given
61-
CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder()
62-
.title("커뮤니티 테스트 제목")
63-
.content("커뮤니티 테스트 내용")
64-
.build();
65-
66-
UUID writerId = UUID.randomUUID();
67-
String imgUrl = "https://image.test.url/123";
68-
69-
Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl);
7069
deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId);
7170

7271
//when
@@ -82,16 +81,6 @@ void deleteCommunityBoardWithDeletedId() {
8281
@Test
8382
void deleteCommunityBoardWithNotWriterId() {
8483
//given
85-
CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder()
86-
.title("커뮤니티 테스트 제목")
87-
.content("커뮤니티 테스트 내용")
88-
.build();
89-
90-
UUID writerId = UUID.randomUUID();
91-
String imgUrl = "https://image.test.url/123";
92-
93-
Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl);
94-
9584
//when
9685
ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(UUID.randomUUID(), communityId);
9786

@@ -100,4 +89,4 @@ void deleteCommunityBoardWithNotWriterId() {
10089
.isThrownBy(callable)
10190
.withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD.getMessage());
10291
}
103-
}
92+
}

0 commit comments

Comments
 (0)