Skip to content

Commit 7dff9d3

Browse files
author
Oliver Rumbelow
committed
Merge pull request #150 from holidayextras/memory-store-clone
Clone all objects passing in-to and out-of the in memory store
2 parents 1595316 + d1462fc commit 7dff9d3

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

lib/MemoryHandler.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ MemoryStore.prototype.search = function(request, callback) {
3636
if (request.params.page) {
3737
results = results.slice(request.params.page.offset, request.params.page.offset + request.params.page.limit);
3838
}
39-
return callback(null, results, resultCount);
39+
return callback(null, MemoryStore._clone(results), resultCount);
4040
};
4141

4242
/**
@@ -59,17 +59,27 @@ MemoryStore.prototype.find = function(request, callback) {
5959
}
6060

6161
// Return the requested resource
62-
return callback(null, theResource);
62+
return callback(null, MemoryStore._clone(theResource));
6363
};
6464

6565
/**
66-
Create (store) a new resource give a resource type and an object.
66+
Create (store) a new resource given a resource type and an object.
6767
*/
6868
MemoryStore.prototype.create = function(request, newResource, callback) {
69+
// Check to see if the ID already exists
70+
var index = MemoryStore._indexOf(resources[request.params.type], newResource);
71+
if (index !== -1) {
72+
return callback({
73+
status: "403",
74+
code: "EFORBIDDEN",
75+
title: "Requested resource already exists",
76+
detail: "The requested resource already exists of type " + request.params.type + " with id " + request.params.id
77+
});
78+
}
6979
// Push the newResource into our in-memory store.
7080
resources[request.params.type].push(newResource);
7181
// Return the newly created resource
72-
return callback(null, newResource);
82+
return callback(null, MemoryStore._clone(newResource));
7383
};
7484

7585
/**
@@ -80,9 +90,9 @@ MemoryStore.prototype.delete = function(request, callback) {
8090
this.find(request, function(err, theResource) {
8191
if (err) return callback(err);
8292

83-
// Remove the resource from the in-meory store.
84-
var resourceIndex = resources[request.params.type].indexOf(theResource);
85-
resources[request.params.type].splice(resourceIndex, 1);
93+
// Remove the resource from the in-memory store.
94+
var index = MemoryStore._indexOf(resources[request.params.type], theResource);
95+
resources[request.params.type].splice(index, 1);
8696

8797
// Return with no error
8898
return callback();
@@ -102,10 +112,11 @@ MemoryStore.prototype.update = function(request, partialResource, callback) {
102112
theResource = _.assign(theResource, partialResource);
103113

104114
// Push the newly updated resource back into the in-memory store
105-
resources[request.params.type][request.params.id] = theResource;
115+
var index = MemoryStore._indexOf(resources[request.params.type], theResource);
116+
resources[request.params.type][index] = theResource;
106117

107118
// Return the newly updated resource
108-
return callback(null, theResource);
119+
return callback(null, MemoryStore._clone(theResource));
109120
});
110121
};
111122

@@ -133,3 +144,14 @@ MemoryStore.prototype._sortList = function(request, list) {
133144
}
134145
});
135146
};
147+
148+
MemoryStore._clone = function(obj) {
149+
return JSON.parse(JSON.stringify(obj));
150+
};
151+
152+
MemoryStore._indexOf = function(list, obj) {
153+
for (var i in list) {
154+
if (list[i].id === obj.id) return i;
155+
}
156+
return -1;
157+
};

0 commit comments

Comments
 (0)