Shows: complete controller
This commit is contained in:
parent
66778f9944
commit
82e1a6258f
1 changed files with 122 additions and 14 deletions
|
|
@ -3,8 +3,12 @@ import {
|
||||||
Get,
|
Get,
|
||||||
Post,
|
Post,
|
||||||
Body,
|
Body,
|
||||||
Patch,
|
|
||||||
Param,
|
Param,
|
||||||
|
BadRequestException,
|
||||||
|
InternalServerErrorException,
|
||||||
|
NotFoundException,
|
||||||
|
Query,
|
||||||
|
Put,
|
||||||
Delete,
|
Delete,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ShowsService } from './shows.service';
|
import { ShowsService } from './shows.service';
|
||||||
|
|
@ -15,27 +19,131 @@ export class ShowsController {
|
||||||
constructor(private readonly showsService: ShowsService) {}
|
constructor(private readonly showsService: ShowsService) {}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
create(@Body() dto: ShowDto) {
|
async create(@Body() dto: ShowDto) {
|
||||||
return this.showsService.create(dto);
|
try {
|
||||||
|
await this.showsService.create(dto);
|
||||||
|
return {
|
||||||
|
status: 'Ok',
|
||||||
|
message: 'Show was created successfully',
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new BadRequestException({
|
||||||
|
status: 'Bad request',
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
findAll() {
|
async findAll() {
|
||||||
return this.showsService.findAll();
|
try {
|
||||||
|
const shows = await this.showsService.findAll();
|
||||||
|
return { status: 'Ok', shows, totalShows: shows.length };
|
||||||
|
} 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.showsService.findOne(+id);
|
try {
|
||||||
|
const show = await this.showsService.findId(id);
|
||||||
|
if (show) {
|
||||||
|
return { status: 'Ok', show };
|
||||||
|
} else {
|
||||||
|
throw new NotFoundException({
|
||||||
|
status: 'Error',
|
||||||
|
message: `Can't find show with id ${id}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch(':id')
|
@Get('search')
|
||||||
update(@Param('id') id: string, @Body() dto: ShowDto) {
|
async search(@Query('query') name: string) {
|
||||||
return this.showsService.update(+id, dto);
|
try {
|
||||||
|
const shows = await this.showsService.search(name);
|
||||||
|
if (shows.length > 0) {
|
||||||
|
return { status: 'Ok', show: shows, test: shows.length };
|
||||||
|
} else {
|
||||||
|
throw new NotFoundException({
|
||||||
|
status: 'Error',
|
||||||
|
message: `Can't find show matching ${name}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':id')
|
@Put('id/:id')
|
||||||
remove(@Param('id') id: string) {
|
async update(@Param('id') id: string, @Body() dto: ShowDto) {
|
||||||
return this.showsService.remove(+id);
|
try {
|
||||||
|
const newShow = await this.showsService.update(id, dto);
|
||||||
|
if (!newShow) {
|
||||||
|
throw new NotFoundException({
|
||||||
|
status: 'Error',
|
||||||
|
message: `Can't find show with id ${id}`,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
status: 'Ok',
|
||||||
|
message: 'Movie was updated successfully',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof NotFoundException) {
|
||||||
|
throw error;
|
||||||
|
} else {
|
||||||
|
throw new InternalServerErrorException({
|
||||||
|
status: error.name,
|
||||||
|
message: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete('id/:id')
|
||||||
|
async remove(@Param('id') id: string) {
|
||||||
|
try {
|
||||||
|
const deletedMovie = await this.showsService.remove(id);
|
||||||
|
if (!deletedMovie) {
|
||||||
|
throw new NotFoundException({
|
||||||
|
status: 'Error',
|
||||||
|
message: `Can't find show with id ${id}`,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
status: 'Ok',
|
||||||
|
message: 'Movie 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