Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination feature to get all jobs endpoint. #1309

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# . "$(dirname "$0")/_/husky.sh"

npx --no -- commitlint --edit $1
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# . "$(dirname "$0")/_/husky.sh"

npx lint-staged
23 changes: 18 additions & 5 deletions src/modules/jobs/jobs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {
UseGuards,
ValidationPipe,
ParseUUIDPipe,
ParseIntPipe,
Patch,
UseInterceptors,
UploadedFile,
BadRequestException,
BadRequestException,
} from '@nestjs/common';
import {
ApiConsumes,
Expand Down Expand Up @@ -108,11 +109,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()
Expand Down
28 changes: 24 additions & 4 deletions src/modules/jobs/jobs.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -94,14 +94,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,
},
};
}

Expand Down
Loading