Add byGenre show endpoint
This commit is contained in:
parent
0758b1ae3d
commit
b4fdee776a
3 changed files with 53 additions and 1 deletions
|
|
@ -13,7 +13,7 @@ async function bootstrap() {
|
||||||
} else {
|
} else {
|
||||||
logger.log('In development mode');
|
logger.log('In development mode');
|
||||||
origin.push('http://localhost:4200');
|
origin.push('http://localhost:4200');
|
||||||
origin.push('http://localhost:8100')
|
origin.push('http://localhost:8100');
|
||||||
}
|
}
|
||||||
app.enableCors({
|
app.enableCors({
|
||||||
credentials: true,
|
credentials: true,
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,12 @@ export class SearchDto {
|
||||||
query: string;
|
query: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class GenreDto {
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
genreId: string;
|
||||||
|
}
|
||||||
|
|
||||||
@Controller('shows')
|
@Controller('shows')
|
||||||
export class ShowsController {
|
export class ShowsController {
|
||||||
constructor(private readonly showsService: ShowsService) {}
|
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')
|
@Put('id/:id')
|
||||||
async update(@Param('id') id: string, @Body() dto: ShowDto) {
|
async update(@Param('id') id: string, @Body() dto: ShowDto) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -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> {
|
async update(id: string, dto: ShowDto): Promise<any> {
|
||||||
return this.showModel.findByIdAndUpdate(id, { $set: dto }, { new: true });
|
return this.showModel.findByIdAndUpdate(id, { $set: dto }, { new: true });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue