@Get(":id")
getOne(@Param('id') id:number):Movie{
console.log(typeof id);
return this.movieService.getOne(id);
}
위 코드에서 log를 찍었을 시 string type으로 찍힘. 이것 때문에 url : http://localhost:3000/movies/1
로 id를 입력했는데 get으로 찾을 수 없다길래 어떤 문제인 지 몰랐음. 결론적으로 id값을 int로 type 변환을 해주고서야 성공이 됐는데 분명 type을 int로 줬는데 왜 string으로 type이 들어오는 거야?
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}));
await app.listen(process.env.PORT ?? 3000);
}
여기서 crate() → validation check → listen() 이 흐름이 이해가 안됨
지금 계속해서
Cotroller ↔ Service
Service ↔ entity 이 사이에서 dto로 validation check를 해주는 것 같다
아무튼 MVC모델을 사용하는 것 같은데 사실 이부분을 공부하다가 말아서 위 구조에 너무 취약한 상태인 것 같다.
app.useGlobalPipes(new ValidationPipe)
큰일 났다. validationPipe의 목적은 data type check하는 건 알겠는데 이걸 언제 왜 어디서 써야하는 지 흐름을 모르겠다..
살짝 OOP에 대해서 공부를 해야할 것 같은데 1주일만에 react.js랑 nest.js를 둘 다 배워야한다는게…
@Controller('movies')
export class MoviesController {
constructor(private readonly movieService: MoviesService){
}
@Get()
getAll():Movie[]{
return this.movieService.getAll();
}
@Get("search")
search(@Query('name') searchingName:string){
return `search for a movie ${searchingName}`;
}
@Get(":id")
getOne(@Param('id') id:number):Movie{
console.log(typeof id);
return this.movieService.getOne(id);
}
@Post()
create(@Body() movieData: CreateMovieDto){
return this.movieService.create(movieData);
}
@Delete(':id')
delete(@Param('id') id:number){
return this.movieService.deleteOne(id);
}
@Patch(':id')
patch(@Param('id') id:number, @Body() upData:UpdateMovieDto){
return this.movieService.update(id, upData)
}
}
@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 ID 존재하지 않음 : ${id}`);
}
return movie
}
deleteOne(id:number):boolean{
this.getOne(id);
this.movies.filter(movie => movie.id ! == +id);
return true;
}
create(movieData:CreateMovieDto):boolean{
this.movies.push({
id: this.movies.length + 1,
...movieData,
});
return true;
}
update(id:number, movieData:UpdateMovieDto):boolean{
const movie = this.getOne(id)
this.deleteOne(id);
this.movies.push({...movie, ...this.update});
return true
}
}
export class Movie{
id: number;
title: string;
year: number;
genres: string[];
}
export class CreateMovieDto{
@IsString()
readonly title: string
@IsNumber()
readonly year: number
@IsOptional()
@IsString({each: true})
readonly genres: string[]
}
export class UpdateMovieDto extends PartialType(CreateMovieDto){
}