03-Query-Parameters
1. REST API 세트
- [GET] https://localhost:3000/posts
- 다수의 Post를 가져온다.
- Query 사용
- [GET] https://localhost:3000/posts/11
- 11이라는 ID를 갖고있는 Post 하나를 가져온다.
- Query 사용
- [POST] https://localhost:3000/posts
- 새로운 POST를 생성한다.
- Body 사용
- [PATCH] https://localhost:3000/posts/8
- 8이라는 ID를 갖고있는 Post를 부분 변경한다.
- Body 사용
- [PUT] https://localhost:3000/posts/8
- 8이라는 ID를 갖고있는 Post를 변경하거나 생성한다.
- Body 사용
- [DELETE] https://localhost:3000/posts/3
- 3이라는 ID를 갖고있는 Post를 삭제한다.
- Body 사용
보통 put보다 patch를 주로 쓰긴 한다.
2. Get Posts 구현
posts.controller.ts
1import { Controller, Get } from '@nestjs/common'2import { PostsService } from './posts.service'34interface PostModel {5id: number6author: string7title: string8content: string9likeCount: number10commentCount: number11}1213let posts: PostModel[] = [14{15id: 1,16author: 'newjeans_official',17title: '뉴진스 민지',18content: '메이크업 고치고 있는 민지',19likeCount: 100000,20commentCount: 999999,21},22{23id: 2,24author: 'newjeans_official',25title: '뉴진스 혜린',26content: '노래 연습하고 있는 혜린',27likeCount: 100000,28commentCount: 999999,29},30{31id: 3,32author: 'blackpink_official',33title: '블랙핑크 로제',34content: '공연중인 로제',35likeCount: 100000,36commentCount: 999999,37},38]3940@Controller('posts')41export class PostsController {42constructor(private readonly postsService: PostsService) {}4344/*** 1) GET /posts45* 모든 post를 다 가져온다46*/47@Get()48getPosts() {49return posts50}5152/*** 2) GET /posts/:id53* id에 해당하는 post를 가져온다54* e.g. 11이라는 ID를 갖고있는 Post 하나를 가져온다.55*/5657/*** 3) POST /posts58* post를 생성한다59*/6061/*** 4) PATCH /posts/:id62* id에 해당하는 post를 부분 변경한다63*/64/*** 5) DELETE /posts/:id65* id에 해당하는 post를 삭제한다66*/67}
3. ID Param을 이용한 Post 조회
posts.controller.ts
1// posts.controller.ts 생략2/*** 2) GET /posts/:id3* id에 해당하는 post를 가져온다4* e.g. 11이라는 ID를 갖고있는 Post 하나를 가져온다.5*/6// @Param('id') 뜻 : 가져오는 파라미터의 이름은 id이다7@Get(':id')8getPost(@Param('id') id: string) {9return posts.find(post => post.id === +id)10}
4. Not Found Exception 던지기
posts.controller.ts
1// posts.controller.ts 생략2/*** 2) GET /posts/:id3* id에 해당하는 post를 가져온다4* e.g. 11이라는 ID를 갖고있는 Post 하나를 가져온다.5*/6// @Param('id') 뜻 : 가져오는 파라미터의 이름은 id이다7@Get(':id')8getPost(@Param('id') id: string) {9const post = posts.find(post => post.id === +id)1011if (!post) {12throw new NotFoundException()13}1415return post16}
5. 기본 제공되는 Exception들
- https://docs.nestjs.com/exception-filters#built-in-http-exceptions
- NestJS에서 기본제공해주는 Exception들
- 자주 쓰는 건 위에 맨 위 4개이다.
BadRequestExceptionUnauthorizedExceptionNotFoundExceptionForbiddenException
6. Post 요청 만들기
posts.controller.ts
1// posts.controller.ts 생략2/*** 3) POST /posts3* post를 생성한다4*/5@Post()6postPosts(7@Body('author') author: string, //8@Body('title') title: string,9@Body('content') content: string,10) {11const post = {12id: posts[posts.length - 1].id + 1,13author,14title,15content,16likeCount: 0,17commentCount: 0,18}1920posts = [...posts, post]2122return post23}
7. Patch Post 엔드포인트 생성
posts.controller.ts
1// posts.controller.ts 생략2/*** 4) PATCH /posts/:id3* id에 해당하는 post를 부분 변경한다4*/5@Patch(':id')6putPost(7@Param('id') id: string, //8@Body('author') author?: string, //9@Body('title') title?: string,10@Body('content') content?: string,11) {12const post = posts.find(post => post.id === +id)1314if (!post) {15throw new NotFoundException()16}17if (author) {18post.author = author19}20if (title) {21post.title = title22}23if (content) {24post.content = content25}26posts = posts.map(prevPost => (prevPost.id === +id ? post : prevPost))2728return post29}
8. Delete 엔드포인트 생성
posts.controller.ts
1// posts.controller.ts 생략2/*** 5) DELETE /posts/:id3* id에 해당하는 post를 삭제한다4*/5@Delete(':id')6deletePost(@Param('id') id: string) {7const post = posts.find(post => post.id === +id)8if (!post) {9throw new NotFoundException()10}1112posts = posts.filter(post => post.id !== +id)13return id14}