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 { GenreDto } from './dto/genre.dto';
|
||||
import { PaginationDto } from 'src/pagination.dto';
|
||||
|
||||
@Controller('genres')
|
||||
export class GenresController {
|
||||
constructor(private readonly genresService: GenresService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: GenreDto) {
|
||||
return this.genresService.create(dto);
|
||||
async create(@Body() dto: GenreDto) {
|
||||
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()
|
||||
findAll() {
|
||||
return this.genresService.findAll();
|
||||
async findAll(@Query() pagination: PaginationDto) {
|
||||
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')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.genresService.findOne(+id);
|
||||
@Get('/id/:id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
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')
|
||||
update(@Param('id') id: string, @Body() dto: GenreDto) {
|
||||
return this.genresService.update(+id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.genresService.remove(+id);
|
||||
@Delete('/id/:id')
|
||||
async remove(@Param('id') id: string) {
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue