Merge pull request #415 from strongloop/feature/add-remoting-for-exists

Map exists to HEAD for REST
This commit is contained in:
Raymond Feng 2014-07-30 10:22:16 -07:00
commit 7fe68a6ee1
2 changed files with 55 additions and 1 deletions

View File

@ -420,7 +420,22 @@ Model.hasManyRemoting = function (relationName, relation, define) {
description: 'Foreign key for ' + relationName, required: true,
http: {source: 'path'}},
description: 'Check the existence of ' + relationName + ' relation to an item by id',
returns: {}
returns: {arg: 'exists', type: 'boolean', root: true},
rest: {
// After hook to map exists to 200/404 for HEAD
after: function(ctx, cb) {
if(ctx.result === false) {
var modelName = ctx.method.sharedClass.name;
var id = ctx.getArgByName('id');
var msg = 'Unknown "' + modelName + '" id "' + id + '".';
var error = new Error(msg);
error.statusCode = error.status = 404;
cb(error);
} else {
cb();
}
}
}
}, existsFunc);
}
};

View File

@ -213,6 +213,45 @@ describe('relations - integration', function () {
});
});
describe('HEAD /physicians/:id/patients/rel/:fk', function () {
before(function (done) {
var self = this;
setup(true, function (err, root) {
self.url = root.relUrl;
self.patient = root.patient;
self.physician = root.physician;
done();
});
});
lt.describe.whenCalledRemotely('HEAD', '/api/physicians/:id/patients/rel/:fk', function () {
it('should succeed with statusCode 200', function () {
assert.equal(this.res.statusCode, 200);
});
});
});
describe('HEAD /physicians/:id/patients/rel/:fk that does not exist', function () {
before(function (done) {
var self = this;
setup(true, function (err, root) {
self.url = '/api/physicians/' + root.physician.id
+ '/patients/rel/' + '999';
self.patient = root.patient;
self.physician = root.physician;
done();
});
});
lt.describe.whenCalledRemotely('HEAD', '/api/physicians/:id/patients/rel/:fk', function () {
it('should succeed with statusCode 404', function () {
assert.equal(this.res.statusCode, 404);
});
});
});
describe('DELETE /physicians/:id/patients/rel/:fk', function () {
before(function (done) {