From 42985da24487a79d7b21d46d1626c16b6cff8e8c Mon Sep 17 00:00:00 2001 From: Glydric Date: Mon, 22 Jan 2024 16:52:38 +0100 Subject: [PATCH] added limit to filtering --- .../src/app/frontend/frontend.controller.ts | 14 ++++++++ Backend/src/database/database.service.ts | 33 ++++++++++--------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/Backend/src/app/frontend/frontend.controller.ts b/Backend/src/app/frontend/frontend.controller.ts index 24f6748..e0b5e50 100644 --- a/Backend/src/app/frontend/frontend.controller.ts +++ b/Backend/src/app/frontend/frontend.controller.ts @@ -71,6 +71,20 @@ export class FrontendController { return this.databaseService.getData(filter); } + @ApiParam(filterParams) + @ApiParam({ + name: 'limit', + type: 'number', + example: 10, + }) + @Get(`:filter(${filters.join('|')})/:limit(\\d+)`) + getValuesLimit( + @Param('filter', FiltersValidator) filter: FiltersAvailable, + @Param('limit', PositiveNumberValidator) limit: number, + ) { + return this.databaseService.getData(filter, limit); + } + @ApiParam({ name: 'id', type: 'number', diff --git a/Backend/src/database/database.service.ts b/Backend/src/database/database.service.ts index 78a7cf2..4be9756 100644 --- a/Backend/src/database/database.service.ts +++ b/Backend/src/database/database.service.ts @@ -71,25 +71,28 @@ export class DatabaseService { return await col.insertOne(data); } - getData(filter?: FiltersAvailable): Promise { - return this.DB.collection('cameras') - .aggregate([ - { - $addFields: { - intrusionDetection: { - $cond: { - if: { - $ifNull: ['$intrusionDetection', false], - }, - then: true, - else: false, + getData( + filter: FiltersAvailable, + limit: number = undefined, + ): Promise { + const res = this.DB.collection('cameras').aggregate([ + { + $addFields: { + intrusionDetection: { + $cond: { + if: { + $ifNull: ['$intrusionDetection', false], }, + then: true, + else: false, }, }, }, - ]) - .match(this.getFilter(filter)) - .toArray(); + }, + ]); + if (limit != undefined) + return res.limit(limit).match(this.getFilter(filter)).toArray(); + else return res.match(this.getFilter(filter)).toArray(); } aggregateCamera(filter?: FiltersAvailable): Promise {