Shows: complete controller

This commit is contained in:
Toast 2025-01-24 10:17:42 +01:00
parent 66778f9944
commit 82e1a6258f

View file

@ -3,8 +3,12 @@ import {
Get,
Post,
Body,
Patch,
Param,
BadRequestException,
InternalServerErrorException,
NotFoundException,
Query,
Put,
Delete,
} from '@nestjs/common';
import { ShowsService } from './shows.service';
@ -15,27 +19,131 @@ export class ShowsController {
constructor(private readonly showsService: ShowsService) {}
@Post()
create(@Body() dto: ShowDto) {
return this.showsService.create(dto);
async create(@Body() dto: ShowDto) {
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()
findAll() {
return this.showsService.findAll();
async 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')
findOne(@Param('id') id: string) {
return this.showsService.findOne(+id);
@Get('/id/:id')
async findOne(@Param('id') id: string) {
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')
update(@Param('id') id: string, @Body() dto: ShowDto) {
return this.showsService.update(+id, dto);
@Get('search')
async search(@Query('query') name: string) {
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')
remove(@Param('id') id: string) {
return this.showsService.remove(+id);
@Put('id/:id')
async update(@Param('id') id: string, @Body() dto: ShowDto) {
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,
});
}
}
}
}