//모든 포스트들을 가져옴
List<PostDto> getAllPosts();
@Override
public PostDto createPost(PostDto postDto) {
// PostDto => Post 변환
Post post = mapToPost(postDto);
// DB에 새 포스트 저장 (리턴 Post)
Post newPost = postRepository.save(post);
// Post => PostDto 변환
PostDto postResponse = mapToDto(newPost);
return postResponse;
}
@Override
public List<PostDto> getAllPosts() {
return null;
}
//Entity -> DTO
private PostDto mapToDto(Post post) {
PostDto postResponse = new PostDto();
postResponse.setId(post.getId());
postResponse.setTitle(post.getTitle());
postResponse.setContent(post.getContent());
postResponse.setDescription(post.getDescription());
return postResponse;
}
//DTO -> Entity
private Post mapToPost(PostDto postDto) {
Post post = new Post();
post.setTitle(postDto.getTitle());
post.setContent(postDto.getContent());
post.setDescription(postDto.getDescription());
return post;
}
@Override
public List<PostDto> getAllPosts() {
List<Post> posts = postRepository.findAll();
return posts.stream().map(post -> mapToDto(post)).collect(Collectors.toList());
}
@GetMapping
public List<PostDto> getAllPosts(){
return ? ;
}
참고
https://ikkison.tistory.com/82
https://codechacha.com/ko/java8-convert-stream-to-list/