2016-01-26 18:03:52 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JSON API helper
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
default: {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse request
|
|
|
|
*/
|
|
|
|
|
2016-01-27 16:45:44 +01:00
|
|
|
parse: function * (ctx, cb) {
|
2016-01-27 14:20:09 +01:00
|
|
|
switch (ctx.method.toUpperCase()) {
|
|
|
|
case 'GET':
|
|
|
|
console.log('GET');
|
|
|
|
break;
|
|
|
|
case 'PUT':
|
|
|
|
case 'POST':
|
2016-01-27 16:45:44 +01:00
|
|
|
yield this.fetchSchema(ctx, function * (err) {
|
|
|
|
yield cb(err);
|
2016-01-27 14:20:09 +01:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'DELETE':
|
|
|
|
console.log('DELETE');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch attributes schema
|
|
|
|
*/
|
|
|
|
|
2016-01-27 16:45:44 +01:00
|
|
|
fetchSchema: function * (ctx, cb) {
|
2016-01-27 14:20:09 +01:00
|
|
|
const attributes = ctx.request.body;
|
|
|
|
|
|
|
|
if (!attributes.hasOwnProperty('data')) {
|
2016-01-27 16:45:44 +01:00
|
|
|
return yield cb({
|
|
|
|
status: 403,
|
|
|
|
body: 'Missing `data` member'
|
|
|
|
});
|
2016-01-27 14:20:09 +01:00
|
|
|
} else if (!attributes.data.hasOwnProperty('type')) {
|
2016-01-27 16:45:44 +01:00
|
|
|
return yield cb({
|
|
|
|
status: 403,
|
|
|
|
body: 'Missing `type` member'
|
|
|
|
});
|
2016-01-27 14:20:09 +01:00
|
|
|
} else if (!strapi.models.hasOwnProperty(attributes.data.type)) {
|
2016-01-27 16:45:44 +01:00
|
|
|
return yield cb({
|
|
|
|
status: 403,
|
|
|
|
body: 'Unknow `type` ' + attributes.data.type
|
|
|
|
});
|
2016-01-27 14:20:09 +01:00
|
|
|
}
|
2016-01-27 16:45:44 +01:00
|
|
|
|
|
|
|
// Extract required attributes
|
|
|
|
const requiredAttributes = _.omit(_.mapValues(strapi.models[attributes.data.type].attributes, function (attr) {
|
|
|
|
return attr.required ? attr : undefined;
|
|
|
|
}), _.isUndefined);
|
|
|
|
// Identify missing attributes
|
|
|
|
const missingAttributes = _.difference(_.keys(requiredAttributes), _.keys(attributes.data.attributes));
|
|
|
|
|
|
|
|
if (!_.isEmpty(missingAttributes)) {
|
|
|
|
return yield cb({
|
|
|
|
status: 403,
|
|
|
|
body: 'Missing required attributes (' + missingAttributes.toString() + ')'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Identify required relationships
|
|
|
|
const relationships = _.indexBy(strapi.models[attributes.data.type].associations, 'alias');
|
|
|
|
// Extract required relationships
|
|
|
|
const requiredRelationships = _.intersection(_.keys(requiredAttributes), _.keys(relationships));
|
|
|
|
// Identify missing relationships
|
|
|
|
const missingRelationships = _.difference(_.keys(requiredRelationships), _.keys(attributes.data.relationships));
|
|
|
|
|
|
|
|
if (!_.isEmpty(missingRelationships)) {
|
|
|
|
return yield cb({
|
|
|
|
status: 403,
|
|
|
|
body: 'Missing required relationships (' + missingRelationships.toString() + ')'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looks good
|
|
|
|
yield cb();
|
|
|
|
|
2016-01-27 14:20:09 +01:00
|
|
|
}
|
2016-01-26 18:03:52 +01:00
|
|
|
};
|