Skip to content

Commit 33f9854

Browse files
authored
feat: community-comment 수정 기능 (#77)
* test(community): communityComment 수정 기능 테스트 작성 * test(community): communityComment 수정 Usecase 및 Service 생성 * test(community): communityComment 수정 requestDto 생성 및 Entity 메서드 추가 * test(community): communityComment 수정 기능 테스트 작성 * test(community): communityComment 수정 Usecase 및 Service 생성 * test(community): communityComment 수정 requestDto 생성 및 Entity 메서드 추가 * feat(community): communityComment 수정 시 게시글 존재 검증 메서드 추가 * chore(community): 파일 마지막에 개행 추가
1 parent be132e1 commit 33f9854

File tree

5 files changed

+204
-1
lines changed

5 files changed

+204
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.somemore.community.domain;
22

3+
import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;
34
import com.somemore.global.common.BaseEntity;
45
import jakarta.persistence.*;
56
import lombok.Builder;
@@ -46,4 +47,8 @@ public CommunityComment(Long communityBoardId, UUID writerId, String content, Lo
4647
public boolean isWriter(UUID writerId) {
4748
return this.writerId.equals(writerId);
4849
}
49-
}
50+
51+
public void updateWith(CommunityCommentUpdateRequestDto dto) {
52+
this.content = dto.content();
53+
}
54+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.somemore.community.dto.request;
2+
3+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
4+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
5+
import io.swagger.v3.oas.annotations.media.Schema;
6+
import jakarta.validation.constraints.NotBlank;
7+
import lombok.Builder;
8+
9+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
10+
@Builder
11+
public record CommunityCommentUpdateRequestDto (
12+
@Schema(description = "커뮤니티 댓글 내용", example = "저도 함께 하고 싶습니다.")
13+
@NotBlank(message = "댓글 내용은 필수 값입니다.")
14+
String content
15+
) {}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.somemore.community.service.comment;
2+
3+
import com.somemore.community.domain.CommunityComment;
4+
import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;
5+
import com.somemore.community.repository.board.CommunityBoardRepository;
6+
import com.somemore.community.repository.comment.CommunityCommentRepository;
7+
import com.somemore.community.usecase.comment.UpdateCommunityCommentUseCase;
8+
import com.somemore.global.exception.BadRequestException;
9+
import lombok.RequiredArgsConstructor;
10+
import org.springframework.stereotype.Service;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
import java.util.UUID;
14+
15+
import static com.somemore.global.exception.ExceptionMessage.*;
16+
17+
@RequiredArgsConstructor
18+
@Transactional
19+
@Service
20+
public class UpdateCommunityCommentService implements UpdateCommunityCommentUseCase {
21+
22+
private final CommunityCommentRepository communityCommentRepository;
23+
private final CommunityBoardRepository communityBoardRepository;
24+
25+
@Override
26+
public void updateCommunityComment(CommunityCommentUpdateRequestDto requestDto, Long communityCommentId, UUID writerId) {
27+
28+
CommunityComment communityComment = getCommunityCommentById(communityCommentId);
29+
30+
validateCommunityBoardExists(communityComment.getCommunityBoardId());
31+
32+
validateWriter(communityComment, writerId);
33+
34+
communityComment.updateWith(requestDto);
35+
36+
communityCommentRepository.save(communityComment);
37+
}
38+
39+
private CommunityComment getCommunityCommentById(Long id) {
40+
return communityCommentRepository.findById(id)
41+
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage()));
42+
}
43+
44+
private void validateCommunityBoardExists(Long communityBoardId) {
45+
if (communityBoardRepository.doesNotExistById(communityBoardId)) {
46+
throw new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage());
47+
}
48+
}
49+
50+
private void validateWriter(CommunityComment communityComment, UUID writerId) {
51+
if (communityComment.isWriter(writerId)) {
52+
return;
53+
}
54+
55+
throw new BadRequestException(UNAUTHORIZED_COMMUNITY_COMMENT.getMessage());
56+
}
57+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.somemore.community.usecase.comment;
2+
3+
import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;
4+
5+
import java.util.UUID;
6+
7+
public interface UpdateCommunityCommentUseCase {
8+
void updateCommunityComment(
9+
CommunityCommentUpdateRequestDto requestDto,
10+
Long communityCommentId,
11+
UUID writerId);
12+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.somemore.community.service.comment;
2+
3+
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD;
4+
import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_COMMUNITY_COMMENT;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
7+
8+
import com.somemore.IntegrationTestSupport;
9+
import com.somemore.community.domain.CommunityBoard;
10+
import com.somemore.community.domain.CommunityComment;
11+
import com.somemore.community.dto.request.CommunityBoardCreateRequestDto;
12+
import com.somemore.community.dto.request.CommunityCommentCreateRequestDto;
13+
import com.somemore.community.dto.request.CommunityCommentUpdateRequestDto;
14+
import com.somemore.community.repository.board.CommunityBoardRepository;
15+
import com.somemore.community.repository.comment.CommunityCommentRepository;
16+
import com.somemore.global.exception.BadRequestException;
17+
import org.assertj.core.api.ThrowableAssert;
18+
import org.junit.jupiter.api.AfterEach;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.DisplayName;
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
24+
import java.util.Optional;
25+
import java.util.UUID;
26+
27+
class UpdateCommunityCommentServiceTest extends IntegrationTestSupport {
28+
29+
@Autowired
30+
private UpdateCommunityCommentService updateCommunityCommentService;
31+
@Autowired
32+
private CommunityCommentRepository communityCommentRepository;
33+
@Autowired
34+
private CommunityBoardRepository communityBoardRepository;
35+
36+
private UUID writerId;
37+
private Long commentId;
38+
private CommunityCommentUpdateRequestDto updateRequestDto;
39+
40+
@BeforeEach
41+
void setUp() {
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())
53+
.content("커뮤니티 댓글 테스트 내용")
54+
.parentCommentId(null)
55+
.build();
56+
57+
CommunityComment communityComment = communityCommentRepository.save(commentDto.toEntity(writerId));
58+
59+
commentId = communityComment.getId();
60+
61+
updateRequestDto = CommunityCommentUpdateRequestDto.builder()
62+
.content("수정한 커뮤니티 댓글 내용")
63+
.build();
64+
}
65+
66+
@AfterEach
67+
void tearDown() {
68+
communityCommentRepository.deleteAllInBatch();
69+
}
70+
71+
@DisplayName("댓글을 수정한다.")
72+
@Test
73+
void updateCommunityComment() {
74+
75+
//given
76+
//when
77+
updateCommunityCommentService.updateCommunityComment(updateRequestDto, commentId, writerId);
78+
79+
//then
80+
Optional<CommunityComment> communityComment = communityCommentRepository.findById(commentId);
81+
assertThat(communityComment).isPresent();
82+
assertThat(communityComment.get().getId()).isEqualTo(commentId);
83+
assertThat(communityComment.get().getContent()).isEqualTo("수정한 커뮤니티 댓글 내용");
84+
}
85+
86+
@DisplayName("작성자가 아닌 id로 댓글을 수정하고자 할 때 예외를 던진다.")
87+
@Test
88+
void updateCommunityCommentWithNotWriterId() {
89+
90+
//given
91+
//when
92+
ThrowableAssert.ThrowingCallable callable = () -> updateCommunityCommentService.updateCommunityComment(updateRequestDto, commentId, UUID.randomUUID());
93+
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();
106+
//when
107+
ThrowableAssert.ThrowingCallable callable = () -> updateCommunityCommentService.updateCommunityComment(updateRequestDto, commentId, writerId);
108+
109+
//then
110+
assertThatExceptionOfType(BadRequestException.class)
111+
.isThrownBy(callable)
112+
.withMessage(NOT_EXISTS_COMMUNITY_BOARD.getMessage());
113+
}
114+
}

0 commit comments

Comments
 (0)