Skip to content

Commit e514f40

Browse files
committed
feat(community): communityComment 수정 시 게시글 존재 검증 메서드 추가
1 parent e19055f commit e514f40

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

src/main/java/com/somemore/community/service/comment/UpdateCommunityCommentService.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.somemore.community.domain.CommunityComment;
44
import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;
5+
import com.somemore.community.repository.board.CommunityBoardRepository;
56
import com.somemore.community.repository.comment.CommunityCommentRepository;
67
import com.somemore.community.usecase.comment.UpdateCommunityCommentUseCase;
78
import com.somemore.global.exception.BadRequestException;
@@ -19,12 +20,17 @@
1920
public class UpdateCommunityCommentService implements UpdateCommunityCommentUseCase {
2021

2122
private final CommunityCommentRepository communityCommentRepository;
23+
private final CommunityBoardRepository communityBoardRepository;
2224

2325
@Override
2426
public void updateCommunityComment(CommunityCommentUpdateRequestDto requestDto, Long communityCommentId, UUID writerId) {
2527

2628
CommunityComment communityComment = getCommunityCommentById(communityCommentId);
29+
30+
validateCommunityBoardExists(communityComment.getCommunityBoardId());
31+
2732
validateWriter(communityComment, writerId);
33+
2834
communityComment.updateWith(requestDto);
2935

3036
communityCommentRepository.save(communityComment);
@@ -35,11 +41,17 @@ private CommunityComment getCommunityCommentById(Long id) {
3541
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage()));
3642
}
3743

44+
private void validateCommunityBoardExists(Long communityBoardId) {
45+
if (communityBoardRepository.doesNotExistById(communityBoardId)) {
46+
throw new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage());
47+
}
48+
}
49+
3850
private void validateWriter(CommunityComment communityComment, UUID writerId) {
3951
if (communityComment.isWriter(writerId)) {
4052
return;
4153
}
42-
54+
4355
throw new BadRequestException(UNAUTHORIZED_COMMUNITY_COMMENT.getMessage());
4456
}
4557
}
Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.somemore.community.service.comment;
22

3+
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD;
4+
import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_COMMUNITY_COMMENT;
35
import static org.assertj.core.api.Assertions.assertThat;
46
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
57

68
import com.somemore.IntegrationTestSupport;
9+
import com.somemore.community.domain.CommunityBoard;
710
import com.somemore.community.domain.CommunityComment;
11+
import com.somemore.community.dto.request.CommunityBoardCreateRequestDto;
812
import com.somemore.community.dto.request.CommunityCommentCreateRequestDto;
913
import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;
14+
import com.somemore.community.repository.board.CommunityBoardRepository;
1015
import com.somemore.community.repository.comment.CommunityCommentRepository;
11-
import com.somemore.community.usecase.comment.CreateCommunityCommentUseCase;
1216
import com.somemore.global.exception.BadRequestException;
13-
import com.somemore.global.exception.ExceptionMessage;
1417
import org.assertj.core.api.ThrowableAssert;
1518
import org.junit.jupiter.api.AfterEach;
1619
import org.junit.jupiter.api.BeforeEach;
@@ -26,23 +29,38 @@ class UpdateCommunityCommentServiceTest extends IntegrationTestSupport {
2629
@Autowired
2730
private UpdateCommunityCommentService updateCommunityCommentService;
2831
@Autowired
29-
private CreateCommunityCommentUseCase createCommunityCommentUseCase;
30-
@Autowired
3132
private CommunityCommentRepository communityCommentRepository;
33+
@Autowired
34+
private CommunityBoardRepository communityBoardRepository;
3235

3336
private UUID writerId;
3437
private Long commentId;
38+
private CommunityCommentUpdateRequestDto updateRequestDto;
3539

3640
@BeforeEach
3741
void setUp() {
38-
CommunityCommentCreateRequestDto dto = CommunityCommentCreateRequestDto.builder()
42+
CommunityBoardCreateRequestDto boardDto = CommunityBoardCreateRequestDto.builder()
43+
.title("커뮤니티 테스트 제목")
44+
.content("커뮤니티 테스트 내용")
45+
.build();
46+
47+
writerId = UUID.randomUUID();
48+
49+
CommunityBoard communityBoard = communityBoardRepository.save(boardDto.toEntity(writerId, "https://test.image/123"));
50+
51+
CommunityCommentCreateRequestDto commentDto = CommunityCommentCreateRequestDto.builder()
52+
.communityBoardId(communityBoard.getId())
3953
.content("커뮤니티 댓글 테스트 내용")
4054
.parentCommentId(null)
4155
.build();
4256

43-
writerId = UUID.randomUUID();
57+
CommunityComment communityComment = communityCommentRepository.save(commentDto.toEntity(writerId));
58+
59+
commentId = communityComment.getId();
4460

45-
commentId = createCommunityCommentUseCase.createCommunityComment(dto, writerId);
61+
updateRequestDto = CommunityCommentUpdateRequestDto.builder()
62+
.content("수정한 커뮤니티 댓글 내용")
63+
.build();
4664
}
4765

4866
@AfterEach
@@ -53,13 +71,10 @@ void tearDown() {
5371
@DisplayName("댓글을 수정한다.")
5472
@Test
5573
void updateCommunityComment() {
56-
//given
57-
CommunityCommentUpdateRequestDto dto = CommunityCommentUpdateRequestDto.builder()
58-
.content("수정한 커뮤니티 댓글 내용")
59-
.build();
6074

75+
//given
6176
//when
62-
updateCommunityCommentService.updateCommunityComment(dto, commentId, writerId);
77+
updateCommunityCommentService.updateCommunityComment(updateRequestDto, commentId, writerId);
6378

6479
//then
6580
Optional<CommunityComment> communityComment = communityCommentRepository.findById(commentId);
@@ -71,17 +86,29 @@ void updateCommunityComment() {
7186
@DisplayName("작성자가 아닌 id로 댓글을 수정하고자 할 때 예외를 던진다.")
7287
@Test
7388
void updateCommunityCommentWithNotWriterId() {
89+
7490
//given
75-
CommunityCommentUpdateRequestDto dto = CommunityCommentUpdateRequestDto.builder()
76-
.content("수정한 커뮤니티 댓글 내용")
77-
.build();
91+
//when
92+
ThrowableAssert.ThrowingCallable callable = () -> updateCommunityCommentService.updateCommunityComment(updateRequestDto, commentId, UUID.randomUUID());
7893

94+
//then
95+
assertThatExceptionOfType(BadRequestException.class)
96+
.isThrownBy(callable)
97+
.withMessage(UNAUTHORIZED_COMMUNITY_COMMENT.getMessage());
98+
}
99+
100+
@DisplayName("삭제된 게시글의 댓글을 수정하고자 할 때 예외를 던진다.")
101+
@Test
102+
void updateCommunityCommentWithDeletedBoardId() {
103+
104+
//given
105+
communityBoardRepository.deleteAllInBatch();
79106
//when
80-
ThrowableAssert.ThrowingCallable callable = () -> updateCommunityCommentService.updateCommunityComment(dto, commentId, UUID.randomUUID());
107+
ThrowableAssert.ThrowingCallable callable = () -> updateCommunityCommentService.updateCommunityComment(updateRequestDto, commentId, writerId);
81108

82109
//then
83110
assertThatExceptionOfType(BadRequestException.class)
84111
.isThrownBy(callable)
85-
.withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_COMMENT.getMessage());
112+
.withMessage(NOT_EXISTS_COMMUNITY_BOARD.getMessage());
86113
}
87114
}

0 commit comments

Comments
 (0)