๐ŸŽ‰ berenickt ๋ธ”๋กœ๊ทธ์— ์˜จ ๊ฑธ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ๐ŸŽ‰
Back
NestJs
04-Service

1. Service ํŒŒ์ผ์˜ ์—ญํ• 

1
์œ ์ €(client)๊ฐ€ ์š”์ฒญ --> controller ---> service
  • controller : ๊ฐ€์žฅ ๋งจ ์•ž์—์„œ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›๋Š” ์—ญํ• 
    • ๋ฐ›์€ ์š”์ฒญ์„ service์—์„œ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜๋กœ ๋ผ์šฐํŒ…๋งŒ ํ•ด์ฃผ๋Š” ์—ญํ• 
    • controller๋Š” ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๋งŽ์€ ๊ฒƒ๋“ค์ด ํ•„์š”ํ•˜๊ธฐ์—,
      • ์ด ๋งŽ์€๊ฒƒ๋“ค์„ ๋ชจ๋‘ controller์—์„œ ๊ตฌํ˜„ํ•˜๊ธฐ ๋ณด๋‹ค, ๊ทธ ๊ธฐ๋Šฅ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ์ชผ๊ฐœ์„œ service ๋‹จ์œ„๋กœ ๋ถ„ํ• ํ•œ๋‹ค.
    • ๋ถ„ํ• ๋œ ์„œ๋น„์Šค๋‚˜ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ, ํŒฉํ† ๋ฆฌ, ํ—ฌํผ ๋“ฑ์„ Provider๋ผ๊ณ  ํ•˜๋ฉฐ, NestJS์—์„œ๋Š” service๋ฅผ Provider๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
      • ์ด๋Ÿฐ Provider๋“ค์„ controller์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
      • Provider๋Š” moduleํŒŒ์ผ์—์„œ ๋“ฑ๋ก ํ›„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํžˆ๋””.
  • module : module์€ ๋ฐ€์ ‘ํ•˜๊ฒŒ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ์˜ ์ง‘ํ•ฉ
    • ์ผ๋ฐ˜์ ์œผ๋กœ module์€ ๊ธฐ๋Šฅ๋ณ„๋กœ ๋งŒ๋“ค์–ด์ง (e.g. user, chat, order, โ€ฆ)
    • ๊ฐ™์€ ๊ธฐ๋Šฅ์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ๋“ค์€ ํ•˜๋‚˜์˜ module ํด๋” ์•ˆ์— ๋„ฃ์–ด์„œ ์‚ฌ์šฉ
      • UserController, UserService, UserEntity โ€ฆ๊ฐ™์€ ๊ฒƒ์€ User์— ๊ด€ํ•œ ๊ธฐ๋Šฅ์ด๊ธฐ์— userModule ์•ˆ์— ๋„ฃ์–ด์คŒ
    • module์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹ฑ๊ธ€ ํ†ค์ด๋ฏ€๋กœ ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ๊ฐ„์— ์‰ฝ๊ฒŒ ์ž์›์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Œ
  • service : ๊ฐ ์š”์ฒญ์˜ ๊ธฐ๋Šฅ๋“ค์˜ ๋…ผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ถ€๋ถ„
    • e.g. DB์•ˆ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜, ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ์‹œ, ์ƒ์„ฑํ•œ ๊ฒŒ์‹œํŒ ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๋“ฑ์˜ ๋กœ์ง์„ ์ฒ˜๋ฆฌ

2. Service๋กœ ๋ชจ๋“  ๋กœ์ง ์˜ฎ๊ธฐ๊ธฐ

