From a3dd007bde1948b158c7ba1ee4eee869ef1d732d Mon Sep 17 00:00:00 2001 From: Toast Date: Fri, 7 Feb 2025 11:16:21 +0100 Subject: [PATCH 1/4] Genres: complete service --- src/genres/genres.service.ts | 37 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/genres/genres.service.ts b/src/genres/genres.service.ts index 37f1a66..5d73ab3 100644 --- a/src/genres/genres.service.ts +++ b/src/genres/genres.service.ts @@ -1,25 +1,38 @@ import { Injectable } from '@nestjs/common'; import { GenreDto } from './dto/genre.dto'; +import { InjectModel } from '@nestjs/mongoose'; +import { Genre } from './entities/genre.entity'; +import { Model } from 'mongoose'; @Injectable() export class GenresService { - create(dto: GenreDto) { - return 'This action adds a new genre'; + constructor(@InjectModel('Genre') private genreModel: Model) {} + + async create(dto: GenreDto): Promise { + const genre = new this.genreModel(dto); + return genre.save(); } - findAll() { - return `This action returns all genres`; + async findAll(page: number, genresPerPage: number): Promise { + let genres; + if (genresPerPage !== undefined) { + const skip = (page - 1) * genresPerPage; + genres = await this.genreModel.find().skip(skip).limit(genresPerPage); + } else { + genres = await this.genreModel.find(); + } + const total = await this.genreModel.countDocuments(); + return { + genres: genres, + genreCount: total, + }; } - findOne(id: number) { - return `This action returns a #${id} genre`; + async findId(id: string): Promise { + return this.genreModel.findById(id); } - update(id: number, dto: GenreDto) { - return `This action updates a #${id} genre`; - } - - remove(id: number) { - return `This action removes a #${id} genre`; + async remove(id: string): Promise { + return this.genreModel.findByIdAndDelete(id); } } From 66ccbe88e961113f7f9cfe247124c1566655367a Mon Sep 17 00:00:00 2001 From: Toast Date: Fri, 7 Feb 2025 13:54:42 +0100 Subject: [PATCH 2/4] Genres/schema: fix spelling --- src/genres/schema/genres.schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/genres/schema/genres.schema.ts b/src/genres/schema/genres.schema.ts index 2fb697f..c1ee57d 100644 --- a/src/genres/schema/genres.schema.ts +++ b/src/genres/schema/genres.schema.ts @@ -2,5 +2,5 @@ import { Schema } from 'mongoose'; export const GenreSchema = new Schema({ name: { type: String, required: true }, - showsIDs: [{ type: Schema.Types.ObjectId, ref: 'Show' }], + showIDs: [{ type: Schema.Types.ObjectId, ref: 'Show' }], }); From ec1a032a357835e99cc6f65779c81164d33c7a5f Mon Sep 17 00:00:00 2001 From: Toast Date: Fri, 7 Feb 2025 13:58:55 +0100 Subject: [PATCH 3/4] Genres: complete controller --- src/genres/genres.controller.ts | 104 +++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 16 deletions(-) diff --git a/src/genres/genres.controller.ts b/src/genres/genres.controller.ts index 55dd15c..3df4f22 100644 --- a/src/genres/genres.controller.ts +++ b/src/genres/genres.controller.ts @@ -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, + }); + } + } } } From 7093a76d177778e21cbc1ad85708d2d7f4c91bdf Mon Sep 17 00:00:00 2001 From: Toast Date: Fri, 7 Feb 2025 14:07:03 +0100 Subject: [PATCH 4/4] Shows/schema: add genre reference --- src/shows/schema/show.schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shows/schema/show.schema.ts b/src/shows/schema/show.schema.ts index 33e1e68..70e0aab 100644 --- a/src/shows/schema/show.schema.ts +++ b/src/shows/schema/show.schema.ts @@ -6,6 +6,6 @@ export const ShowSchema = new Schema({ seasons: { type: Number, required: true }, episodes: { type: Number, required: true }, description: { type: String, required: true }, - genres: [{ type: String, required: true }], + genres: [{ type: Schema.Types.ObjectId, required: true, ref: 'Genre' }], images: [{ type: String, required: true }], });