본문 바로가기

항해99

23.11.02 항해 99 16기 실전 프로젝트 26일차

오늘 공부한 것

* 우리 프로젝트에 Github Action 으로 CI / CD 배포 하기

* 대댓글 조회시 댓글에 List 형식으로 조회되도록 코드 변경

 

오늘 드디어 Github Action 으로 CI / CD 배포 하기에 성공했다

트러블 슈팅은 오늘 날짜로 별도 작성했다

 

이후에는 프론트 분들이 요청하신 사항을 수정했다

대댓글 조회 API 를 별도로 작성했었는데 댓글을 조회할 때 List 형식으로 대댓글이 함께 나올 수 있도록 코드 변경을

원하셨다

변경된 코드는 아래와 같다

 

1. 댓글

더보기

 1) CommentsResponseDto

     repliesList 를 추가해주었다

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommentsResponseDto {
    private Long commentId;
    private String contents;
    private String email;
    private String nickname;
    private String checkUser;
    private LocalDateTime createAt;
    private LocalDateTime modifiedAt;
    private List<RepliesResponseDto> repliesList = new ArrayList<>();



    public CommentsResponseDto(Comments comments, List<RepliesResponseDto> repliesList) {
        this.commentId = comments.getId();
        this.contents = comments.getContents();
        this.createAt = comments.getCreatedAt();
        this.modifiedAt = comments.getModifiedAt();
        this.email = comments.getEmail();
        this.nickname = comments.getNickname();
        this.repliesList = repliesList;
    }

    public CommentsResponseDto(Comments comments, String checkUser, List<RepliesResponseDto> repliesList) {
        this.commentId = comments.getId();
        this.contents = comments.getContents();
        this.createAt = comments.getCreatedAt();
        this.modifiedAt = comments.getModifiedAt();
        this.email = comments.getEmail();
        this.nickname = comments.getNickname();
        this.checkUser = checkUser;
        this.repliesList = repliesList;
    }
}

 

 2) CommentsService

      List 형식으로 repliesList 를 생성하여 넣어주었다

 // 댓글 조회
    public Slice<CommentsResponseDto> commentsList(Long postId,
                                                   Pageable pageable) {

        Posts posts = postsRepository.findById(postId).orElseThrow(
                () -> new CustomException(ErrorCode.POST_NOT_EXIST)); // 존재하지 않는 게시글입니다

        Slice<Comments> commentsList = commentsRepository.findByPosts_IdOrderByCreatedAtDesc(postId, pageable);

        if (commentsList.isEmpty()) {
            throw new CustomException(ErrorCode.COMMENTS_NOT_EXIST); // 존재하지 않는 댓글입니다
        }

        List<CommentsResponseDto> commentsResponseDtoList = new ArrayList<>();

        for (Comments comments : commentsList) {
            List<RepliesResponseDto> repliesList = new ArrayList<>();
            for (Replies replies : comments.getRepliesList()) {
                if (posts.getUsers().getEmail().equals(replies.getEmail())) {
                    repliesList.add(new RepliesResponseDto(replies, "글쓴이"));
                } else {
                    repliesList.add(new RepliesResponseDto(replies));
                }
            }
            if (posts.getUsers().getEmail().equals(comments.getEmail())) {
                commentsResponseDtoList.add(new CommentsResponseDto(comments, "글쓴이", repliesList));
            } else {
                commentsResponseDtoList.add(new CommentsResponseDto(comments, repliesList));
            }
        }
        return new SliceImpl<>(commentsResponseDtoList, pageable, commentsList.hasNext());
    }

 

 

2. 대댓글

더보기

 1) RepliesController

     혹시 몰라서 주석처리 하였다

//    // 대댓글 조회
//    @Operation(summary = "댓글별 대댓글 조회", description = "댓글별 대댓글 조회 api 입니다.")
//    @GetMapping("/comments/{commentId}/replies")
//    public ResponseEntity<Slice<RepliesResponseDto>> repliesList(@PathVariable("commentId") Long commentId,
//                                                                 @PageableDefault Pageable pageable) {
//        return ResponseEntity.ok(repliesService.repliesList(commentId, pageable));
//    }

 

 2) RepliesService

     혹시 몰라서 주석처리 하였다

//    // 대댓글 조회
//    public Slice<RepliesResponseDto> repliesList(Long commentId,
//                                                 Pageable pageable) {
//
//        Comments comments = commentsRepository.findById(commentId).orElseThrow(
//                () -> new CustomException(ErrorCode.COMMENTS_NOT_EXIST)); // 존재하지 않는 댓글입니다
//
//        Posts posts = comments.getPosts();
//
//        Slice<Replies> repliesList = repliesRepository.findByComments_IdOrderByCreatedAtDesc(commentId, pageable);
//
//        if (repliesList.isEmpty()) {
//            throw new CustomException(ErrorCode.REPLIES_NOT_EXIST); // 존재하지 않는 대댓글입니다
//        }
//
//        List<RepliesResponseDto> RepliesResponseDtoList = new ArrayList<>();
//
//        for (Replies replies : repliesList) {
//            if (posts.getUsers().getEmail().equals(replies.getEmail())) {
//                RepliesResponseDtoList.add(new RepliesResponseDto(replies, "글쓴이"));
//            } else {
//                RepliesResponseDtoList.add(new RepliesResponseDto(replies));
//            }
//        }
//        return new SliceImpl<>(RepliesResponseDtoList, pageable, repliesList.hasNext());
//    }