토이 프로젝트 스터디 #20
- 스터디 진행 날짜 : 7/18
 
- 스터디 작업 날짜 : 7/15 ~ 7/18
 
토이 프로젝트 진행 사항
내용
@Repository
@RequiredArgsConstructor
public class CustomCommentRepositoryImpl implements CustomCommentRepository {
    private static final QComment QCOMMENT = QComment.comment;
    private final JPAQueryFactory queryFactory;
    
    @Value("{page.limit}")
    private int limit;
    
    ...
    
    @Override
    public List<RootCommentResponse> findCommentsWithChildrenCount(Pageable pageable, Post post) {
        QComment parent = new QComment("parent");
        QComment child = new QComment("child");
        return queryFactory
                .select(
                        Projections.fields(RootCommentResponse.class,
                                parent.id.as("commentId"),
                                parent.account.nickname.as("author"),
                                parent.content.as("content"),
                                parent.depth.as("depth"),
                                parent.createdDate.as("createdDate"),
                                ExpressionUtils.as(
                                        JPAExpressions
                                                .select(child.count())
                                                .from(child)
                                                .where(child.leftNode.gt(parent.leftNode), child.rightNode.lt(parent.rightNode)),
                                        "childrenCount"
                                )
                        )
                )
                .from(parent)
                .innerJoin(parent.account)
                .where(parent.post.eq(post).and(parent.isDeleted.eq(false)), parent.depth.lt(2))
                .orderBy(parent.rootComment.id.asc(), parent.leftNode.asc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetch();
    }
    
    @Override
    public List<ChildCommentResponse>findChildrenCommentsByRootCommentId(Post post, Long rootCommentId, Long commentId) {
        return queryFactory
                .select(
                        Projections.fields(ChildCommentResponse.class,
                                QCOMMENT.id.as("commentId"),
                                QCOMMENT.account.nickname.as("author"),
                                QCOMMENT.content.as("content"),
                                QCOMMENT.depth.as("depth"),
                                QCOMMENT.createdDate.as("createdDate"),
                                QCOMMENT.rootComment.id.as("rootId"),
                                QCOMMENT.parentComment.id.as("parentId"),
                                QCOMMENT.parentComment.account.nickname.as("parentAuthor")
                        )
                )
                .from(QCOMMENT)
                .innerJoin(QCOMMENT.account)
                .innerJoin(QCOMMENT.parentComment.account)
                .where(QCOMMENT.post.eq(post).and(QCOMMENT.parentComment.isDeleted.eq(false)))
                .where(QCOMMENT.depth.gt(1))
                .where(isGreaterThanCommentId(commentId))
                .where(QCOMMENT.rootComment.id.eq(rootCommentId))
                .orderBy(QCOMMENT.leftNode.asc())
                .limit(limit)
                .fetch();
    }
- 루트 댓글 조회 메소드와 자식 댓글 조회 메소드 추가
- 루트 댓글 조회 시 : 
PageRequest를 활용한 페이징 처리 
- 자식 댓글 조회 시 : 
cursor 기반 페이징 처리 
 
실행 결과
{
    "comments": [
        {
            "id": 1,
            "author": "nickname1",
            "content": "a",
            "depth": 1,
            "createdDate": "2022-07-18 19:55:00",
            "rootId": 1
        },
        {
            "id": 2,
            "author": "nickname1",
            "content": "b",
            "depth": 1,
            "createdDate": "2022-07-18 19:55:05",
            "rootId": 2
        },
        {
            "id": 3,
            "author": "nickname1",
            "content": "c",
            "depth": 2,
            "createdDate": "2022-07-18 19:55:15",
            "parentId": 2,
            "rootId": 2
        },
        {
            "id": 4,
            "author": "nickname1",
            "content": "d",
            "depth": 2,
            "createdDate": "2022-07-18 19:55:17",
            "parentId": 2,
            "rootId": 2
        },
        {
            "id": 5,
            "author": "nickname1",
            "content": "e",
            "depth": 3,
            "createdDate": "2022-07-18 19:55:22",
            "parentId": 4,
            "rootId": 2
        }
    ],
    "startPage": 1,
    "nowPage": 1,
    "endPage": 1,
    "prev": false,
    "next": false
}
- 이전 댓글 조회 방식
 
- 루트 댓글뿐만 아니라 자식 댓글까지 모두 페이징의 대상이 됨 
 
http://localhost:8080/api/comments/root/1?page=1&size=5
{
    "comments": [
        {
            "commentId": 1,
            "author": "nickname1",
            "content": "a",
            "depth": 1,
            "createdDate": "2022-07-18 19:55:00",
            "childrenCount": 0
        },
        {
            "commentId": 2,
            "author": "nickname1",
            "content": "b",
            "depth": 1,
            "createdDate": "2022-07-18 19:55:05",
            "childrenCount": 3
        }
    ],
    "startPage": 1,
    "nowPage": 1,
    "endPage": 1,
    "prev": false,
    "next": false
}
- 추가된 루트 댓글 조회 방식
 
- 오직 루트 댓글만 조회하도록 처리
 
- 각 루트 댓글이 가지고 있는 자식 댓글의 수를 출력하도록 설정
 
{
    "postId" : 1,
    "rootCommentId" : 2
}
{
    "comments": [
        {
            "commentId": 3,
            "author": "nickname1",
            "content": "c",
            "depth": 2,
            "createdDate": "2022-07-18 19:55:15",
            "rootId": 2,
            "parentId": 2,
            "parentAuthor": "nickname1"
        },
        {
            "commentId": 4,
            "author": "nickname1",
            "content": "d",
            "depth": 2,
            "createdDate": "2022-07-18 19:55:17",
            "rootId": 2,
            "parentId": 2,
            "parentAuthor": "nickname1"
        },
        {
            "commentId": 5,
            "author": "nickname1",
            "content": "e",
            "depth": 3,
            "createdDate": "2022-07-18 19:55:22",
            "rootId": 2,
            "parentId": 4,
            "parentAuthor": "nickname1"
        }
    ],
    "lastChildCommentId": 5
}
- 추가된 자식 댓글 조회 방식
 
- 루트 댓글의 
ID를 통해 해당 루트 댓글만의 자식 댓글 조회 
lastChildCommentId를 통해 cursor 방식으로 다음 자식 댓글 조회 가능