๐ŸŽ‰ berenickt ๋ธ”๋กœ๊ทธ์— ์˜จ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๐ŸŽ‰
Back
NestJs
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
1
import { Controller, Get } from '@nestjs/common'
2
import { PostsService } from './posts.service'
3
4
interface PostModel {
5
id: number
6
author: string
7
title: string
8
content: string
9
likeCount: number
10
commentCount: number
11
}
12
13
let posts: PostModel[] = [
14
{
15
id: 1,
16
author: 'newjeans_official',
17
title: '๋‰ด์ง„์Šค ๋ฏผ์ง€',
18
content: '๋ฉ”์ดํฌ์—… ๊ณ ์น˜๊ณ  ์žˆ๋Š” ๋ฏผ์ง€',
19
likeCount: 100000,
20
commentCount: 999999,
21
},
22
{
23
id: 2,
24
author: 'newjeans_official',
25
title: '๋‰ด์ง„์Šค ํ˜œ๋ฆฐ',
26
content: '๋…ธ๋ž˜ ์—ฐ์Šตํ•˜๊ณ  ์žˆ๋Š” ํ˜œ๋ฆฐ',
27
likeCount: 100000,
28
commentCount: 999999,
29
},
30
{
31
id: 3,
32
author: 'blackpink_official',
33
title: '๋ธ”๋ž™ํ•‘ํฌ ๋กœ์ œ',
34
content: '๊ณต์—ฐ์ค‘์ธ ๋กœ์ œ',
35
likeCount: 100000,
36
commentCount: 999999,
37
},
38
]
39
40
@Controller('posts')
41
export class PostsController {
42
constructor(private readonly postsService: PostsService) {}
43
44
/*** 1) GET /posts
45
* ๋ชจ๋“  post๋ฅผ ๋‹ค ๊ฐ€์ ธ์˜จ๋‹ค
46
*/
47
@Get()
48
getPosts() {
49
return posts
50
}
51
52
/*** 2) GET /posts/:id
53
* id์— ํ•ด๋‹นํ•˜๋Š” post๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
54
* e.g. 11์ด๋ผ๋Š” ID๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” Post ํ•˜๋‚˜๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
55
*/
56
57
/*** 3) POST /posts
58
* post๋ฅผ ์ƒ์„ฑํ•œ๋‹ค
59
*/
60
61
/*** 4) PATCH /posts/:id
62
* id์— ํ•ด๋‹นํ•˜๋Š” post๋ฅผ ๋ถ€๋ถ„ ๋ณ€๊ฒฝํ•œ๋‹ค
63
*/
64
/*** 5) DELETE /posts/:id
65
* id์— ํ•ด๋‹นํ•˜๋Š” post๋ฅผ ์‚ญ์ œํ•œ๋‹ค
66
*/
67
}

3. ID Param์„ ์ด์šฉํ•œ Post ์กฐํšŒ

posts.controller.ts
1
// posts.controller.ts ์ƒ๋žต
2
/*** 2) GET /posts/:id
3
* id์— ํ•ด๋‹นํ•˜๋Š” post๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
4
* e.g. 11์ด๋ผ๋Š” ID๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” Post ํ•˜๋‚˜๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
5
*/
6
// @Param('id') ๋œป : ๊ฐ€์ ธ์˜ค๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ด๋ฆ„์€ id์ด๋‹ค
7
@Get(':id')
8
getPost(@Param('id') id: string) {
9
return posts.find(post => post.id === +id)
10
}

4. Not Found Exception ๋˜์ง€๊ธฐ

posts.controller.ts
1
// posts.controller.ts ์ƒ๋žต
2
/*** 2) GET /posts/:id
3
* id์— ํ•ด๋‹นํ•˜๋Š” post๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
4
* e.g. 11์ด๋ผ๋Š” ID๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” Post ํ•˜๋‚˜๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
5
*/
6
// @Param('id') ๋œป : ๊ฐ€์ ธ์˜ค๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ด๋ฆ„์€ id์ด๋‹ค
7
@Get(':id')
8
getPost(@Param('id') id: string) {
9
const post = posts.find(post => post.id === +id)
10
11
if (!post) {
12
throw new NotFoundException()
13
}
14
15
return post
16
}

5. ๊ธฐ๋ณธ ์ œ๊ณต๋˜๋Š” Exception๋“ค

  • https://docs.nestjs.com/exception-filters#built-in-http-exceptions
  • NestJS์—์„œ ๊ธฐ๋ณธ์ œ๊ณตํ•ด์ฃผ๋Š” Exception๋“ค
  • ์ž์ฃผ ์“ฐ๋Š” ๊ฑด ์œ„์— ๋งจ ์œ„ 4๊ฐœ์ด๋‹ค.
    • BadRequestException
    • UnauthorizedException
    • NotFoundException
    • ForbiddenException

6. Post ์š”์ฒญ ๋งŒ๋“ค๊ธฐ

posts.controller.ts
1
// posts.controller.ts ์ƒ๋žต
2
/*** 3) POST /posts
3
* post๋ฅผ ์ƒ์„ฑํ•œ๋‹ค
4
*/
5
@Post()
6
postPosts(
7
@Body('author') author: string, //
8
@Body('title') title: string,
9
@Body('content') content: string,
10
) {
11
const post = {
12
id: posts[posts.length - 1].id + 1,
13
author,
14
title,
15
content,
16
likeCount: 0,
17
commentCount: 0,
18
}
19
20
posts = [...posts, post]
21
22
return post
23
}

7. Patch Post ์—”๋“œํฌ์ธํŠธ ์ƒ์„ฑ

posts.controller.ts
1
// posts.controller.ts ์ƒ๋žต
2
/*** 4) PATCH /posts/:id
3
* id์— ํ•ด๋‹นํ•˜๋Š” post๋ฅผ ๋ถ€๋ถ„ ๋ณ€๊ฒฝํ•œ๋‹ค
4
*/
5
@Patch(':id')
6
putPost(
7
@Param('id') id: string, //
8
@Body('author') author?: string, //
9
@Body('title') title?: string,
10
@Body('content') content?: string,
11
) {
12
const post = posts.find(post => post.id === +id)
13
14
if (!post) {
15
throw new NotFoundException()
16
}
17
if (author) {
18
post.author = author
19
}
20
if (title) {
21
post.title = title
22
}
23
if (content) {
24
post.content = content
25
}
26
posts = posts.map(prevPost => (prevPost.id === +id ? post : prevPost))
27
28
return post
29
}

8. Delete ์—”๋“œํฌ์ธํŠธ ์ƒ์„ฑ

posts.controller.ts
1
// posts.controller.ts ์ƒ๋žต
2
/*** 5) DELETE /posts/:id
3
* id์— ํ•ด๋‹นํ•˜๋Š” post๋ฅผ ์‚ญ์ œํ•œ๋‹ค
4
*/
5
@Delete(':id')
6
deletePost(@Param('id') id: string) {
7
const post = posts.find(post => post.id === +id)
8
if (!post) {
9
throw new NotFoundException()
10
}
11
12
posts = posts.filter(post => post.id !== +id)
13
return id
14
}