Skip to content

Commit 78e179e

Browse files
committed
feat(gen:endpoint): change PUT to do an upsert instead of an update
1 parent 2727cc6 commit 78e179e

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

templates/endpoint/basename.controller.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ function respondWithResult(res, statusCode) {
2222
};
2323
}
2424

25+
function saveUpsert(new) {
26+
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+
}
36+
2537
function saveUpdates(updates) {
2638
return function(entity) {
2739
<%_ if(filters.mongooseModels) { -%>
@@ -93,6 +105,23 @@ export function create(req, res) {
93105
.catch(handleError(res));
94106
}
95107

108+
// Upserts the given <%= classedName %> in the DB at the specified ID
109+
export function upsert(req, res) {
110+
if(req.body._id) {
111+
delete req.body._id;
112+
}
113+
<%_ if(filters.mongooseModels) { -%>
114+
return <%= classedName %>.findOneAndUpdate(req.params.id, req.body, {upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()<% } %>
115+
<%_ if(filters.sequelizeModels) { -%>
116+
return <%= classedName %>.upsert(req.body, {
117+
where: {
118+
_id: req.params.id
119+
}
120+
})<% } %>
121+
.then(respondWithResult(res))
122+
.catch(handleError(res));
123+
}
124+
96125
// Updates an existing <%= classedName %> in the DB
97126
export function update(req, res) {
98127
if(req.body._id) {

templates/endpoint/index.js

Lines changed: 1 addition & 1 deletion
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.put('/:id', controller.update);
1211
router.patch('/:id', controller.update);
12+
router.put('/:id', controller.upsert);
1313
router.delete('/:id', controller.destroy);<% } %>
1414

1515
module.exports = router;

templates/endpoint/index.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var <%= cameledName %>CtrlStub = {
66
index: '<%= cameledName %>Ctrl.index'<% if(filters.models) { %>,
77
show: '<%= cameledName %>Ctrl.show',
88
create: '<%= cameledName %>Ctrl.create',
9-
update: '<%= cameledName %>Ctrl.update',
9+
upsert: '<%= cameledName %>Ctrl.upsert',
1010
destroy: '<%= cameledName %>Ctrl.destroy'<% } %>
1111
};
1212

@@ -58,9 +58,9 @@ describe('<%= classedName %> API Router:', function() {
5858
});
5959

6060
describe('PUT <%= route %>/:id', function() {
61-
it('should route to <%= cameledName %>.controller.update', function() {
61+
it('should route to <%= cameledName %>.controller.upsert', function() {
6262
<%= expect() %>routerStub.put
63-
.withArgs('/:id', '<%= cameledName %>Ctrl.update')
63+
.withArgs('/:id', '<%= cameledName %>Ctrl.upsert')
6464
<%= to() %>.have.been.calledOnce;
6565
});
6666
});

0 commit comments

Comments
 (0)