Skip to content

Commit 10de320

Browse files
author
Richa Khandelwal
committed
Sorting and Paging functionality
1 parent 950502b commit 10de320

6 files changed

+292
-196
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
* Create new configuration - POST to localhost:3000/configuration with body {"name": "host3","hostname": "test.lab.com","port": "5555", "username":"test"}
1818
* Update a configuration - PUT to localhost:3000/configuration/id/{id} with desired updates.
1919
* Delete a configuration - localhost:3000/configuration/id/{id}
20+
* Paging - localhost:3000/configuration/commonProperty/something10?page=2
21+
* Paging - localhost:3000/configuration/?page=2
22+
* Sorting - localhost:3000/configuration/?sort_by=port&sort_order=asc
23+
* Sorting - localhost:3000/configuration/?sort_by=port&sort_order=asc
24+
* Sorting - localhost:3000/configuration/?sort_by=hostname
25+
* Sorting and Paging - localhost:3000/configuration/?sort_by=hostname&sort_order=desc&page=4
2026
* End with logout - Delete localhost:3000/login/richa
2127

2228

configGenerator.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
generateConfigs: function generateConfigs(numberOfConfigs) {
3+
var configs = [];
4+
for (var i = 1; i <= numberOfConfigs; i++) {
5+
configs.push({
6+
id: Math.random(),
7+
name: 'host' + i,
8+
hostname: 'test' + i + '.lab.com',
9+
port: 5000 + i,
10+
username: 'test' + i,
11+
commonProperty: i > 10 ? 'something10' : 'something1'
12+
})
13+
}
14+
return configs;
15+
}
16+
};

configurationController.js

+144-99
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,148 @@
1-
var configuration1 = {id: Math.random(), name: "host1", hostname: "nessus-ntp.lab.com", port: "1241", username: "toto"};
2-
var configuration2 = {id: Math.random(), name: "host2", hostname: "nessus-xml.lab.com", port: "3384", username: "admin"};
3-
var existingConfigurations = [configuration1, configuration2];
1+
var configGenerator = require('./configGenerator');
2+
var utility = require('./utility');
3+
4+
var pageSize = 5;
5+
var existingConfigurations = configGenerator.generateConfigs(30);
6+
var acceptedSortByProperties = ['hostname', 'name', 'username', 'port'];
47

