@AllArgsConstructor
@Builder
생성자 이름 from (매개변수) {
return 생성자이름.builder()
.필드이름()
.필드이름()
.build();
}
ex)
// builder 사용했을 때
public BoardListResponseDto from (Board board) {
return BoardListResponseDto.builder()
.title(board.getTitle())
.createdAt(board.getCreatedAt())
.modifiedAt(board.getModifiedAt())
.username(board.getAccount().getUsername())
.build();
}
// 기존 생성자를 사용했을 때
public BoardListResponseDto(Board board) {
this.title = board.getTitle();
this.createdAt = board.getModifiedAt();
this.modifiedAt = board.getCreatedAt();
this.username = board.getAccount().getUsername();
}
boardService.java
// builder 사용 글 생성
public BoardResponseDto createBoard(BoardRequestDto requestDto, Account account) {
Board board = new Board(requestDto, account);
boardRepository.save(board);
return new BoardResponseDto().from(board);
}
// 기본 생성자 사용 글 생성
public BoardResponseDto createBoard(BoardRequestDto requestDto, Account account) {
Board board = new Board(requestDto, account);
boardRepository.save(board);
return new BoardResponseDto(board);
}