Skip to content
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
42 changes: 21 additions & 21 deletions helpers/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ export default async function pagination(queryObject, opportunity) {
limit = parseInt(limit);
page = parseInt(page);

// The startIndex is the first index to be rendered from the database
// skip is the total no of documents/items on each page as per the user
const startIndex = page - 1;
const skip = (page - 1) * limit;


// Object initialised to store the results for various queries
const result = {};
Expand All @@ -49,16 +46,17 @@ export default async function pagination(queryObject, opportunity) {

// When limit is less than or equal to the number of Docs
if (numDocs >= limit) {
totalPages = Math.floor(numDocs / limit) + (numDocs % limit);

totalPages = Math.floor(numDocs / limit) + (numDocs % limit === 0 ? 0 : 1);

}

// this is the case when limit exceeds the documents , so we only have a single page
else {
totalPages = 1;
}

// Begin here shows the first index of next Page and helps in determining wether or not it is possible to go on the next page
let begin = page * limit;


// If any of the paramter becomes neagtive or Zero
if (page <= 0 || limit <= 0) {
Expand All @@ -67,43 +65,39 @@ export default async function pagination(queryObject, opportunity) {
}

// If user enters a Page number greater than the number of Documents present in the Database
if (startIndex >= numDocs) {
result.results = 'The maximum page value should be ' + numDocs;
return result;
}

// If the page entered exceeds the Max allowed value(totalPages)
if (page > totalPages) {
let maxAllowedPages = totalPages;
result.results = 'The maximum allowed pages are ' + maxAllowedPages;
return result;
}



// StartIndex should be atleast 0 and for the min val of Page should be 1
if (startIndex < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove these checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purpose this has been updated in my code.

if (page <=0) {

result.results = 'The minimum page value should be 1';
return result;
}

// Page can't be entered more than the Number of documents present in DataBase
if (startIndex >= numDocs) {
result.results = 'The maximum page value should be ' + numDocs;
return result;
}



// If we get valid data we also render the Next and Previous page to the User along with the Results
else {
// If there is valid next page in the Database , then we let the user know about it

if (begin < numDocs) {
if (page * limit < numDocs) {
result.next = {
page: page + 1,
limit: limit,
};
}

// If there is valid Previous page in the Database , then we let the user know about it
if (startIndex > 0) {

if (page>1) {
result.previous = {
page: page - 1,
limit: limit,
Expand All @@ -114,6 +108,11 @@ export default async function pagination(queryObject, opportunity) {
let query = {};
if (queryObject.type) query['opportunityType'] = queryObject.type;


if(queryObject.opportunityType!=null)
{
query={ opportunityType: queryObject.opportunityType,}
}
// Once a Valid Query(The one which is inside the Range) is entered, we render the Results
try {
result.results = await opportunity.find(
Expand All @@ -122,7 +121,8 @@ export default async function pagination(queryObject, opportunity) {
/* No constraints */
},
{
skip: skip,
skip: limit*(page-1),

limit: limit,
}
);
Expand Down
Loading