JS/Nest.js
[Nest JS] DTO
Mini_96
2024. 8. 17. 19:12
* 문제 :
* 해결 : DTO
export class CreateBoardDto{
title:string;
description:string;
}
* 컨트롤러
//전
@Post()
createBoard( @Body('title') title:string, @Body('description') description:string, ) : Board{
return this.boardsService.createBoard(title,description);
}
//후
//생성은 post
@Post()
createBoard( @Body() createBoardDto : CreateBoardDto) : Board{
return this.boardsService.createBoard(createBoardDto);
}
@Body() 뒤에 Dto를 매개변수로 두면된다.
* 서비스
createBoard(createBoardDto : CreateBoardDto){
const {title,description} = createBoardDto;
const board = {
id : uuid(), //자동으로 유니크한 값을 넣어줌
title : title,
description : description,
status : BoardStatus.PUBLIC
}
this.boards.push(board);
return board;
}
맨앞줄에서 dto의 변수를 대입하면된다.