PROJECT-LOG/likelion.university

[리팩토링] 대댓글 생성시, 댓글 id만 받고, postid는 댓글 id에서 뽑기

HwangJerry 2023. 10. 13. 16:32

기존 구현

postId와 parentCommentId를 둘 다 body로 받아서 대댓글 생성.

 

문제점

parentComment가 가진 postId와 childComment가 가진 postId가 불일치하진 않는지 체크해야 한다. 이는 불필요한 로직을 추가적으로 양산하며, 클라이언트가 postId까지 신경쓰도록 반영해야 하므로 서버에서 parentComment가 가진 postId를 뽑아서 반영해주는 로직이 더 깔끔할 것이라 보았다.

 

결과 로직

private Comment childCommentBy(CommentCreateChildServiceDto request) {
    Comment comment = Comment.builder()
            .post(getPostFromParentComment(request))
            .author(userAdaptor.findById(request.getLoginUserId()))
            .body(request.getBody())
            .build();
    comment.setParent(parentCommentBy(request.getParentCommentId()));
    return comment;
}

private Post getPostFromParentComment(CommentCreateChildServiceDto request) {
    return commentAdaptor.findById(request.getParentCommentId()).getPost();
}