Skip to content

Commit 0fe18b0

Browse files
author
Ben Hanna
committed
Updated remove and update handlers to support all primary keys.
1 parent ce7b5e1 commit 0fe18b0

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

handlers/remove.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
var HttpStatusError = require('../errors/HttpStatusError');
1+
var _ = require('lodash'),
2+
qs = require('../parsers/qs'),
3+
HttpStatusError = require('../errors/HttpStatusError');
24

35
module.exports = init;
46

@@ -8,10 +10,16 @@ function init(model) {
810
];
911

1012
function remove(req, res, next) {
11-
var options = req.options || {};
12-
13-
options.where = options.where || {};
14-
options.where[model.primaryKeyAttribute] = req.params.id;
13+
var keys = {},
14+
options = {};
15+
16+
keys.model = _.keys(model.primaryKeyAttributes);
17+
keys.params = _.keys(req.params);
18+
keys.filters = _.intersection(keys.model, keys.params);
19+
20+
options.where = qs.filters(_.pick(req.params, keys.filters))
21+
22+
options = _.merge({}, options, req.options);
1523

1624
model
1725
.findOne(options)

handlers/update.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
var HttpStatusError = require('../errors/HttpStatusError'),
1+
var _ = require('lodash'),
2+
qs = require('../parsers/qs'),
3+
HttpStatusError = require('../errors/HttpStatusError'),
24
plainTransform = require('../transforms/plain');
35

46
module.exports = init;
@@ -11,10 +13,16 @@ function init(model) {
1113

1214
function update(req, res, next) {
1315
var body = req.body,
14-
options = req.options || {};
15-
16-
options.where = options.where || {};
17-
options.where[model.primaryKeyAttribute] = req.params.id;
16+
keys = {},
17+
options = {};
18+
19+
keys.model = _.keys(model.primaryKeyAttributes);
20+
keys.params = _.keys(req.params);
21+
keys.filters = _.intersection(keys.model, keys.params);
22+
23+
options.where = qs.filters(_.pick(req.params, keys.filters))
24+
25+
options = _.merge({}, options, req.options);
1826

1927
model
2028
.findOne(options)

0 commit comments

Comments
 (0)