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}