Skip to content

Put patch #2033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/test/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function copyAsync(src, dest) {
*/
export function runCmd(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, {}, function(err, stdout, stderr) {
exec(cmd, {maxBuffer: 1024 * 500}, function(err, stdout, stderr) {
if(err) {
console.error(stdout);
return reject(err);
Expand Down
1 change: 1 addition & 0 deletions templates/app/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"errorhandler": "^1.4.2",
"compression": "^1.5.2",
"composable-middleware": "^0.3.0",
"fast-json-patch": "^1.0.0",
"lodash": "^4.6.1",
"lusca": "^1.3.0",
"babel-runtime": "^6.6.1",
Expand Down
42 changes: 30 additions & 12 deletions templates/endpoint/basename.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* GET <%= route %> -> index<% if(filters.models) { %>
* POST <%= route %> -> create
* GET <%= route %>/:id -> show
* PUT <%= route %>/:id -> update
* PUT <%= route %>/:id -> upsert
* PATCH <%= route %>/:id -> patch
* DELETE <%= route %>/:id -> destroy<% } %>
*/

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

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

Expand All @@ -22,15 +23,15 @@ function respondWithResult(res, statusCode) {
};
}

function saveUpdates(updates) {
function patchUpdates(patches) {
return function(entity) {
<%_ if(filters.mongooseModels) { -%>
var updated = _.merge(entity, updates);
return updated.save();
<%_ } -%>
<%_ if(filters.sequelizeModels) { -%>
return entity.updateAttributes(updates);
<%_ } -%>
try {
jsonpatch.apply(entity, patches, /*validate*/ true);
} catch(err) {
return Promise.reject(err);
}

return entity.save();
};
}

Expand Down Expand Up @@ -93,8 +94,25 @@ export function create(req, res) {
.catch(handleError(res));
}

// Upserts the given <%= classedName %> in the DB at the specified ID
export function upsert(req, res) {
if(req.body._id) {
delete req.body._id;
}
<%_ if(filters.mongooseModels) { -%>
return <%= classedName %>.findOneAndUpdate(req.params.id, req.body, {upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()<% } %>
<%_ if(filters.sequelizeModels) { -%>
return <%= classedName %>.upsert(req.body, {
where: {
_id: req.params.id
}
})<% } %>
.then(respondWithResult(res))
.catch(handleError(res));
}

// Updates an existing <%= classedName %> in the DB
export function update(req, res) {
export function patch(req, res) {
if(req.body._id) {
delete req.body._id;
}
Expand All @@ -105,7 +123,7 @@ export function update(req, res) {
}
})<% } %>
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(patchUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
Expand Down
63 changes: 56 additions & 7 deletions templates/endpoint/basename.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('<%= classedName %> API:', function() {

beforeEach(function(done) {
request(app)
.get('<%= route %>/' + new<%= classedName %>._id)
.get(`<%= route %>/${new<%= classedName %>._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
Expand All @@ -85,7 +85,7 @@ describe('<%= classedName %> API:', function() {

beforeEach(function(done) {
request(app)
.put('<%= route %>/' + new<%= classedName %>._id)
.put(`<%= route %>/${new<%= classedName %>._id}`)
.send({
name: 'Updated <%= classedName %>',
info: 'This is the updated <%= cameledName %>!!!'
Expand All @@ -105,16 +105,65 @@ describe('<%= classedName %> API:', function() {
updated<%= classedName %> = {};
});

it('should respond with the updated <%= cameledName %>', function() {
<%= expect() %>updated<%= classedName %>.name<%= to() %>.equal('Updated <%= classedName %>');
<%= expect() %>updated<%= classedName %>.info<%= to() %>.equal('This is the updated <%= cameledName %>!!!');
it('should respond with the original <%= cameledName %>', function() {
<%= expect() %>updated<%= classedName %>.name<%= to() %>.equal('New <%= classedName %>');
<%= expect() %>updated<%= classedName %>.info<%= to() %>.equal('This is the brand new <%= cameledName %>!!!');
});

it('should respond with the updated <%= cameledName %> on a subsequent GET', function(done) {
request(app)
.get(`<%= route %>/${new<%= classedName %>._id}`)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if(err) {
return done(err);
}
let <%= cameledName %> = res.body;

<%= expect() %><%= cameledName %>.name<%= to() %>.equal('Updated <%= classedName %>');
<%= expect() %><%= cameledName %>.info<%= to() %>.equal('This is the updated <%= cameledName %>!!!');

done();
});
});
});

describe('PATCH <%= route %>/:id', function() {
var patched<%= classedName %>;

beforeEach(function(done) {
request(app)
.patch(`<%= route %>/${new<%= classedName %>._id}`)
.send([
{ op: 'replace', path: '/name', value: 'Patched <%= classedName %>' },
{ op: 'replace', path: '/info', value: 'This is the patched <%= cameledName %>!!!' }
])
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if(err) {
return done(err);
}
patched<%= classedName %> = res.body;
done();
});
});

afterEach(function() {
patched<%= classedName %> = {};
});

it('should respond with the patched <%= cameledName %>', function() {
<%= expect() %>patched<%= classedName %>.name<%= to() %>.equal('Patched <%= classedName %>');
<%= expect() %>patched<%= classedName %>.info<%= to() %>.equal('This is the patched <%= cameledName %>!!!');
});
});

describe('DELETE <%= route %>/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete('<%= route %>/' + new<%= classedName %>._id)
.delete(`<%= route %>/${new<%= classedName %>._id}`)
.expect(204)
.end(err => {
if(err) {
Expand All @@ -126,7 +175,7 @@ describe('<%= classedName %> API:', function() {

it('should respond with 404 when <%= cameledName %> does not exist', function(done) {
request(app)
.delete('<%= route %>/' + new<%= classedName %>._id)
.delete(`<%= route %>/${new<%= classedName %>._id}`)
.expect(404)
.end(err => {
if(err) {
Expand Down
4 changes: 2 additions & 2 deletions templates/endpoint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var router = express.Router();
router.get('/', controller.index);<% if (filters.models) { %>
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.update);
router.patch('/:id', controller.update);
router.put('/:id', controller.upsert);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.destroy);<% } %>

module.exports = router;
11 changes: 6 additions & 5 deletions templates/endpoint/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var <%= cameledName %>CtrlStub = {
index: '<%= cameledName %>Ctrl.index'<% if(filters.models) { %>,
show: '<%= cameledName %>Ctrl.show',
create: '<%= cameledName %>Ctrl.create',
update: '<%= cameledName %>Ctrl.update',
upsert: '<%= cameledName %>Ctrl.upsert',
patch: '<%= cameledName %>Ctrl.patch',
destroy: '<%= cameledName %>Ctrl.destroy'<% } %>
};

Expand Down Expand Up @@ -58,17 +59,17 @@ describe('<%= classedName %> API Router:', function() {
});

describe('PUT <%= route %>/:id', function() {
it('should route to <%= cameledName %>.controller.update', function() {
it('should route to <%= cameledName %>.controller.upsert', function() {
<%= expect() %>routerStub.put
.withArgs('/:id', '<%= cameledName %>Ctrl.update')
.withArgs('/:id', '<%= cameledName %>Ctrl.upsert')
<%= to() %>.have.been.calledOnce;
});
});

describe('PATCH <%= route %>/:id', function() {
it('should route to <%= cameledName %>.controller.update', function() {
it('should route to <%= cameledName %>.controller.patch', function() {
<%= expect() %>routerStub.patch
.withArgs('/:id', '<%= cameledName %>Ctrl.update')
.withArgs('/:id', '<%= cameledName %>Ctrl.patch')
<%= to() %>.have.been.calledOnce;
});
});
Expand Down