Shows: add pagination to search endpoint
This commit is contained in:
parent
f219011784
commit
65797b9243
2 changed files with 20 additions and 6 deletions
|
|
@ -85,15 +85,21 @@ export class ShowsController {
|
|||
}
|
||||
|
||||
@Get('search')
|
||||
async search(@Query() search: SearchDto) {
|
||||
async search(@Query() search: SearchDto, @Query() pagination: PaginationDto) {
|
||||
try {
|
||||
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) {
|
||||
return {
|
||||
status: 'Ok',
|
||||
shows: serviceResponse.shows,
|
||||
showCount: serviceResponse.showCount
|
||||
showCount: serviceResponse.showCount,
|
||||
};
|
||||
} else {
|
||||
throw new NotFoundException({
|
||||
|
|
|
|||
|
|
@ -32,14 +32,22 @@ export class ShowsService {
|
|||
return this.showModel.findById(id);
|
||||
}
|
||||
|
||||
async search(name: string) {
|
||||
async search(name: string, page: number, showsPerPage: number) {
|
||||
const regex = new RegExp(name, 'i');
|
||||
const filter = {
|
||||
$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 {
|
||||
shows: await this.showModel.find(filter),
|
||||
showCount: await this.showModel.find(filter).countDocuments()
|
||||
shows: shows,
|
||||
showCount: await this.showModel.find(filter).countDocuments(),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue