๐Ÿฅณ NEST.JS๋กœ API ๋งŒ๋“ค๊ธฐ part 1

hwakyungChoiยท2020๋…„ 12์›” 31์ผ
0
post-thumbnail

๐Ÿ˜Ž ๊ฐ•์˜ ํ›„๊ธฐ ๋ฐ ๋ฐฐ์šด์ 

  • NEST.JS๋ž€ Node.js ์˜ ํ”„๋ ˆ์ž„์›Œํฌ๋กœ express ์œ„์— ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ๋‹ค
  • ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ํ•จ์ˆ˜ ๋ฐ˜์‘ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์š”์†Œ๋„ ์ผ๋ถ€๋ถ„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ ์•„ํ‚คํ…์ณ์™€ ๊ตฌ์กฐ๋ฅผ ์ œ๊ณตํ•ด์ค€๋‹ค.
  • Insomnia๋ฅผ ์ด์šฉํ•˜์—ฌ API ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด์•˜๋‹ค.
  • ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์˜€์œผ๋ฉฐ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ํด๋ž˜์Šค์— ํ•จ์ˆ˜ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ์Œ ex) @Get() : get ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Œ
  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๊ด€๋ฆฌํ•˜๋Š” ์„œ๋น„์Šค์™€ url ๋ผ์šฐํ„ฐ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์žˆ๋‹ค
  • Single-responsibility principle : ํ•˜๋‚˜์˜ ๋ชจ๋“ˆ, ํด๋ž˜์Šค ํ˜น์€ ํ•จ์ˆ˜์— ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ์€ ๊ผญ ์ฑ…์ž„์ ธ์•ผ ํ•จ
  • DTO ๋ฐ์ดํ„ฐ ์ „์†ก ๊ฐ์ฒด : ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค
  • ์„œ๋น„์Šค์™€ ์ปจํŠธ๋กค๋Ÿฌ ๊ฐœ๋…์ด ์กฐ๊ธˆ ๋” ํ™•๋ฆฝ๋œ ์ˆ˜์—…์ด์—ˆ๋˜ ๊ฑฐ ๊ฐ™๋‹ค.

โœ๏ธ ์ฝ”๋“œ


//movies.controller.ts
import {
    Controller,
    Get,
    Param,
    Post,
    Delete,
    Patch,
    Body,
    Query,
} from '@nestjs/common';
import { MoviesService } from './movies.service';
import { Movie } from './entities/movie.entity';
import { CreateMovieDto } from './dto/create-movie.dto';
import { UpdateMovieDto } from './dto/update-movie.dto';

//๋ผ์šฐํ„ฐ
@Controller('movies')
export class MoviesController {
    constructor(private readonly moviesService: MoviesService) { }

    @Get()
    getAll(): Movie[] {
        return this.moviesService.getAll();
    }

    @Get(":id")
    getOne(@Param('id') movieId: number): Movie {
        return this.moviesService.getOne(movieId);
    }

    @Post()
    create(@Body() movieData: CreateMovieDto) {
        return this.moviesService.create(movieData);
    }

    @Delete(":id")
    remove(@Param('id') movieId: number) {
        return this.moviesService.deleteOne(movieId);
    }


    @Patch(':id')
    patch(@Param('id') movieId: number, @Body() updateData: UpdateMovieDto) {
        return this.moviesService.update(movieId, updateData);

    }
}

//movie.service.ts
import { Injectable, NotFoundException } from '@nestjs/common';
import { Movie } from './entities/movie.entity';
import { CreateMovieDto } from './dto/create-movie.dto';
import { UpdateMovieDto } from './dto/update-movie.dto';


@Injectable()
export class MoviesService {
    private movies: Movie[] = [];

    getAll(): Movie[] {
        return this.movies;
    }

    getOne(id: number): Movie {
        const movie = this.movies.find(movie => movie.id === id);
        if (!movie) {
            throw new NotFoundException(`Movie with ID ${id} not found.`);
        }
        return movie;
    }

    deleteOne(id: number) {
        this.getOne(id);
        this.movies = this.movies.filter(movie => movie.id !== id);
    }

    create(movieData: CreateMovieDto) {
        this.movies.push({
            id: this.movies.length + 1,
            ...movieData,
        });
    }
    update(id: number, updateData: UpdateMovieDto) {
        const movie = this.getOne(id);
        this.deleteOne(id);
        this.movies.push({ ...movie, ...updateData });
    }
}

//movie.entity.ts
export class Movie {
    id: number;
    title: string;
    year: number;
    genres: string[];
}

0๊ฐœ์˜ ๋Œ“๊ธ€