Untitled

필요한 정보

BoardId

title

content

BoardUserId

username

{

replyId

comment

replyUserId

replyUsername

}

{
	"id":1,
	"title":"제목1",
	"content":"내용1",
	"id":1,
	"username":"ssar"
	"replies" : {
	    { 
	      "id":1, 
		    "comment":"댓글1", 
	      "userid":2,
	      "username":"cos"
	    },
		  {
		   "id":2, 
	      "comment":"댓글2", 
	      "user" : {
	         "id":3,
	         "username":"love"  
	         }
	      }
	   }
	}

화면에 안보이더라도 id는 무조건 줘야함

dto는 자주 바뀔 수 있다. (프론트의 요청에 의해서)

보더 리스폰스에 dto 만듬

@Data
public static class DetailDTO {
    private int id;
    private String title;
    private String content;
    private int userId; // 게시글 작성자 아이디
    private String username; // 게시글 작성자 이름
    private boolean isOwner;
    
    // 못찾으면 초기화만 시켜서 빈 배열로 만들어야 오류가 안난다.
    private List<ReplyDTO> replies = new ArrayList<>();
    

    public DetailDTO(Board board, User sessionUser) {
        this.id = board.getId();
        this.title = board.getTitle();;
        this.content = board.getContent();
        this.userId = board.getUser().getId();
        this.username = board.getUser().getUsername();
        this.isOwner = false;
        if (sessionUser != null) {
            if (sessionUser.getId() == userId) {
                this.isOwner = true;
            }
        }
        
        this.replies = board.getReplies().stream().map(reply -> new ReplyDTO(reply, sessionUser)).toList();
    }

    @Data
    public class ReplyDTO {
        private int id;
        private String comment;
        private int userId; // 댓글 작성자 아이디
        private String username; // 댓글 작성자 이름
        private boolean isOwner;

        public ReplyDTO(Reply reply, User sessionUser) {
            this.id = reply.getId();
            this.comment = reply.getComment();
            this.userId = reply.getUser().getId();
            this.username = reply.getUser().getUsername(); // 레이지 로딩 발동
            this.isOwner = false;
            if (sessionUser != null) {
                if (sessionUser.getId() == userId) {
                    this.isOwner = true;
                }
            }
        }
    }
}

보더 서비스 수정