CommentsController 개발오늘은 NestJS를 활용하여 댓글 기능을 관리하는 컨트롤러를 작성해보았다. 아래는 내가 작성한 코드의 주요 내용을 정리한 것이다.
CommentsController는 comments 관련 요청을 처리하기 위해 만들어졌다.
주요 역할은 다음과 같다:
NestJS의 데코레이터를 활용하여 요청 메서드와 라우트를 정의하였다.
@Post(':cardId')
async createComment(
@Param('cardId') cardId: number,
@Body() createCommentDto: CreateCommentDto,
) {
await this.commentsService.createComment(cardId, 1, createCommentDto.content);
}
POST /comments/:cardIdcardId)에 댓글을 생성한다.@Param('cardId'): URL에서 카드 ID를 가져온다.@Body(): 요청 Body에서 댓글 내용을 가져온다.user.id 대신 임시로 1을 사용 중이며, 나중에 사용자 인증 기능을 추가해야 한다.@Get(':cardId')
async findAllComment(@Param('cardId') cardId: number) {
return await this.commentsService.getCommentByCardId(cardId);
}
GET /comments/:cardIdcardId)에 작성된 모든 댓글을 조회한다.@Param('cardId'): URL에서 카드 ID를 가져온다.@Patch(':id')
async updateComment(
@Param('id') id: number,
@Body() updateCommentDto: UpdateCommentDto,
) {
await this.commentsService.updateComment(+id, 1, updateCommentDto.content);
}
PATCH /comments/:idid)의 내용을 수정한다.@Param('id'): URL에서 댓글 ID를 가져온다.@Body(): 요청 Body에서 수정할 내용을 가져온다.user.id)를 받아와야 한다.@Delete(':id')
async deleteComment(@Param('id') id: number) {
await this.commentsService.deleteComment(+id, 1);
}
DELETE /comments/:idid)을 삭제한다.@Param('id'): URL에서 댓글 ID를 가져온다.user.id를 받아와 인증 기능과 연동해야 한다.유저 인증 추가:
현재 user.id를 하드코딩(1)으로 사용하고 있다. 실제 사용자 정보를 받아오도록 인증 미들웨어를 추가할 필요가 있다.
@userInfo() 데코레이터 활용 가능.에러 핸들링:
응답 데이터 개선:
다음 목표: