Genres: complete controller
This commit is contained in:
parent
66ccbe88e9
commit
ec1a032a35
1 changed files with 88 additions and 16 deletions
|
|
@ -1,33 +1,105 @@
|
||||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
import {
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
Post,
|
||||||
|
Body,
|
||||||
|
Param,
|
||||||
|
Delete,
|
||||||
|
BadRequestException,
|
||||||
|
Query,
|
||||||
|
InternalServerErrorException,
|
||||||
|
NotFoundException,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { GenresService } from './genres.service';
|
import { GenresService } from './genres.service';
|
||||||
import { GenreDto } from './dto/genre.dto';
|
import { GenreDto } from './dto/genre.dto';
|
||||||
|
import { PaginationDto } from 'src/pagination.dto';
|
||||||
|
|
||||||
@Controller('genres')
|
@Controller('genres')
|
||||||
export class GenresController {
|
export class GenresController {
|
||||||
constructor(private readonly genresService: GenresService) {}
|
constructor(private readonly genresService: GenresService) {}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
create(@Body() dto: GenreDto) {
|
async create(@Body() dto: GenreDto) {
|
||||||
return this.genresService.create(dto);
|
try {
|
||||||
|
const newGenre = await this.genresService.create(dto);
|
||||||
|
return {
|
||||||
|
status: 'Ok',
|
||||||
|
message: 'Genre was created successfully',
|
||||||
|
newId: newGenre._id,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new BadRequestException({
|
||||||
|
status: 'Bad request',
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
findAll() {
|
async findAll(@Query() pagination: PaginationDto) {
|
||||||
return this.genresService.findAll();
|
try {
|
||||||
|
const { page, limit } = pagination;
|
||||||
|
const serviceResponse = await this.genresService.findAll(page, limit);
|
||||||
|
return {
|
||||||
|
status: 'Ok',
|
||||||
|
genres: serviceResponse.genres,
|
||||||
|
genreCount: serviceResponse.genreCount,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get('/id/:id')
|
||||||
findOne(@Param('id') id: string) {
|
async findOne(@Param('id') id: string) {
|
||||||
return this.genresService.findOne(+id);
|
try {
|
||||||
|
const genre = await this.genresService.findId(id);
|
||||||
|
if (genre) {
|
||||||
|
return { status: 'Ok', genre: genre };
|
||||||
|
} else {
|
||||||
|
throw new NotFoundException({
|
||||||
|
status: 'Error',
|
||||||
|
message: `Can't find genre with id ${id}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch(':id')
|
@Delete('/id/:id')
|
||||||
update(@Param('id') id: string, @Body() dto: GenreDto) {
|
async remove(@Param('id') id: string) {
|
||||||
return this.genresService.update(+id, dto);
|
try {
|
||||||
|
const deletedMovie = await this.genresService.remove(id);
|
||||||
|
if (!deletedMovie) {
|
||||||
|
throw new NotFoundException({
|
||||||
|
status: 'Error',
|
||||||
|
message: `Can't find genre with id ${id}`,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
status: 'Ok',
|
||||||
|
message: 'Genre removed successfully',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
throw error;
|
||||||
|
} else {
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':id')
|
|
||||||
remove(@Param('id') id: string) {
|
|
||||||
return this.genresService.remove(+id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue