mirror of
https://github.com/strapi/strapi.git
synced 2025-09-20 05:52:08 +00:00
fix 204 issues and use strapi.db.getModelByAssoc
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
parent
ba8509eac8
commit
7c8d51d15d
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const { getComponentAttributes, isComponent } = require('./utils/attributes');
|
const { getComponentAttributes, isComponent } = require('./utils/attributes');
|
||||||
const { findModelByAssoc, isPolymorphic } = require('./utils/associations');
|
const { isPolymorphic } = require('./utils/associations');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create utilities to populate a model on fetch
|
* Create utilities to populate a model on fetch
|
||||||
@ -54,7 +54,7 @@ const populateBareAssociations = (definition, { prefix = '' } = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const path = `${prefix}${assoc.alias}`;
|
const path = `${prefix}${assoc.alias}`;
|
||||||
const assocModel = findModelByAssoc({ assoc });
|
const assocModel = strapi.db.getModelByAssoc(assoc);
|
||||||
|
|
||||||
const polyAssocs = assocModel.associations
|
const polyAssocs = assocModel.associations
|
||||||
.filter(assoc => isPolymorphic({ assoc }))
|
.filter(assoc => isPolymorphic({ assoc }))
|
||||||
@ -72,7 +72,7 @@ const populateBareAssociations = (definition, { prefix = '' } = {}) => {
|
|||||||
|
|
||||||
const formatAssociationPopulate = ({ assoc, prefix = '' }) => {
|
const formatAssociationPopulate = ({ assoc, prefix = '' }) => {
|
||||||
const path = `${prefix}${assoc.alias}`;
|
const path = `${prefix}${assoc.alias}`;
|
||||||
const assocModel = findModelByAssoc({ assoc });
|
const assocModel = strapi.db.getModelByAssoc(assoc);
|
||||||
|
|
||||||
const polyAssocs = assocModel.associations
|
const polyAssocs = assocModel.associations
|
||||||
.filter(assoc => isPolymorphic({ assoc }))
|
.filter(assoc => isPolymorphic({ assoc }))
|
||||||
@ -176,7 +176,7 @@ const formatPopulateOptions = (definition, withRelated) => {
|
|||||||
|
|
||||||
if (!assoc) return acc;
|
if (!assoc) return acc;
|
||||||
|
|
||||||
tmpModel = findModelByAssoc({ assoc });
|
tmpModel = strapi.db.getModelByAssoc(assoc);
|
||||||
|
|
||||||
if (isPolymorphic({ assoc })) {
|
if (isPolymorphic({ assoc })) {
|
||||||
const path = formatPolymorphicPopulate({
|
const path = formatPolymorphicPopulate({
|
||||||
@ -206,7 +206,7 @@ const formatPolymorphicPopulate = ({ assoc, prefix = '' }) => {
|
|||||||
|
|
||||||
// oneToMorph or manyToMorph side.
|
// oneToMorph or manyToMorph side.
|
||||||
// Retrieve collection name because we are using it to build our hidden model.
|
// Retrieve collection name because we are using it to build our hidden model.
|
||||||
const model = findModelByAssoc({ assoc });
|
const model = strapi.db.getModelByAssoc(assoc);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
[`${prefix}${assoc.alias}.${model.collectionName}`]: function(query) {
|
[`${prefix}${assoc.alias}.${model.collectionName}`]: function(query) {
|
||||||
|
@ -1,14 +1,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const findModelByAssoc = ({ assoc }) => {
|
|
||||||
return strapi.db.getModelByAssoc(assoc);
|
|
||||||
};
|
|
||||||
|
|
||||||
const isPolymorphic = ({ assoc }) => {
|
const isPolymorphic = ({ assoc }) => {
|
||||||
return assoc.nature.toLowerCase().indexOf('morph') !== -1;
|
return assoc.nature.toLowerCase().indexOf('morph') !== -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
findModelByAssoc,
|
|
||||||
isPolymorphic,
|
isPolymorphic,
|
||||||
};
|
};
|
||||||
|
@ -55,6 +55,10 @@ module.exports = {
|
|||||||
entities = await contentManagerService.fetchAll({ model }, ctx.request.query);
|
entities = await contentManagerService.fetchAll({ model }, ctx.request.query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!entities) {
|
||||||
|
return ctx.notFound();
|
||||||
|
}
|
||||||
|
|
||||||
ctx.body = entities;
|
ctx.body = entities;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -94,7 +94,6 @@ module.exports = strapi => {
|
|||||||
|
|
||||||
strapi.app.use(async (ctx, next) => {
|
strapi.app.use(async (ctx, next) => {
|
||||||
await next();
|
await next();
|
||||||
|
|
||||||
// Empty body is considered as `notFound` response.
|
// Empty body is considered as `notFound` response.
|
||||||
if (_.isNil(ctx.body) && _.isNil(ctx.status)) {
|
if (_.isNil(ctx.body) && _.isNil(ctx.status)) {
|
||||||
ctx.notFound();
|
ctx.notFound();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user