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