Shows: add pagination to search endpoint

This commit is contained in:
Toast 2025-02-02 18:45:36 +01:00
parent f219011784
commit 65797b9243
2 changed files with 20 additions and 6 deletions

View file

@ -85,15 +85,21 @@ export class ShowsController {
} }
@Get('search') @Get('search')
async search(@Query() search: SearchDto) { async search(@Query() search: SearchDto, @Query() pagination: PaginationDto) {
try { try {
const { query } = search; const { query } = search;
const serviceResponse = await this.showsService.search(query); const { page, limit } = pagination;
const serviceResponse = await this.showsService.search(
query,
page,
limit,
);
if (serviceResponse.showCount > 0) { if (serviceResponse.showCount > 0) {
return { return {
status: 'Ok', status: 'Ok',
shows: serviceResponse.shows, shows: serviceResponse.shows,
showCount: serviceResponse.showCount showCount: serviceResponse.showCount,
}; };
} else { } else {
throw new NotFoundException({ throw new NotFoundException({

View file

@ -32,14 +32,22 @@ export class ShowsService {
return this.showModel.findById(id); return this.showModel.findById(id);
} }
async search(name: string) { async search(name: string, page: number, showsPerPage: number) {
const regex = new RegExp(name, 'i'); const regex = new RegExp(name, 'i');
const filter = { const filter = {
$or: [{ title: { $regex: regex } }, { description: { $regex: regex } }], $or: [{ title: { $regex: regex } }, { description: { $regex: regex } }],
}; };
let shows;
if (showsPerPage !== undefined) {
const skip = (page - 1) * showsPerPage;
shows = await this.showModel.find(filter).skip(skip).limit(showsPerPage);
} else {
shows = await this.showModel.find(filter);
}
return { return {
shows: await this.showModel.find(filter), shows: shows,
showCount: await this.showModel.find(filter).countDocuments() showCount: await this.showModel.find(filter).countDocuments(),
}; };
} }