From b4fdee776aa7aa2ae6648070cad449a6c88c65c5 Mon Sep 17 00:00:00 2001 From: Toast Date: Fri, 21 Feb 2025 18:57:43 +0100 Subject: [PATCH] Add byGenre show endpoint --- src/main.ts | 2 +- src/shows/shows.controller.ts | 30 ++++++++++++++++++++++++++++++ src/shows/shows.service.ts | 22 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index abb4796..0903dd0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,7 +13,7 @@ async function bootstrap() { } else { logger.log('In development mode'); origin.push('http://localhost:4200'); - origin.push('http://localhost:8100') + origin.push('http://localhost:8100'); } app.enableCors({ credentials: true, diff --git a/src/shows/shows.controller.ts b/src/shows/shows.controller.ts index 06f04cb..c2a3581 100644 --- a/src/shows/shows.controller.ts +++ b/src/shows/shows.controller.ts @@ -22,6 +22,12 @@ export class SearchDto { query: string; } +export class GenreDto { + @IsString() + @IsNotEmpty() + genreId: string; +} + @Controller('shows') export class ShowsController { constructor(private readonly showsService: ShowsService) {} @@ -118,6 +124,30 @@ export class ShowsController { } } + @Get('byGenre') + async byGenre(@Query() genre: GenreDto, @Query() pagination: PaginationDto) { + try { + const { genreId } = genre; + const { page, limit } = pagination; + + const serviceResponse = await this.showsService.byGenre( + genreId, + page, + limit, + ); + return { + status: 'Ok', + shows: serviceResponse.shows, + showCount: serviceResponse.showCount, + }; + } catch (error) { + throw new InternalServerErrorException({ + status: error.name, + message: error.message, + }); + } + } + @Put('id/:id') async update(@Param('id') id: string, @Body() dto: ShowDto) { try { diff --git a/src/shows/shows.service.ts b/src/shows/shows.service.ts index 393382e..00923f4 100644 --- a/src/shows/shows.service.ts +++ b/src/shows/shows.service.ts @@ -59,6 +59,28 @@ export class ShowsService { }; } + async byGenre(genreId: string, page: number, showsPerPage: number) { + const filter = { + genres: { $in: [genreId] }, + }; + let shows; + + if (showsPerPage !== undefined) { + const skip = (page - 1) * showsPerPage; + shows = await this.showModel + .find(filter) + .skip(skip) + .limit(showsPerPage) + .populate('genres'); + } else { + shows = await this.showModel.find(filter).populate('genres'); + } + return { + shows: shows, + showCount: await this.showModel.find(filter).countDocuments(), + }; + } + async update(id: string, dto: ShowDto): Promise { return this.showModel.findByIdAndUpdate(id, { $set: dto }, { new: true }); }