Skip to content

Commit 92e9847

Browse files
committed
feat(community): 게시글 ID로 댓글 조회 메서드 추가
1 parent 84f9578 commit 92e9847

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.somemore.community.repository.comment;
22

33
import com.somemore.community.domain.CommunityComment;
4+
import com.somemore.community.repository.mapper.CommunityCommentView;
45

6+
import java.util.List;
57
import java.util.Optional;
68

79
public interface CommunityCommentRepository {
810
CommunityComment save(CommunityComment communityComment);
911
Optional<CommunityComment> findById(Long id);
12+
List<CommunityCommentView> findCommentsByBoardId(Long boardId);
1013
boolean existsById(Long id);
1114
default boolean doesNotExistById(Long id) {
1215
return !existsById(id);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.somemore.community.repository.comment;
22

3+
import com.querydsl.core.types.Projections;
34
import com.querydsl.jpa.impl.JPAQueryFactory;
45
import com.somemore.community.domain.CommunityComment;
56
import com.somemore.community.domain.QCommunityComment;
7+
import com.somemore.community.repository.mapper.CommunityCommentView;
8+
import com.somemore.volunteer.domain.QVolunteer;
69
import lombok.RequiredArgsConstructor;
710
import org.springframework.stereotype.Repository;
811

12+
import java.util.List;
913
import java.util.Optional;
1014

1115
@RequiredArgsConstructor
@@ -31,6 +35,21 @@ public Optional<CommunityComment> findById(Long id) {
3135
.fetchOne());
3236
}
3337

38+
public List<CommunityCommentView> findCommentsByBoardId(Long boardId) {
39+
QCommunityComment communityComment = QCommunityComment.communityComment;
40+
QVolunteer volunteer = QVolunteer.volunteer;
41+
42+
return queryFactory
43+
.select(Projections.constructor(CommunityCommentView.class,
44+
communityComment,
45+
volunteer.nickname))
46+
.from(communityComment)
47+
.join(volunteer).on(communityComment.writerId.eq(volunteer.id))
48+
.where(communityComment.communityBoardId.eq(boardId))
49+
.orderBy(communityComment.parentCommentId.asc().nullsFirst(), communityComment.createdAt.asc())
50+
.fetch();
51+
}
52+
3453
@Override
3554
public boolean existsById(Long id) {
3655
QCommunityComment communityComment = QCommunityComment.communityComment;

0 commit comments

Comments
 (0)