Skip to content

Commit

Permalink
feat: set of default params for the paginated request
Browse files Browse the repository at this point in the history
  • Loading branch information
JanssenBrm committed Dec 6, 2024
1 parent 069c2d9 commit 3883f77
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/jobs/controllers/jobs/jobs.controller.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import {
Body,
Controller,
DefaultValuePipe,
Delete,
HttpCode,
InternalServerErrorException,
Logger,
NotFoundException,
Param,
ParseIntPipe,
Patch,
Post,
Query,
} from '@nestjs/common';
import { ApiBody, ApiOperation } from '@nestjs/swagger';
import { ApiBody, ApiOperation, ApiQuery } from '@nestjs/swagger';
import { Job, PatchJob } from '../../models/job.dto';
import { DatabaseService } from '../../services/database/database.service';
import { CachingService } from '../../../caching/services/cache.service';
import { ConfigService } from '../../../config/config/config.service';
import { Pagination } from '../../models/pagination.dto';

@Controller('jobs')
Expand Down Expand Up @@ -50,20 +51,31 @@ export class JobsController {
description: 'Query supported by ElasticSearch',
required: true,
})
@ApiQuery({
name: 'size',
type: Number,
description: 'Number of elements to return on each page',
required: false,
})
@ApiQuery({
name: 'page',
type: Number,
description: 'Page number to request',
required: false,
})
async queryJobsPaginated(
@Body() query: any,
@Query('size') size: number,
@Query('page') page?: number,
@Query('size', new DefaultValuePipe(10), ParseIntPipe) size: number,
@Query('page', new DefaultValuePipe(0), ParseIntPipe) page?: number,
): Promise<Pagination> {
try {
const p = page || 0;
const cacheKey = `paginated_search_result_${btoa(
JSON.stringify(query),
)}_${size}_${p}`;
)}_${size}_${page}`;
let result = await this.cachingService.checkCache<Pagination>(cacheKey);

if (!result) {
result = await this.databaseService.queryJobs(query, p, size, false);
result = await this.databaseService.queryJobs(query, page, size, false);
await this.cachingService.store(cacheKey, result);
}

Expand Down

0 comments on commit 3883f77

Please sign in to comment.