Merge pull request #522 from clarkorz/feature/link-with-data
Support data field as body for link operation
This commit is contained in:
commit
28754519d4
|
@ -459,14 +459,20 @@ Model.hasManyRemoting = function (relationName, relation, define) {
|
|||
|
||||
if (relation.modelThrough || relation.type === 'referencesMany') {
|
||||
var modelThrough = relation.modelThrough || relation.modelTo;
|
||||
|
||||
|
||||
var accepts = [];
|
||||
if (relation.type === 'hasMany' && relation.modelThrough) {
|
||||
// Restrict: only hasManyThrough relation can have additional properties
|
||||
accepts.push({arg: 'data', type: modelThrough.modelName, http: {source: 'body'}});
|
||||
}
|
||||
|
||||
var addFunc = this.prototype['__link__' + relationName];
|
||||
define('__link__' + relationName, {
|
||||
isStatic: false,
|
||||
http: {verb: 'put', path: '/' + pathName + '/rel/:fk'},
|
||||
accepts: {arg: 'fk', type: 'any',
|
||||
accepts: [{arg: 'fk', type: 'any',
|
||||
description: 'Foreign key for ' + relationName, required: true,
|
||||
http: {source: 'path'}},
|
||||
http: {source: 'path'}}].concat(accepts),
|
||||
description: 'Add a related item by id for ' + relationName,
|
||||
returns: {arg: relationName, type: modelThrough.modelName, root: true}
|
||||
}, addFunc);
|
||||
|
|
|
@ -296,6 +296,51 @@ describe('relations - integration', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('PUT /physicians/:id/patients/rel/:fk with data', function () {
|
||||
|
||||
before(function (done) {
|
||||
var self = this;
|
||||
setup(false, function (err, root) {
|
||||
self.url = root.relUrl;
|
||||
self.patient = root.patient;
|
||||
self.physician = root.physician;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
var NOW = Date.now();
|
||||
var data = { date: new Date(NOW) };
|
||||
|
||||
lt.describe.whenCalledRemotely('PUT', '/api/physicians/:id/patients/rel/:fk', data, function () {
|
||||
it('should succeed with statusCode 200', function () {
|
||||
assert.equal(this.res.statusCode, 200);
|
||||
assert.equal(this.res.body.patientId, this.patient.id);
|
||||
assert.equal(this.res.body.physicianId, this.physician.id);
|
||||
assert.equal(new Date(this.res.body.date).getTime(), NOW);
|
||||
});
|
||||
|
||||
it('should create a record in appointment', function (done) {
|
||||
var self = this;
|
||||
app.models.appointment.find(function (err, apps) {
|
||||
assert.equal(apps.length, 1);
|
||||
assert.equal(apps[0].patientId, self.patient.id);
|
||||
assert.equal(apps[0].physicianId, self.physician.id);
|
||||
assert.equal(apps[0].date.getTime(), NOW);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should connect physician to patient', function (done) {
|
||||
var self = this;
|
||||
self.physician.patients(function (err, patients) {
|
||||
assert.equal(patients.length, 1);
|
||||
assert.equal(patients[0].id, self.patient.id);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('HEAD /physicians/:id/patients/rel/:fk', function () {
|
||||
|
||||
before(function (done) {
|
||||
|
|
Loading…
Reference in New Issue