-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestInMemoryProcessor.js
134 lines (120 loc) · 3.89 KB
/
restInMemoryProcessor.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
// module.exports.restInMemoryProcessor =
exports = module.exports =function() {
var initializeCrudlRoutes = function(app, listUrl, getUrl, postUrl, postPropertyToAutoIncrement, putUrl, deleteUrl, jsonDataList) {
var __ = require("underscore"),
autoIncrementKey = postPropertyToAutoIncrement,
data = jsonDataList || [];
var getItemFromCache = function(filter) {
return __.findWhere(data, filter);
};
var getKeysFromUrl = function(req) {
var filter = {};
for (key in req.params) {
if (key===autoIncrementKey)
{
filter[key] = parseInt(req.params[key]);
}
else
{
filter[key] = req.params[key];
}
}
console.log("filter = " + JSON.stringify(filter));
// Need to make the object into JSON
return JSON.parse(JSON.stringify(filter));
};
// Register List endpoint
if (listUrl) {
app.get(listUrl, function(req, res){
var body = JSON.stringify(data) || "{}";
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', body.length);
res.end(body);
});
}
// Get an item
if (getUrl) {
app.get(getUrl, function(req, res){
var item = getItemFromCache(getKeysFromUrl(req));
if (item) {
var body = JSON.stringify(item) || "{}";
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', body.length);
res.end(body);
}
else {
var body = '{"error": "No item found with the required parameters. ' + req.params + '"}';
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', body.length);
res.status(404);
res.end(body);
}
});
}
// Create an item
if (postUrl) {
app.post(postUrl, function(req, res){
var val = req.body[autoIncrementKey] ? req.body[autoIncrementKey] : 0;
var filter = __.extend(req.params, { autoIncrementKey: val });
var item = getItemFromCache(getKeysFromUrl(filter));
if (item) {
var body = '{ "error": "Duplicate value found" }';
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', body.length);
res.status(409);
res.end(body);
}
else {
var newId = __.max(data, function(dataItem) {
return ((dataItem && dataItem[autoIncrementKey]) ? dataItem[autoIncrementKey] + 1 : null ) || 1;
});
if (newId > 999999999 || newId < 1) {
newId = 1;
}
item = req.body;
item[postPropertyToAutoIncrement] = newId;
data.push(item);
var body = JSON.stringify(item) || "{}";
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', body.length);
res.status(201);
res.end(body);
}
});
}
// Update an item
if (putUrl) {
app.put(putUrl, function(req, res){
var item = getItemFromCache(getKeysFromUrl(req));
if (item) {
__.extend(item, JSON.parse(JSON.stringify(req.body)));
var body = JSON.stringify(item) || "{}";
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', body.length);
res.end(body);
}
else {
var body = '{"error": "No item found with the required parameters. ' + req.params + '"}';
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', body.length);
res.status(404);
res.end(body);
}
});
}
if (deleteUrl) {
app.delete(deleteUrl, function(req, res){
var item = getItemFromCache(getKeysFromUrl(req));
if (item) {
data = __.reject(data, function(dataItem){ return dataItem === item; });
}
var body = '';
res.setHeader('Content-Type', 'application/json');
res.setHeader('Content-Length', body.length);
res.status(204);
res.end(body);
});
}
};
return { initializeCrudlRoutes: initializeCrudlRoutes };
};