feat(associate): routes to associate objects with each other
This commit is contained in:
37
src/associations/associate.js
Normal file
37
src/associations/associate.js
Normal file
@ -0,0 +1,37 @@
|
||||
import joi from 'joi';
|
||||
import error from '../error';
|
||||
import { capitalize } from 'lodash/string';
|
||||
|
||||
let prefix;
|
||||
|
||||
export default (server, a, b, options) => {
|
||||
prefix = options.prefix;
|
||||
|
||||
console.log(`${prefix}/associate/${a._singular}/{aid}/${b._singular}/{bid}`);
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: `${prefix}/associate/${a._singular}/{aid}/${b._singular}/{bid}`,
|
||||
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
let instanceb = await b.findOne({
|
||||
where: {
|
||||
id: request.params.bid
|
||||
}
|
||||
});
|
||||
|
||||
let instancea = await a.findOne({
|
||||
where: {
|
||||
id: request.params.aid
|
||||
}
|
||||
});
|
||||
|
||||
let fna = (instancea['add' + b.name] || instancea['set' + b.name]).bind(instancea);
|
||||
let fnb = (instanceb['add' + a.name] || instanceb['set' + a.name]).bind(instanceb);
|
||||
await fna(instanceb);
|
||||
await fnb(instancea);
|
||||
|
||||
reply(instancea);
|
||||
}
|
||||
})
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
import oneToOne from './one-to-one';
|
||||
import oneToMany from './one-to-many';
|
||||
import associate from './associate';
|
||||
|
||||
export { oneToOne, oneToMany };
|
||||
export { oneToOne, oneToMany, associate };
|
||||
|
Reference in New Issue
Block a user