Skip to content

Commit

Permalink
added limit to filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Glydric committed Jan 22, 2024
1 parent f8900c4 commit 42985da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
14 changes: 14 additions & 0 deletions Backend/src/app/frontend/frontend.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
33 changes: 18 additions & 15 deletions Backend/src/database/database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,28 @@ export class DatabaseService {
return await col.insertOne(data);
}

getData(filter?: FiltersAvailable): Promise<Document[]> {
return this.DB.collection('cameras')
.aggregate([
{
$addFields: {
intrusionDetection: {
$cond: {
if: {
$ifNull: ['$intrusionDetection', false],
},
then: true,
else: false,
getData(
filter: FiltersAvailable,
limit: number = undefined,
): Promise<Document[]> {
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<Document[]> {
Expand Down

0 comments on commit 42985da

Please sign in to comment.