posts.controller.ts
1
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common'
2
import { PostsService } from './posts.service'
3
4
@Controller('posts')
5
export class PostsController {
6
constructor(private readonly postsService: PostsService) {}
7
8
/*** 1) GET /posts
9
* ๋ชจ๋“  post๋ฅผ ๋‹ค ๊ฐ€์ ธ์˜จ๋‹ค
10
*/
11
@Get()
12
getPosts() {
13
return this.postsService.getAllPosts()
14
}
15
16
/*** 2) GET /posts/:id
17
* id์— ํ•ด๋‹นํ•˜๋Š” post๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
18
* e.g. 11์ด๋ผ๋Š” ID๋ฅผ ๊ฐ–๊ณ ์žˆ๋Š” Post ํ•˜๋‚˜๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
19
*/
20
// @Param('id') ๋œป : ๊ฐ€์ ธ์˜ค๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ์ด๋ฆ„์€ id์ด๋‹ค
21
@Get(':id')
22
getPost(@Param('id') id: string) {
23
return this.postsService.getPostById(+id)
24
}
25
26
/*** 3) POST /posts
27
* post๋ฅผ ์ƒ์„ฑํ•œ๋‹ค
28
*/
29
@Post()
30
postPosts(
31
@Body('author') author: string, //
32
@Body('title') title: string,
33
@Body('content') content: string,
34
) {
35
return this.postsService.createPost(author, title, content)
36
}
37
38
/*** 4) PATCH /posts/:id
39
* id์— ํ•ด๋‹นํ•˜๋Š” post๋ฅผ ๋ถ€๋ถ„ ๋ณ€๊ฒฝํ•œ๋‹ค
40
*/
41
@Patch(':id')
42
putPost(
43
@Param('id') id: string, //
44
@Body('author') author?: string, //
45
@Body('title') title?: string,
46
@Body('content') content?: string,
47
) {
48
return this.postsService.updatePost(+id, author, title, content)
49
}
50
51
/*** 5) DELETE /posts/:id
52
* id์— ํ•ด๋‹นํ•˜๋Š” post๋ฅผ ์‚ญ์ œํ•œ๋‹ค
53
*/
54
@Delete(':id')
55
deletePost(@Param('id') id: string) {
56
return this.postsService.deletePost(+id)
57
}
58
}
posts.service.ts
1
import { Injectable, NotFoundException } from '@nestjs/common'
2
3
export interface PostModel {
4
id: number
5
author: string
6
title: string
7
content: string
8
likeCount: number
9
commentCount: number
10
}
11
12
let posts: PostModel[] = [
13
{
14
id: 1,
15
author: 'newjeans_official',
16
title: '๋‰ด์ง„์Šค ๋ฏผ์ง€',
17
content: '๋ฉ”์ดํฌ์—… ๊ณ ์น˜๊ณ  ์žˆ๋Š” ๋ฏผ์ง€',
18
likeCount: 100000,
19
commentCount: 999999,
20
},
21
{
22
id: 2,
23
author: 'newjeans_official',
24
title: '๋‰ด์ง„์Šค ํ˜œ๋ฆฐ',
25
content: '๋…ธ๋ž˜ ์—ฐ์Šตํ•˜๊ณ  ์žˆ๋Š” ํ˜œ๋ฆฐ',
26
likeCount: 100000,
27
commentCount: 999999,
28
},
29
{
30
id: 3,
31
author: 'blackpink_official',
32
title: '๋ธ”๋ž™ํ•‘ํฌ ๋กœ์ œ',
33
content: '๊ณต์—ฐ์ค‘์ธ ๋กœ์ œ',
34
likeCount: 100000,
35
commentCount: 999999,
36
},
37
]
38
39
@Injectable()
40
export class PostsService {
41
getAllPosts() {
42
return posts
43
}
44
45
getPostById(id: number) {
46
const post = posts.find(post => post.id === +id)
47
48
if (!post) {
49
throw new NotFoundException()
50
}
51
52
return post
53
}
54
55
createPost(author: string, title: string, content: string) {
56
const post = {
57
id: posts[posts.length - 1].id + 1,
58
author,
59
title,
60
content,
61
likeCount: 0,
62
commentCount: 0,
63
}
64
65
posts = [...posts, post]
66
67
return post
68
}
69
70
updatePost(postId: number, author: string, title: string, content: string) {
71
const post = posts.find(post => post.id === postId)
72
73
if (!post) {
74
throw new NotFoundException()
75
}
76
if (author) {
77
post.author = author
78
}
79
if (title) {
80
post.title = title
81
}
82
if (content) {
83
post.content = content
84
}
85
posts = posts.map(prevPost => (prevPost.id === postId ? post : prevPost))
86
87
return post
88
}
89
90
deletePost(postId: number) {
91
const post = posts.find(post => post.id === postId)
92
if (!post) {
93
throw new NotFoundException()
94
}
95
96
posts = posts.filter(post => post.id !== postId)
97
return postId
98
}
99
}