Skip to content

Commit da19380

Browse files
committed
feat(gen:endpoint): change PATCH to do a patch
add fast-json-patch dep
1 parent 78e179e commit da19380

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

Diff for: templates/app/_package.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"errorhandler": "^1.4.2",
3131
"compression": "^1.5.2",
3232
"composable-middleware": "^0.3.0",
33+
"fast-json-patch": "^1.0.0",
3334
"lodash": "^4.6.1",
3435
"lusca": "^1.3.0",
3536
"babel-runtime": "^6.6.1",

Diff for: templates/endpoint/basename.controller.js

+12-23
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
* GET <%= route %> -> index<% if(filters.models) { %>
44
* POST <%= route %> -> create
55
* GET <%= route %>/:id -> show
6-
* PUT <%= route %>/:id -> update
6+
* PUT <%= route %>/:id -> upsert
7+
* PATCH <%= route %>/:id -> patch
78
* DELETE <%= route %>/:id -> destroy<% } %>
89
*/
910

1011
'use strict';<% if(filters.models) { %>
1112

12-
import _ from 'lodash';<% if(filters.mongooseModels) { %>
13+
import jsonpatch from 'fast-json-patch';<% if(filters.mongooseModels) { %>
1314
import <%= classedName %> from './<%= basename %>.model';<% } if(filters.sequelizeModels) { %>
1415
import {<%= classedName %>} from '<%= relativeRequire(config.get('registerModelsFile')) %>';<% } %>
1516

@@ -22,27 +23,15 @@ function respondWithResult(res, statusCode) {
2223
};
2324
}
2425

25-
function saveUpsert(new) {
26+
function patchUpdates(patches) {
2627
return function(entity) {
27-
<%_ if(filters.mongooseModels) { -%>
28-
var updated = _.assignIn(entity, updates);
29-
return updated.save();
30-
<%_ } -%>
31-
<%_ if(filters.sequelizeModels) { -%>
32-
return entity.updateAttributes(updates);
33-
<%_ } -%>
34-
};
35-
}
28+
try {
29+
jsonpatch.apply(entity, patches, /*validate*/ true);
30+
} catch(err) {
31+
return Promise.reject(err);
32+
}
3633

37-
function saveUpdates(updates) {
38-
return function(entity) {
39-
<%_ if(filters.mongooseModels) { -%>
40-
var updated = _.merge(entity, updates);
41-
return updated.save();
42-
<%_ } -%>
43-
<%_ if(filters.sequelizeModels) { -%>
44-
return entity.updateAttributes(updates);
45-
<%_ } -%>
34+
return entity.save();
4635
};
4736
}
4837

@@ -123,7 +112,7 @@ export function upsert(req, res) {
123112
}
124113

125114
// Updates an existing <%= classedName %> in the DB
126-
export function update(req, res) {
115+
export function patch(req, res) {
127116
if(req.body._id) {
128117
delete req.body._id;
129118
}
@@ -134,7 +123,7 @@ export function update(req, res) {
134123
}
135124
})<% } %>
136125
.then(handleEntityNotFound(res))
137-
.then(saveUpdates(req.body))
126+
.then(patchUpdates(req.body))
138127
.then(respondWithResult(res))
139128
.catch(handleError(res));
140129
}

Diff for: templates/endpoint/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var router = express.Router();
88
router.get('/', controller.index);<% if (filters.models) { %>
99
router.get('/:id', controller.show);
1010
router.post('/', controller.create);
11-
router.patch('/:id', controller.update);
1211
router.put('/:id', controller.upsert);
12+
router.patch('/:id', controller.patch);
1313
router.delete('/:id', controller.destroy);<% } %>
1414

1515
module.exports = router;

Diff for: templates/endpoint/index.spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var <%= cameledName %>CtrlStub = {
77
show: '<%= cameledName %>Ctrl.show',
88
create: '<%= cameledName %>Ctrl.create',
99
upsert: '<%= cameledName %>Ctrl.upsert',
10+
patch: '<%= cameledName %>Ctrl.patch',
1011
destroy: '<%= cameledName %>Ctrl.destroy'<% } %>
1112
};
1213

@@ -66,9 +67,9 @@ describe('<%= classedName %> API Router:', function() {
6667
});
6768

6869
describe('PATCH <%= route %>/:id', function() {
69-
it('should route to <%= cameledName %>.controller.update', function() {
70+
it('should route to <%= cameledName %>.controller.patch', function() {
7071
<%= expect() %>routerStub.patch
71-
.withArgs('/:id', '<%= cameledName %>Ctrl.update')
72+
.withArgs('/:id', '<%= cameledName %>Ctrl.patch')
7273
<%= to() %>.have.been.calledOnce;
7374
});
7475
});

0 commit comments

Comments
 (0)