이번 강의에서는 응답 DTO에 작성자 정보를 명시적으로 포함하도록 구조를 변경했다.
엔티티에 author(Member)가 연결된 상태에서,
API 응답에서도 “누가 작성했는지”를 명확하게 내려주기 위한 작업이다.
이전 단계까지는 다음과 같은 한계가 있었다.
👉 엔티티에는 있지만, 응답에는 없는 정보였다.
PostDto에 작성자 정보 추가authorIdauthorNamePostCommentDto에 작성자 정보 추가authorIdauthorNamePostCommentDto에 댓글이 속한 글 식별을 위한postId 추가public String getName() {
return nickname;
}
public record PostDto(
int id,
LocalDateTime createDate,
LocalDateTime modifyDate,
int authorId,
String authorName,
String title,
String content
) {
public PostDto(Post post) {
this(
post.getId(),
post.getCreateDate(),
post.getModifyDate(),
post.getAuthor().getId(),
post.getAuthor().getName(),
post.getTitle(),
post.getContent()
);
}
public record PostCommentDto(
int id,
LocalDateTime createDate,
LocalDateTime modifyDate,
int authorId,
String authorName,
int postId,
String content
) {
public PostCommentDto(PostComment postComment) {
this(
postComment.getId(),
postComment.getCreateDate(),
postComment.getModifyDate(),
postComment.getAuthor().getId(),
postComment.getAuthor().getName(),
postComment.getPost().getId(),
postComment.getContent()
);
}