Spring Level 3 전체 게시글 목록 조회 API

song yuheon·2023년 9월 2일
0

Spring

목록 보기
40/93
post-thumbnail

전체 게시글 목록 조회 API


각각의 게시글에 등록된 모든 댓글을 게시글과 같이 Client에 반환하기

댓글은 작성 날짜 기준 내림차순으로 정렬하기 ? ( API 명세표에는 ID 기준 오름차순 정렬 -> 명세표 우선 )


  • 게시글 클래스에 해당 게시글의 모든 댓글을 저장할 리스트 Memo에 추가

    private List<Comment> comments = new ArrayList<>();
  • Service ( getMemos ) 수정

    public List<MemoCommentResponseAllDto> getMemos() {
        List<MemoCommentResponseAllDto> memoCommentResponseAllDtoList = new LinkedList<>();
        // DB 조회
        memoRepository.findAllByOrderById().stream() // stream 메소드를 통해 MemoResponseDto로 변환
                .forEach(
                        memo -> { // memo MemoResponseDto로 변환
                            MemoCommentDto temp = new MemoCommentDto(memo,
                                    commentRepository.findAllByPostidOrderById(memo.getId()).stream()
                                            .map(n->new CommentResponseDto().fromComment(n)).toList());
                            memoCommentResponseAllDtoList.add(new MemoCommentResponseAllDto(temp));
    
                        }
    
                );// 메모를 리스트 타입으로 반환
        return memoCommentResponseAllDtoList;
    }

Test



profile
backend_Devloper

0개의 댓글