Skip to Content
BackendNestJs03-Query-Parameters

1. REST API 세트

보통 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들


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
}