58
module.exports = {
6-
process: function process (method, requestedResource, key, value, requestBody) {
7-
var result = {
8-
success: true,
9-
statusCode: 200,
10-
header : {"Content-Type": "application/json"}
11-
}
12-
var data;
13-
if (method === 'POST' || method === 'PUT') {
14-
data = JSON.parse(requestBody);
15-
}
16-
17-
switch (method) {
18-
case 'GET':
19-
if (!key) {
20-
result.message = JSON.stringify(existingConfigurations); //return all configurations.
21-
} else {
22-
var matchingConfigs = existingConfigurations.filter(function (el) {
23-
return el[key] == value;
24-
});
25-
26-
if(matchingConfigs) {
27-
result.message = JSON.stringify(matchingConfigs);
28-
} else {
29-
result.statusCode = 404;
30-
result.success = false;
31-
result.message = 'No configuration found with key ' + key + ' and value ' + value;
32-
}
33-
}
34-
break;
35-
case 'POST':
36-
if(data) {
37-
var newConfig = data;
38-
data.id = Math.random();
39-
existingConfigurations.push(data);
40-
result.message = 'Created new config with id ' + data.id
41-
result.statusCode = 201;
42-
result.header.location = "localhost:3000/configuration/id/" + data.id
43-
} else {
44-
result.statusCode = 400;
45-
result.success = false;
46-
result.message = 'Please supply data to create new config.';
47-
}
48-
break;
49-
case 'PUT':
50-
if('id' === key) {
51-
var matchingConfig = getMatchingConfig(key, value);
52-
console.log('Matching config for update ' +matchingConfig)
53-
if(!matchingConfig) {
54-
result.message = 'No matching config found to update for id ' + value;
55-
} else if (matchingConfig[key] !== data[key]) {
56-
result.message = 'Config ID in the path ' + value + ' does not match the config ID ' + data[key] + ' in the request body.'
57-
} else {
58-
for (var k in data) {
59-
if (data.hasOwnProperty(k) && k !== 'id') {
60-
matchingConfig[k] = data[k]
61-
}
62-
}
63-
result.message = 'Config with id ' + value + ' has been updated.'
64-
}
65-
66-
} else {
67-
result.statusCode = 400;
68-
result.success = false;
69-
result.message = 'Please provide config unique ID to update an existing config.';
70-
}
71-
break;
72-
case 'DELETE':
73-
if('id' === key) {
74-
var matchingConfig = getMatchingConfig(key, value);
75-
existingConfigurations.splice(existingConfigurations.indexOf(matchingConfig), 1);
76-
result.message = 'Config with id ' + value + ' has been deleted.';
77-
} else {
78-
result.statusCode = 400;
79-
result.success = false;
80-
result.message = 'Please provide config unique ID to delete an existing config.';
81-
}
82-
break;
83-
default:
84-
result.statusCode = 400;
85-
result.success = false;
86-
result.message = 'This action is not supported.';
87-
break;
88-
}
89-
return result;
90-
}
91-
}
9+
process: function process(method, requestedResource, key, value, requestBody, queries) {
10+
var result = {
11+
success: true,
12+
statusCode: 200,
13+
header: {"Content-Type": "application/json"}
14+
};
15+
var data, matchingConfig;
16+
if (method === 'POST' || method === 'PUT') {
17+
data = JSON.parse(requestBody);
18+
}
19+
20+
switch (method) {
21+
case 'GET':
22+
if (!key) {
23+
if(Object.keys(queries).length) {
24+
result.message = JSON.stringify(massageData(existingConfigurations, queries));
25+
} else {
26+
result.message = JSON.stringify(existingConfigurations); //return all configurations.
27+
}
28+
} else {
29+
var matchingConfigs = existingConfigurations.filter(function (el) {
30+
return el[key] == value;
31+
});
32+
33+
if (matchingConfigs) {
34+
if(Object.keys(queries).length) {
35+
result.message = JSON.stringify(massageData(matchingConfigs, queries));
36+
} else {
37+
result.message = JSON.stringify(matchingConfigs);
38+
}
39+
40+
} else {
41+
result.statusCode = 404;
42+
result.success = false;
43+
result.message = 'No configuration found with key ' + key + ' and value ' + value;
44+
}
45+
}
46+
break;
47+
case 'POST':
48+
if (data) {
49+
data.id = Math.random();
50+
existingConfigurations.push(data);
51+
result.message = 'Created new config with id ' + data.id;
52+
result.statusCode = 201;
53+
result.header.location = "localhost:3000/configuration/id/" + data.id;
54+
} else {
55+
result.statusCode = 400;
56+
result.success = false;
57+
result.message = 'Please supply data to create new config.';
58+
}
59+
break;
60+
case 'PUT':
61+
if ('id' === key) {
62+
matchingConfig = getMatchingConfig(key, value);
63+
64+
if (!matchingConfig) {
65+
result.message = 'No matching config found to update for id ' + value;
66+
} else if (matchingConfig[key] !== data[key]) {
67+
result.message = 'Config ID in the path ' + value + ' does not match the config ID ' + data[key] + ' in the request body.';
68+
} else {
69+
for (var k in data) {
70+
if (data.hasOwnProperty(k) && k !== 'id') {
71+
matchingConfig[k] = data[k]
72+
}
73+
}
74+
result.message = 'Config with id ' + value + ' has been updated.';
75+
}
76+
77+
} else {
78+
result.statusCode = 400;
79+
result.success = false;
80+
result.message = 'Please provide config unique ID to update an existing config.';
81+
}
82+
break;
83+
case 'DELETE':
84+
if ('id' === key) {
85+
matchingConfig = getMatchingConfig(key, value);
86+
existingConfigurations.splice(existingConfigurations.indexOf(matchingConfig), 1);
87+
result.message = 'Config with id ' + value + ' has been deleted.';
88+
} else {
89+
result.statusCode = 400;
90+
result.success = false;
91+
result.message = 'Please provide config unique ID to delete an existing config.';
92+
}
93+
break;
94+
default:
95+
result.statusCode = 400;
96+
result.success = false;
97+
result.message = 'This action is not supported.';
98+
break;
99+
}
100+
return result;
101+
}
102+
};
92103

93104
/*
94-
Returns the first config that matches the key value pair. Use it isolation only when you are expecting unique key.
95-
*/
96-
function getMatchingConfig (key, value) {
97-
for(var index in existingConfigurations) {
98-
var config = existingConfigurations[index];
99-
if(config[key] == value) {
100-
return config;
101-
}
102-
}
103-
}
105+
Returns the first config that matches the key value pair. Use it isolation only when you are expecting unique key.
106+
*/
107+
function getMatchingConfig(key, value) {
108+
for (var index in existingConfigurations) {
109+
var config = existingConfigurations[index];
110+
if (config[key] == value) {
111+
return config;
112+
}
113+
}
114+
}
115+
116+
function massageData(configs, queries) {
117+
var result = configs;
118+
if(queries.hasOwnProperty('page')) {
119+
if(queries['page'] > 0) {
120+
var startIndex = (queries['page'] - 1) * 5;
121+
result = configs.slice(startIndex, startIndex + pageSize)
122+
}
123+
}
124+
125+
if(queries.hasOwnProperty('sort_by')) {
126+
var sortBy = queries['sort_by'];
127+
if(acceptedSortByProperties.indexOf(sortBy) < 0) {
128+
return result;
129+
}
130+
131+
var primer = function(a){return a.toUpperCase()} ;
132+
133+
if('port' == sortBy) {
134+
primer = parseInt;
135+
}
136+
137+
var sortOrder = 'asc';
138+
139+
if(queries.hasOwnProperty('sort_order') && queries['sort_order'] == 'desc') {
140+
sortOrder = 'desc';
141+
}
142+
result.slice().sort(utility.customSort(sortBy, sortOrder, primer));
143+
}
144+
145+
return result;
146+
}
147+
148+

0 commit comments

Comments
 (0)