Map not found to 404 for hasOne

This commit is contained in:
Raymond Feng 2015-02-03 11:13:15 -08:00
parent c7f1143f5b
commit 4bf5970f9c
1 changed files with 14 additions and 14 deletions

View File

@ -431,6 +431,17 @@ Model.belongsToRemoting = function(relationName, relation, define) {
}, fn);
};
function convertNullToNotFoundError(toModelName, ctx, cb) {
if (ctx.result !== null) return cb();
var fk = ctx.getArgByName('fk');
var msg = 'Unknown "' + toModelName + '" id "' + fk + '".';
var error = new Error(msg);
error.statusCode = error.status = 404;
error.code = 'MODEL_NOT_FOUND';
cb(error);
}
Model.hasOneRemoting = function(relationName, relation, define) {
var pathName = (relation.options.http && relation.options.http.path) || relationName;
var toModelName = relation.modelTo.modelName;
@ -441,7 +452,8 @@ Model.hasOneRemoting = function(relationName, relation, define) {
accepts: {arg: 'refresh', type: 'boolean', http: {source: 'query'}},
description: 'Fetches hasOne relation ' + relationName,
accessType: 'READ',
returns: {arg: relationName, type: relation.modelTo.modelName, root: true}
returns: {arg: relationName, type: relation.modelTo.modelName, root: true},
rest: {after: convertNullToNotFoundError.bind(null, toModelName)}
});
define('__create__' + relationName, {
@ -468,24 +480,12 @@ Model.hasOneRemoting = function(relationName, relation, define) {
description: 'Deletes ' + relationName + ' of this model.',
accessType: 'WRITE'
});
};
Model.hasManyRemoting = function(relationName, relation, define) {
var pathName = (relation.options.http && relation.options.http.path) || relationName;
var toModelName = relation.modelTo.modelName;
function convertNullToNotFoundError(ctx, cb) {
if (ctx.result !== null) return cb();
var fk = ctx.getArgByName('fk');
var msg = 'Unknown "' + toModelName + '" id "' + fk + '".';
var error = new Error(msg);
error.statusCode = error.status = 404;
error.code = 'MODEL_NOT_FOUND';
cb(error);
}
var findByIdFunc = this.prototype['__findById__' + relationName];
define('__findById__' + relationName, {
isStatic: false,
@ -496,7 +496,7 @@ Model.hasManyRemoting = function(relationName, relation, define) {
description: 'Find a related item by id for ' + relationName,
accessType: 'READ',
returns: {arg: 'result', type: toModelName, root: true},
rest: {after: convertNullToNotFoundError}
rest: {after: convertNullToNotFoundError.bind(null, toModelName)}
}, findByIdFunc);
var destroyByIdFunc = this.prototype['__destroyById__' + relationName];