Single-responsibility principle
- 하나의 module, class 혹은 function이 하나의 기능은 꼭 책임져야 한다.
entities/movie.entity
// 서비스로 보내고 받을 클래스(인터페이스)를 export 함
// 보통 entities에 실제로 데이터베이스의 모델을 만들어야 한다.
export class Movie {
id: number;
title: string;
year: number;
genres: string[];
}
movies.controller
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Put,
Query,
} from '@nestjs/common';
import { Movie } from './entities/movie.entity';
import { MoviesService } from './movies.service';
@Controller('movies')
export class MoviesController {
// 서비스에 접근하여 this.moviesService.getAll를 쓸 수 있다.
// constructor는 moviesService 라는 클래스를 갖게 된다.
constructor(private readonly moviesService: MoviesService) {}
// Movie[] 형태로 리턴
@Get()
getAll(): Movie[] {
// return `This will return all movies`;
return this.moviesService.getAll();
}
// localhost:3000/movies/search?year=2000
// @Get('search')
// search(@Query('year') searchingYear: string) {
// return `We are searching for a movie made after: ${searchingYear}`;
// }
/**
* 필요한 parameter는 작성하여 요청한다.
* getOne에서 요청하는 방법은 parameter를 요청하는 것
* @Param 사용시 url에 있는 id parameter를 인 것을 가리킴
*/
@Get('/:id')
getOne(@Param('id') movieId: string): Movie {
// return `This will return one movie with the id ${movieId}`;
return this.moviesService.getOne(movieId);
}
/**
* @Body 에 클라에서 보낸 movie 만들 data(movieData)를 받아옴
*/
@Post()
create(@Body() movieData) {
// console.log(movieData);
// return movieData;
return this.moviesService.create(movieData);
}
@Delete('/:id')
remove(@Param('id') movieId: string) {
// return `This will delete a movie with the id: ${movieId}`;
return this.moviesService.deleteOne(movieId);
}
@Patch('/:id')
parch(@Param('id') movieId: string, @Body() updateData) {
// return `This will patch a movie with the id: ${movieId}`;
return {
updatedMovie: movieId,
...updateData,
};
}
// search 부분이 get(':/id')보다 밑에 있으면 search를 id로 판단한다.
// get id 위로 옮겨준다.
// @Get('search')
// search() {
// return `We are searching for a movie with a title`;
// }
}
movies.service
import { Injectable } from '@nestjs/common';
import { Movie } from './entities/movie.entity';
@Injectable()
export class MoviesService {
// query 처리
private movies: Movie[] = [];
getAll(): Movie[] {
// 실제 데이터베이스는 데이터 베이스에 대한 query가 옴
return this.movies;
}
getOne(id: string): Movie {
return this.movies.find((movie) => movie.id === +id);
// string->number : parseInt(id) === +id 같은 표현
}
deleteOne(id: string): boolean {
this.movies.filter((movie) => movie.id !== +id);
return true;
}
create(movieData) {
this.movies.push({
id: this.movies.length + 1,
...movieData,
});
}
}
With a bowl of popcorn at hand and a sense of anticipation hanging in the air, I embarked on a cinematic odyssey that would transport me to realms both familiar and fantastical. From the sweeping landscapes of epic adventures to the intimate moments of human connection, Kinogoby offered a tapestry of stories woven with threads https://gidonlinehd.online/ of emotion, imagination, and boundless creativity. As I delved deeper into the vast expanse of cinematic offerings, I found myself captivated by the artistry of filmmakers who brought these stories to life, each frame a masterpiece in its own right.