mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 02:16:03 +00:00
Handle related ressource without include parameter
This commit is contained in:
parent
fc970e6538
commit
38f9239ec9
@ -22,9 +22,19 @@ module.exports = {
|
||||
|
||||
set: function (ctx, matchedRoute, actionRoute) {
|
||||
const object = this.getObject(matchedRoute);
|
||||
const type = this.getType(ctx, actionRoute.controller, object).toLowerCase();
|
||||
const value = this.verifyAndSetValue(ctx, object);
|
||||
const type = this.getType(ctx, actionRoute.controller);
|
||||
|
||||
// Fetch a relationship that does not exist
|
||||
if (_.isUndefined(type)) {
|
||||
ctx.response.status = 404;
|
||||
ctx.response.body = '';
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Fetch and format value
|
||||
const value = this.fetchValue(ctx, object);
|
||||
|
||||
ctx.response.body = this.serialize(ctx, type, object, value);
|
||||
},
|
||||
|
||||
@ -90,6 +100,7 @@ module.exports = {
|
||||
toSerialize.topLevelLinks.related = toSerialize.topLevelLinks.self.replace('relationships/', '');
|
||||
break;
|
||||
case 'related':
|
||||
this.includedRelationShips(ctx, toSerialize, type);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -158,66 +169,43 @@ module.exports = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Verify type of the value
|
||||
* Fetch and format value
|
||||
*/
|
||||
|
||||
verifyAndSetValue: function (ctx, object) {
|
||||
fetchValue: function (ctx, object) {
|
||||
const data = ctx.body;
|
||||
|
||||
switch (ctx.response.status) {
|
||||
case 404:
|
||||
ctx.status = 200;
|
||||
ctx.body = null;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
switch (object) {
|
||||
case 'collection':
|
||||
// Collection
|
||||
if (_.isArray(data) && _.size(data) > 1) {
|
||||
return data;
|
||||
} else if (_.isArray(data) && (_.size(data) === 1 || _.size(data) === 0)) {
|
||||
return _.isObject(data[0]) ? data[0] : [];
|
||||
} else {
|
||||
return null;
|
||||
return _.isObject(_.first(data)) ? _.first(data[0]) : [];
|
||||
}
|
||||
break;
|
||||
|
||||
return null;
|
||||
case 'ressource':
|
||||
// Ressource
|
||||
if (_.isObject(data)) {
|
||||
return data;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
|
||||
return null;
|
||||
case 'related':
|
||||
case 'relationships':
|
||||
// TODO:
|
||||
// - Detect object of relation
|
||||
// - MtM, OtM: array
|
||||
// - OtO, MtO: object
|
||||
|
||||
// Relationships
|
||||
if (_.isObject(data) || _.isArray(data) && data.hasOwnProperty(ctx.params.relation)) {
|
||||
return data[ctx.params.relation];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
case 'related':
|
||||
// TODO:
|
||||
// - Detect object of relation
|
||||
// - MtM, OtM: array
|
||||
// - OtO, MtO: object
|
||||
if (_.isArray(data[ctx.params.relation]) && _.size(data[ctx.params.relation]) > 1) {
|
||||
return data[ctx.params.relation];
|
||||
}
|
||||
|
||||
// Related
|
||||
if (_.isObject(data) || _.isArray(data)) {
|
||||
return data;
|
||||
} else {
|
||||
return null;
|
||||
return _.first(data[ctx.params.relation]) || data[ctx.params.relation];
|
||||
}
|
||||
break;
|
||||
|
||||
return null;
|
||||
default:
|
||||
return 'collection';
|
||||
}
|
||||
@ -236,7 +224,7 @@ module.exports = {
|
||||
case 1:
|
||||
return 'ressource';
|
||||
case 2:
|
||||
return (matchedRoute.path.indexOf('relationships')) ? 'relationships' : 'related';
|
||||
return (matchedRoute.path.indexOf('relationships') !== -1) ? 'relationships' : 'related';
|
||||
default:
|
||||
return 'collection';
|
||||
}
|
||||
@ -246,24 +234,20 @@ module.exports = {
|
||||
* Find data type
|
||||
*/
|
||||
|
||||
getType: function (ctx, supposedType, object) {
|
||||
getType: function (ctx, supposedType) {
|
||||
// TODO:
|
||||
// - Parse the URL and try to extract useful information to find the type
|
||||
|
||||
if (strapi.models.hasOwnProperty(supposedType.toLowerCase())) {
|
||||
switch (object) {
|
||||
case 'relationships':
|
||||
return _.first(_.reject(_.map(strapi.models[supposedType.toLowerCase()].associations, function (relation) {
|
||||
return (ctx.params.hasOwnProperty('relation') && ctx.params.relation === relation.alias) ? relation.model || relation.collection : undefined;
|
||||
}), _.isUndefined)) || supposedType.toLowerCase();
|
||||
case 'related':
|
||||
return supposedType.toLowerCase();
|
||||
default:
|
||||
return supposedType.toLowerCase();
|
||||
}
|
||||
// Relationships or related ressource
|
||||
if (strapi.models.hasOwnProperty(supposedType.toLowerCase()) && ctx.params.hasOwnProperty('relation')) {
|
||||
return _.first(_.reject(_.map(strapi.models[supposedType.toLowerCase()].associations, function (relation) {
|
||||
return (ctx.params.hasOwnProperty('relation') && ctx.params.relation === relation.alias) ? relation.model || relation.collection : undefined;
|
||||
}), _.isUndefined));
|
||||
} else if (strapi.models.hasOwnProperty(supposedType.toLowerCase())) {
|
||||
return supposedType.toLowerCase();
|
||||
}
|
||||
|
||||
return null;
|
||||
return undefined;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user