Add byGenre show endpoint

This commit is contained in:
Toast 2025-02-21 18:57:43 +01:00
parent 0758b1ae3d
commit b4fdee776a
3 changed files with 53 additions and 1 deletions

View file

@ -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,

View file

@ -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 {

View file

@ -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<any> {
return this.showModel.findByIdAndUpdate(id, { $set: dto }, { new: true });
}