stream 사용하지 않고 전체 게시글 조회하기
stream을 향상된 for 문으로 바꿔주기
List<PostResponseDto> postResponseDtos = new ArrayList<>();
List<Post> allByOrderByCreatedAtDesc = postRepository.findAllByOrderByCreatedAtDesc();
for (Post post : allByOrderByCreatedAtDesc) {
postResponseDtos.add(new PostResponseDto(post));
}
return postResponseDtos;
비어있는 PostResponseDto List를 만들어주고
PostResponseDto를 postRepository.findAllByOrderByCreatedAtDesc(); 에서 받아온 데이터로 채워준다.
stream을 사용할 줄 몰라도 적절하게 for 문을 사용하면 완성할 수 있었다.
Spring을 하면서 자바를 꾸준하게 공부해야하는 이유가 더 확실해졌다.