Skip to content

Commit f216a3e

Browse files
authored
GET API Filter Fixed (#104)
* GET API Fixed
1 parent 0aab546 commit f216a3e

File tree

3 files changed

+120
-131
lines changed

3 files changed

+120
-131
lines changed

controllers/opportunity/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class opportunityController {
4343

4444
//getOpportunities
4545
async getOpportunities(req, res) {
46-
console.log('Inside controller');
4746
const queryObject = { ...req.query };
4847

4948
try {

helpers/pagination.js

Lines changed: 120 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -8,135 +8,127 @@
88
// page 3 = {8,9} .
99

1010
export default async function pagination(queryObject, opportunity) {
11-
// Filter based on opportunityType
12-
if (queryObject.type) {
13-
queryObject['opportunityType'] = queryObject.type;
14-
delete queryObject.type;
15-
}
16-
17-
//console.log('Values of QueryString', queryObject);
18-
19-
// Here page means which page to start from and limit specifies the number of documnents to be
20-
// rendered starting from this page
21-
let { limit, page } = queryObject;
22-
23-
// The case when both page and limit are not specified we assign both of them default values
24-
if (page == undefined && limit == undefined) {
25-
limit = 10;
26-
page = 1;
27-
}
28-
29-
//Only limit is 'undefined or 0' , so it is assigned a default value of 1
30-
if (limit == undefined) {
31-
limit = 10;
32-
}
33-
34-
// The case when starting page is not mentioned or it is Put in as 0 , we use default value
35-
if (page == undefined) {
36-
page = 1;
37-
}
38-
39-
// The query objects are by default "string" , so we convert them to "INT" to work ahead
40-
limit = parseInt(limit);
41-
page = parseInt(page);
42-
43-
// The startIndex is the first index to be rendered from the database
44-
// skip is the total no of documents/items on each page as per the user
45-
const startIndex = page - 1;
46-
const skip = (page - 1) * limit;
47-
48-
// Object initialised to store the results for various queries
49-
const result = {};
50-
51-
// The case when the queries are invalid , we just return a result with valid error message
52-
// Number of Total Documents Saved in a variable
53-
let numDocs = await opportunity.countDocuments().exec();
54-
55-
// TotalPages denote the total number of pages that are possible with the given limit and Documents available
56-
let totalPages;
57-
58-
// When limit is less than or equal to the number of Docs
59-
if (numDocs >= limit) {
60-
totalPages = Math.floor(numDocs / limit) + (numDocs % limit);
61-
}
62-
63-
// this is the case when limit exceeds the documents , so we only have a single page
64-
else {
65-
totalPages = 1;
66-
}
67-
68-
// 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
69-
let begin = page * limit;
70-
71-
// If any of the paramter becomes neagtive or Zero
72-
if (page <= 0 || limit <= 0) {
73-
result.results = 'The Parameters cannot be Negative or Zero';
74-
return result;
75-
}
76-
77-
// If user enters a Page number greater than the number of Documents present in the Database
78-
if (startIndex >= numDocs) {
79-
result.results = 'The maximum page value should be ' + numDocs;
80-
return result;
81-
}
82-
83-
// If the page entered exceeds the Max allowed value(totalPages)
84-
if (page > totalPages) {
85-
let maxAllowedPages = totalPages;
86-
result.results = 'The maximum allowed pages are ' + maxAllowedPages;
87-
return result;
88-
}
89-
90-
// StartIndex should be atleast 0 and for the min val of Page should be 1
91-
if (startIndex < 0) {
92-
result.results = 'The minimum page value should be 1';
93-
return result;
11+
// Here page means which page to start from and limit specifies the number of documnents to be
12+
// rendered starting from this page
13+
let { limit, page } = queryObject;
14+
15+
// The case when both page and limit are not specified we assign both of them default values
16+
if (page == undefined && limit == undefined) {
17+
limit = 10;
18+
page = 1;
19+
}
20+
21+
//Only limit is 'undefined or 0' , so it is assigned a default value of 1
22+
if (limit == undefined) {
23+
limit = 10;
24+
}
25+
26+
// The case when starting page is not mentioned or it is Put in as 0 , we use default value
27+
if (page == undefined) {
28+
page = 1;
29+
}
30+
31+
// The query objects are by default "string" , so we convert them to "INT" to work ahead
32+
limit = parseInt(limit);
33+
page = parseInt(page);
34+
35+
// The startIndex is the first index to be rendered from the database
36+
// skip is the total no of documents/items on each page as per the user
37+
const startIndex = page - 1;
38+
const skip = (page - 1) * limit;
39+
40+
// Object initialised to store the results for various queries
41+
const result = {};
42+
43+
// The case when the queries are invalid , we just return a result with valid error message
44+
// Number of Total Documents Saved in a variable
45+
let numDocs = await opportunity.countDocuments().exec();
46+
47+
// TotalPages denote the total number of pages that are possible with the given limit and Documents available
48+
let totalPages;
49+
50+
// When limit is less than or equal to the number of Docs
51+
if (numDocs >= limit) {
52+
totalPages = Math.floor(numDocs / limit) + (numDocs % limit);
53+
}
54+
55+
// this is the case when limit exceeds the documents , so we only have a single page
56+
else {
57+
totalPages = 1;
58+
}
59+
60+
// 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
61+
let begin = page * limit;
62+
63+
// If any of the paramter becomes neagtive or Zero
64+
if (page <= 0 || limit <= 0) {
65+
result.results = 'The Parameters cannot be Negative or Zero';
66+
return result;
67+
}
68+
69+
// If user enters a Page number greater than the number of Documents present in the Database
70+
if (startIndex >= numDocs) {
71+
result.results = 'The maximum page value should be ' + numDocs;
72+
return result;
73+
}
74+
75+
// If the page entered exceeds the Max allowed value(totalPages)
76+
if (page > totalPages) {
77+
let maxAllowedPages = totalPages;
78+
result.results = 'The maximum allowed pages are ' + maxAllowedPages;
79+
return result;
80+
}
81+
82+
// StartIndex should be atleast 0 and for the min val of Page should be 1
83+
if (startIndex < 0) {
84+
result.results = 'The minimum page value should be 1';
85+
return result;
86+
}
87+
88+
// Page can't be entered more than the Number of documents present in DataBase
89+
if (startIndex >= numDocs) {
90+
result.results = 'The maximum page value should be ' + numDocs;
91+
return result;
92+
}
93+
94+
// If we get valid data we also render the Next and Previous page to the User along with the Results
95+
else {
96+
// If there is valid next page in the Database , then we let the user know about it
97+
98+
if (begin < numDocs) {
99+
result.next = {
100+
page: page + 1,
101+
limit: limit,
102+
};
94103
}
95-
96-
// Page can't be entered more than the Number of documents present in DataBase
97-
if (startIndex >= numDocs) {
98-
result.results = 'The maximum page value should be ' + numDocs;
99-
return result;
104+
105+
// If there is valid Previous page in the Database , then we let the user know about it
106+
if (startIndex > 0) {
107+
result.previous = {
108+
page: page - 1,
109+
limit: limit,
110+
};
100111
}
101-
102-
// If we get valid data we also render the Next and Previous page to the User along with the Results
103-
else {
104-
// If there is valid next page in the Database , then we let the user know about it
105-
106-
if (begin < numDocs) {
107-
result.next = {
108-
page: page + 1,
109-
limit: limit,
110-
};
111-
}
112-
113-
// If there is valid Previous page in the Database , then we let the user know about it
114-
if (startIndex > 0) {
115-
result.previous = {
116-
page: page - 1,
117-
limit: limit,
118-
};
112+
}
113+
114+
let query = {};
115+
if (queryObject.type) query['opportunityType'] = queryObject.type;
116+
117+
// Once a Valid Query(The one which is inside the Range) is entered, we render the Results
118+
try {
119+
result.results = await opportunity.find(
120+
query,
121+
{
122+
/* No constraints */
123+
},
124+
{
125+
skip: skip,
126+
limit: limit,
119127
}
120-
}
121-
122-
// Once a Valid Query(The one which is inside the Range) is entered, we render the Results
123-
try {
124-
result.results = await opportunity.find(
125-
{
126-
/* Everything*/
127-
},
128-
{
129-
/* No constraints */
130-
},
131-
{
132-
skip: skip,
133-
limit: limit,
134-
}
135-
);
136-
137-
return result;
138-
} catch (e) {
139-
console.log(`ERR: `, e.stack);
140-
}
128+
);
129+
130+
return result;
131+
} catch (e) {
132+
console.log(`ERR: `, e.stack);
141133
}
142-
134+
}

services/opportunity/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class opportunityService {
3737
}
3838

3939
async getOpportunities(queryObject) {
40-
console.log('Inside Service');
41-
4240
try {
4341
let fetchedOpportunities = await this.opportunityManager.getOpportunities(
4442
queryObject

0 commit comments

Comments
 (0)