From c4e463884951794259561765a28b0b99d0e69596 Mon Sep 17 00:00:00 2001 From: Nzube Uwakwe Date: Fri, 28 Feb 2025 22:06:54 +0300 Subject: [PATCH 1/2] feat: add pagination to get all jobs endpoint --- .husky/pre-commit | 2 +- src/modules/jobs/jobs.controller.ts | 21 +++++++++++++++++---- src/modules/jobs/jobs.service.ts | 28 ++++++++++++++++++++++++---- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 36af21989..a67e26242 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,4 @@ #!/bin/sh -. "$(dirname "$0")/_/husky.sh" +# . "$(dirname "$0")/_/husky.sh" npx lint-staged diff --git a/src/modules/jobs/jobs.controller.ts b/src/modules/jobs/jobs.controller.ts index 50372dc7e..b9b3590f7 100644 --- a/src/modules/jobs/jobs.controller.ts +++ b/src/modules/jobs/jobs.controller.ts @@ -10,6 +10,7 @@ import { UseGuards, ValidationPipe, ParseUUIDPipe, + ParseIntPipe, } from '@nestjs/common'; import { ApiBadRequestResponse, @@ -88,11 +89,23 @@ export class JobsController { @skipAuth() @Get('/') - @ApiOperation({ summary: 'Gets all jobs' }) + @ApiOperation({ summary: 'Gets all jobs with pagination' }) + @ApiQuery({ name: 'page', required: false, type: Number, example: 1, description: 'Page number (default: 1)' }) + @ApiQuery({ + name: 'limit', + required: false, + type: Number, + example: 10, + description: 'Number of jobs per page (default: 10, max: 100)', + }) @ApiResponse({ status: 200, description: 'Jobs returned successfully' }) - @ApiResponse({ status: 404, description: 'Job not found' }) - async getAllJobs() { - return this.jobService.getJobs(); + @ApiResponse({ status: 400, description: 'Invalid pagination parameters' }) + @ApiResponse({ status: 404, description: 'No jobs found' }) + async getAllJobs( + @Query('page', new ParseIntPipe({ errorHttpStatusCode: 400 })) page: number = 1, + @Query('limit', new ParseIntPipe({ errorHttpStatusCode: 400 })) limit: number = 10 + ) { + return this.jobService.getJobs(page, limit); } @skipAuth() diff --git a/src/modules/jobs/jobs.service.ts b/src/modules/jobs/jobs.service.ts index 034395a92..5f704ffcb 100644 --- a/src/modules/jobs/jobs.service.ts +++ b/src/modules/jobs/jobs.service.ts @@ -1,4 +1,4 @@ -import { HttpStatus, Injectable } from '@nestjs/common'; +import { HttpStatus, Injectable, BadRequestException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import * as SYS_MSG from '@shared/constants/SystemMessages'; @@ -81,14 +81,34 @@ export class JobsService { }; } - async getJobs() { - const jobs = await this.jobRepository.find({ where: { is_deleted: false } }); + async getJobs(page: number = 1, limit: number = 10) { + if (!Number.isInteger(page) || page < 1) { + throw new BadRequestException('Page must be a positive integer.'); + } + if (!Number.isInteger(limit) || limit < 1 || limit > 100) { + throw new BadRequestException('Limit must be a positive integer between 1 and 100.'); + } + + limit = limit > 100 ? 100 : limit; + + const [jobs, total] = await this.jobRepository.findAndCount({ + where: { is_deleted: false }, + take: limit, + skip: (page - 1) * limit, + }); + + jobs.forEach(job => delete job.is_deleted); - jobs.map(x => delete x.is_deleted); return { message: SYS_MSG.JOB_LISTING_RETRIEVAL_SUCCESSFUL, status_code: 200, data: jobs, + meta: { + total_jobs: total, + total_pages: Math.ceil(total / limit), + current_page: page, + per_page: limit, + }, }; } From ed55a76f072725ccf8e9b82d1cde8828b3cbc3ea Mon Sep 17 00:00:00 2001 From: Nzube Uwakwe Date: Fri, 28 Feb 2025 22:09:02 +0300 Subject: [PATCH 2/2] chore: update husky commit-msg hook --- .husky/commit-msg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.husky/commit-msg b/.husky/commit-msg index 5a8500090..42644c815 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,4 +1,4 @@ #!/bin/sh -. "$(dirname "$0")/_/husky.sh" +# . "$(dirname "$0")/_/husky.sh" npx --no -- commitlint --edit $1