1
2
3
4
5
6
7
//๊ฒ์๊ธ ์ ์ฒด ์กฐํ
@Transactional
public List<AllPostResDto> getAllPost() {
List<Post> postList = postRepository.findAll();
return postList.stream()
.map(AllPostResDto::new).toList();
}
โฌ๏ธ
1
2
3
4
5
6
7
8
9
//๊ฒ์๊ธ ์ ์ฒด ์กฐํ
@Transactional
public List<AllPostResDto> getAllPost() {
List<Post> postList = postRepository.findAll();
return postList.stream()
.map((post) -> new AllPostResDto(post))
.toList();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ๊ฒ์๊ธ ์์ธ ์กฐํ
@Transactional(readOnly = true)
public PostResponseDto getOnePost(Long id) {
Post post = validateCheck.getPostIfExists(id);
// ๊ฒ์๊ธ ์์ธ ์กฐํ ์ ๋๊ธ๋ ํจ๊ป ์กฐํ
List<Comment> comments = commentRepository.findByPostId(id);
List<CommentResDto> commentList = new ArrayList<>();
for (Comment comment : comments) {
commentList.add(CommentResDto.builder()
.id(comment.getId())
.commenter(comment.getMember().getEmail())
.content(comment.getContent())
.modifiedAt(comment.getModifiedAt())
.build()
);
}
return PostResponseDto.builder()
.id(post.getId())
.title(post.getTitle())
.content(post.getContent())
.email(post.getMember().getEmail())
.modifiedAt(post.getModifiedAt())
.comments(commentList)
.build();
}
โฌ๏ธ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ๊ฒ์๊ธ ์์ธ ์กฐํ
@Transactional(readOnly = true)
public PostResponseDto getOnePost(Long id) {
Post post = validateCheck.getPostIfExists(id);
// ๊ฒ์๊ธ ์์ธ ์กฐํ ์ ๋๊ธ๋ ํจ๊ป ์กฐํ
List<CommentResDto> commentResDtos = commentRepository.findByPostId(id)
.stream()
.map(comment -> CommentResDto.builder()
.id(comment.getId())
.commenter(comment.getMember().getEmail())
.content(comment.getContent())
.modifiedAt(comment.getModifiedAt())
.build())
.toList();
return PostResponseDto.builder()
.id(post.getId())
.title(post.getTitle())
.content(post.getContent())
.email(post.getMember().getEmail())
.modifiedAt(post.getModifiedAt())
.comments(commentList)
.build();
}