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
1import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common'2import { PostsService } from './posts.service'34@Controller('posts')5export class PostsController {6constructor(private readonly postsService: PostsService) {}78/*** 1) GET /posts9* ๋ชจ๋ post๋ฅผ ๋ค ๊ฐ์ ธ์จ๋ค10*/11@Get()12getPosts() {13return this.postsService.getAllPosts()14}1516/*** 2) GET /posts/:id17* id์ ํด๋นํ๋ post๋ฅผ ๊ฐ์ ธ์จ๋ค18* e.g. 11์ด๋ผ๋ ID๋ฅผ ๊ฐ๊ณ ์๋ Post ํ๋๋ฅผ ๊ฐ์ ธ์จ๋ค.19*/20// @Param('id') ๋ป : ๊ฐ์ ธ์ค๋ ํ๋ผ๋ฏธํฐ์ ์ด๋ฆ์ id์ด๋ค21@Get(':id')22getPost(@Param('id') id: string) {23return this.postsService.getPostById(+id)24}2526/*** 3) POST /posts27* post๋ฅผ ์์ฑํ๋ค28*/29@Post()30postPosts(31@Body('author') author: string, //32@Body('title') title: string,33@Body('content') content: string,34) {35return this.postsService.createPost(author, title, content)36}3738/*** 4) PATCH /posts/:id39* id์ ํด๋นํ๋ post๋ฅผ ๋ถ๋ถ ๋ณ๊ฒฝํ๋ค40*/41@Patch(':id')42putPost(43@Param('id') id: string, //44@Body('author') author?: string, //45@Body('title') title?: string,46@Body('content') content?: string,47) {48return this.postsService.updatePost(+id, author, title, content)49}5051/*** 5) DELETE /posts/:id52* id์ ํด๋นํ๋ post๋ฅผ ์ญ์ ํ๋ค53*/54@Delete(':id')55deletePost(@Param('id') id: string) {56return this.postsService.deletePost(+id)57}58}
posts.service.ts
1import { Injectable, NotFoundException } from '@nestjs/common'23export interface PostModel {4id: number5author: string6title: string7content: string8likeCount: number9commentCount: number10}1112let posts: PostModel[] = [13{14id: 1,15author: 'newjeans_official',16title: '๋ด์ง์ค ๋ฏผ์ง',17content: '๋ฉ์ดํฌ์ ๊ณ ์น๊ณ ์๋ ๋ฏผ์ง',18likeCount: 100000,19commentCount: 999999,20},21{22id: 2,23author: 'newjeans_official',24title: '๋ด์ง์ค ํ๋ฆฐ',25content: '๋ ธ๋ ์ฐ์ตํ๊ณ ์๋ ํ๋ฆฐ',26likeCount: 100000,27commentCount: 999999,28},29{30id: 3,31author: 'blackpink_official',32title: '๋ธ๋ํํฌ ๋ก์ ',33content: '๊ณต์ฐ์ค์ธ ๋ก์ ',34likeCount: 100000,35commentCount: 999999,36},37]3839@Injectable()40export class PostsService {41getAllPosts() {42return posts43}4445getPostById(id: number) {46const post = posts.find(post => post.id === +id)4748if (!post) {49throw new NotFoundException()50}5152return post53}5455createPost(author: string, title: string, content: string) {56const post = {57id: posts[posts.length - 1].id + 1,58author,59title,60content,61likeCount: 0,62commentCount: 0,63}6465posts = [...posts, post]6667return post68}6970updatePost(postId: number, author: string, title: string, content: string) {71const post = posts.find(post => post.id === postId)7273if (!post) {74throw new NotFoundException()75}76if (author) {77post.author = author78}79if (title) {80post.title = title81}82if (content) {83post.content = content84}85posts = posts.map(prevPost => (prevPost.id === postId ? post : prevPost))8687return post88}8990deletePost(postId: number) {91const post = posts.find(post => post.id === postId)92if (!post) {93throw new NotFoundException()94}9596posts = posts.filter(post => post.id !== postId)97return postId98}99}