Skip to content

Commit d1caba6

Browse files
committed
feat(community): 커뮤니티 댓글 존재 여부 메서드 및 테스트 추가
1 parent 27a199a commit d1caba6

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

src/main/java/com/somemore/community/repository/comment/CommunityCommentRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
public interface CommunityCommentRepository {
88
CommunityComment save(CommunityComment communityComment);
99
Optional<CommunityComment> findById(Long id);
10+
boolean existsById(Long id);
1011
void deleteAllInBatch();
1112
}

src/main/java/com/somemore/community/repository/comment/CommunityCommentRepositoryImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.stereotype.Repository;
88

99
import java.util.Optional;
10+
import java.util.UUID;
1011

1112
@RequiredArgsConstructor
1213
@Repository
@@ -31,6 +32,18 @@ public Optional<CommunityComment> findById(Long id) {
3132
.fetchOne());
3233
}
3334

35+
@Override
36+
public boolean existsById(Long id) {
37+
QCommunityComment communityComment = QCommunityComment.communityComment;
38+
39+
return queryFactory
40+
.selectOne()
41+
.from(communityComment)
42+
.where(communityComment.id.eq(id)
43+
.and(communityComment.deleted.eq(false)))
44+
.fetchFirst() != null;
45+
}
46+
3447
@Override
3548
public void deleteAllInBatch() { communityCommentJpaRepository.deleteAllInBatch(); }
3649
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ public Long createCommunityComment(CommunityCommentCreateRequestDto requestDto,
3030
}
3131

3232
private void validateParentCommentExists(Long parentCommentId) {
33-
if (parentCommentId != null) {
34-
communityCommentRepository.findById(parentCommentId)
35-
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage()));
33+
if (parentCommentId != null && !communityCommentRepository.existsById(parentCommentId)) {
34+
throw new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage());
3635
}
3736
}
38-
3937
}

src/test/java/com/somemore/community/repository/CommunityCommentRepositoryTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,26 @@ void findCommunityCommentById() {
8686
assertThat(comment.get().getContent()).isEqualTo("커뮤니티 댓글 테스트 내용");
8787
assertThat(comment.get().getParentCommentId()).isNull();
8888
}
89+
90+
@DisplayName("댓글 id로 댓글이 존재하는지 확인할 수 있다.")
91+
@Test
92+
void existsById() {
93+
94+
//given
95+
UUID writerId = UUID.randomUUID();
96+
97+
CommunityComment communityComment = CommunityComment.builder()
98+
.writerId(writerId)
99+
.content("커뮤니티 댓글 테스트 내용")
100+
.parentCommentId(null)
101+
.build();
102+
103+
CommunityComment savedComment = communityCommentRepository.save(communityComment);
104+
105+
//when
106+
boolean isExist = communityCommentRepository.existsById(savedComment.getId());
107+
108+
//then
109+
assertThat(isExist).isTrue();
110+
}
89111
}

0 commit comments

Comments
 (0)