From f0cf48fc873ad47bea4f7bdc884a8e82a990c8b8 Mon Sep 17 00:00:00 2001 From: Toast Date: Thu, 23 Jan 2025 10:01:47 +0100 Subject: [PATCH 1/4] Shows: complete schema --- src/shows/schema/show.schema.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/shows/schema/show.schema.ts b/src/shows/schema/show.schema.ts index 2597c9d..3f98dc4 100644 --- a/src/shows/schema/show.schema.ts +++ b/src/shows/schema/show.schema.ts @@ -1 +1,9 @@ -export class ShowSchema {} +import { Schema } from 'mongoose'; + +export const ShowSchema = new Schema({ + title: { type: String, required: true }, + year: { type: Number, required: true }, + seasons: { type: Number, required: true }, + description: { type: String, required: true }, + genres: [{ type: String, required: true }], +}); From 00d57b4feb9844811d951061f817883e5fd99db4 Mon Sep 17 00:00:00 2001 From: Toast Date: Thu, 23 Jan 2025 10:34:29 +0100 Subject: [PATCH 2/4] Shows: complete service --- src/shows/shows.service.ts | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/shows/shows.service.ts b/src/shows/shows.service.ts index f433f4c..7b38930 100644 --- a/src/shows/shows.service.ts +++ b/src/shows/shows.service.ts @@ -1,25 +1,36 @@ import { Injectable } from '@nestjs/common'; import { ShowDto } from './dto/show.dto'; +import { InjectModel } from '@nestjs/mongoose'; +import { Show } from './entities/show.entity'; +import { Model } from 'mongoose'; @Injectable() export class ShowsService { - create(dto: ShowDto) { - return 'This action adds a new show'; + constructor(@InjectModel('Show') private showModel: Model) {} + + async create(dto: ShowDto): Promise { + const show = new this.showModel(dto); + return show.save(); } - findAll() { - return `This action returns all shows`; + async findAll(): Promise { + return this.showModel.find(); } - findOne(id: number) { - return `This action returns a #${id} show`; + async findId(id: string): Promise { + return this.showModel.findById(id); } - update(id: number, dto: ShowDto) { - return `This action updates a #${id} show`; + async find(name: string) { + const regex = new RegExp(name, 'i'); + return this.showModel.find({ title: { $regex: regex } }); } - remove(id: number) { - return `This action removes a #${id} show`; + async update(id: string, dto: ShowDto): Promise { + return this.showModel.findByIdAndUpdate(id, { $set: dto }, { new: true }); + } + + async remove(id: string): Promise { + return this.showModel.findByIdAndDelete(id); } } From 66778f994405687a231d884501caa0463fe21d7c Mon Sep 17 00:00:00 2001 From: Toast Date: Fri, 24 Jan 2025 10:16:06 +0100 Subject: [PATCH 3/4] Shows/service: rename find function to search --- src/shows/shows.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shows/shows.service.ts b/src/shows/shows.service.ts index 7b38930..0c28442 100644 --- a/src/shows/shows.service.ts +++ b/src/shows/shows.service.ts @@ -21,7 +21,7 @@ export class ShowsService { return this.showModel.findById(id); } - async find(name: string) { + async search(name: string) { const regex = new RegExp(name, 'i'); return this.showModel.find({ title: { $regex: regex } }); } From 82e1a6258fc7704395233c23c79050204dbd1992 Mon Sep 17 00:00:00 2001 From: Toast Date: Fri, 24 Jan 2025 10:17:42 +0100 Subject: [PATCH 4/4] Shows: complete controller --- src/shows/shows.controller.ts | 136 ++++++++++++++++++++++++++++++---- 1 file changed, 122 insertions(+), 14 deletions(-) diff --git a/src/shows/shows.controller.ts b/src/shows/shows.controller.ts index 5b5a2ee..592d0b0 100644 --- a/src/shows/shows.controller.ts +++ b/src/shows/shows.controller.ts @@ -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, + }); + } + } } }