-
Notifications
You must be signed in to change notification settings - Fork 1
/
configurationController.js
148 lines (131 loc) · 5.48 KB
/
configurationController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var configGenerator = require('./configGenerator');
var utility = require('./utility');
var pageSize = 5;
var existingConfigurations = configGenerator.generateConfigs(30);
var acceptedSortByProperties = ['hostname', 'name', 'username', 'port'];
module.exports = {
process: function process(method, requestedResource, key, value, requestBody, queries) {
var result = {
success: true,
statusCode: 200,
header: {"Content-Type": "application/json"}
};
var data, matchingConfig;
if (method === 'POST' || method === 'PUT') {
data = JSON.parse(requestBody);
}
switch (method) {
case 'GET':
if (!key) {
if(Object.keys(queries).length) {
result.message = JSON.stringify(massageData(existingConfigurations, queries));
} else {
result.message = JSON.stringify(existingConfigurations); //return all configurations.
}
} else {
var matchingConfigs = existingConfigurations.filter(function (el) {
return el[key] == value;
});
if (matchingConfigs) {
if(Object.keys(queries).length) {
result.message = JSON.stringify(massageData(matchingConfigs, queries));
} else {
result.message = JSON.stringify(matchingConfigs);
}
} else {
result.statusCode = 404;
result.success = false;
result.message = 'No configuration found with key ' + key + ' and value ' + value;
}
}
break;
case 'POST':
if (data) {
data.id = Math.random();
existingConfigurations.push(data);
result.message = 'Created new config with id ' + data.id;
result.statusCode = 201;
result.header.location = "localhost:3000/configuration/id/" + data.id;
} else {
result.statusCode = 400;
result.success = false;
result.message = 'Please supply data to create new config.';
}
break;
case 'PUT':
if ('id' === key) {
matchingConfig = getMatchingConfig(key, value);
if (!matchingConfig) {
result.message = 'No matching config found to update for id ' + value;
} else if (matchingConfig[key] !== data[key]) {
result.message = 'Config ID in the path ' + value + ' does not match the config ID ' + data[key] + ' in the request body.';
} else {
for (var k in data) {
if (data.hasOwnProperty(k) && k !== 'id') {
matchingConfig[k] = data[k]
}
}
result.message = 'Config with id ' + value + ' has been updated.';
}
} else {
result.statusCode = 400;
result.success = false;
result.message = 'Please provide config unique ID to update an existing config.';
}
break;
case 'DELETE':
if ('id' === key) {
matchingConfig = getMatchingConfig(key, value);
existingConfigurations.splice(existingConfigurations.indexOf(matchingConfig), 1);
result.message = 'Config with id ' + value + ' has been deleted.';
} else {
result.statusCode = 400;
result.success = false;
result.message = 'Please provide config unique ID to delete an existing config.';
}
break;
default:
result.statusCode = 400;
result.success = false;
result.message = 'This action is not supported.';
break;
}
return result;
}
};
/*
Returns the first config that matches the key value pair. Use it isolation only when you are expecting unique key.
*/
function getMatchingConfig(key, value) {
for (var index in existingConfigurations) {
var config = existingConfigurations[index];
if (config[key] == value) {
return config;
}
}
}
function massageData(configs, queries) {
var result = configs;
if(queries.hasOwnProperty('page')) {
if(queries['page'] > 0) {
var startIndex = (queries['page'] - 1) * 5;
result = configs.slice(startIndex, startIndex + pageSize)
}
}
if(queries.hasOwnProperty('sort_by')) {
var sortBy = queries['sort_by'];
if(acceptedSortByProperties.indexOf(sortBy) < 0) {
return result;
}
var primer = function(a){return a.toUpperCase()} ;
if('port' == sortBy) {
primer = parseInt;
}
var sortOrder = 'asc';
if(queries.hasOwnProperty('sort_order') && queries['sort_order'] == 'desc') {
sortOrder = 'desc';
}
result.slice().sort(utility.customSort(sortBy, sortOrder, primer));
}
return result;
}