mirror of
https://github.com/strapi/strapi.git
synced 2025-09-02 13:23:12 +00:00
Handle errors and complete update model
This commit is contained in:
parent
41ec60b084
commit
32ca703098
@ -118,6 +118,9 @@ module.exports = (scope, cb) => {
|
||||
// Set collectionName
|
||||
scope.collectionName = _.get(scope.args, 'collectionName', undefined);
|
||||
|
||||
// Set description
|
||||
scope.description = _.get(scope.args, 'description', undefined);
|
||||
|
||||
// Get default connection
|
||||
try {
|
||||
scope.connection = _.get(scope.args, 'connection') || JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'database.json'))).defaultConnection || '';
|
||||
|
@ -1,6 +1,7 @@
|
||||
{
|
||||
"connection": "<%= connection %>",
|
||||
"collectionName": "<%= idPluralized %>",
|
||||
"description": "<%= description %>",
|
||||
"collectionName": "<%= collectionName || idPluralized %>",
|
||||
"options": {
|
||||
"increments": true,
|
||||
"timestamps": true,
|
||||
|
@ -1,5 +1,6 @@
|
||||
{
|
||||
"connection": "<%= connection %>",
|
||||
"description": "<%= description %>",
|
||||
"collectionName": "<%= collectionName || idPluralized %>",
|
||||
"attributes": {
|
||||
<%= attributes %>
|
||||
|
@ -205,6 +205,8 @@ module.exports = function (strapi) {
|
||||
_.forEach(definition.attributes, (details, name) => {
|
||||
const verbose = _.get(utilsModels.getNature(details, name), 'verbose') || '';
|
||||
|
||||
console.log("coucou", details, name);
|
||||
|
||||
// Build associations key
|
||||
if (!_.isEmpty(verbose)) {
|
||||
utilsModels.defineAssociations(globalName, definition, details, name);
|
||||
|
@ -25,14 +25,22 @@ module.exports = {
|
||||
},
|
||||
|
||||
createModel: async ctx => {
|
||||
const { name, connection, collectionName, attributes = [] } = JSON.parse(ctx.request.body);
|
||||
const { name, description, connection, collectionName, attributes = [] } = JSON.parse(ctx.request.body);
|
||||
|
||||
if (!name) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.name.missing' }] }]);
|
||||
if (!_.includes(Service.getConnections(), connection)) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.connection.unknow' }] }]);
|
||||
if (strapi.models[name]) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.exist' }] }]);
|
||||
if (!_.isNaN(parseFloat(name[0]))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.name' }] }]);
|
||||
|
||||
const [formatedAttributes, attributesErrors] = Service.formatAttributes(attributes);
|
||||
|
||||
if (!_.isEmpty(attributesErrors)) {
|
||||
return ctx.badRequest(null, [{ messages: attributesErrors }]);
|
||||
}
|
||||
|
||||
strapi.reload.isWatching = false;
|
||||
|
||||
await Service.generateAPI(name, connection, collectionName, attributes);
|
||||
await Service.generateAPI(name, description, connection, collectionName, []);
|
||||
|
||||
const [modelFilePath, modelFilePathErrors] = Service.getModelPath(name);
|
||||
|
||||
@ -43,7 +51,7 @@ module.exports = {
|
||||
try {
|
||||
const modelJSON = require(modelFilePath);
|
||||
|
||||
modelJSON.attributes = Service.formatAttributes(attributes);
|
||||
modelJSON.attributes = formatedAttributes;
|
||||
|
||||
const clearRelationsErrors = Service.clearRelations(name);
|
||||
|
||||
@ -73,9 +81,18 @@ module.exports = {
|
||||
|
||||
updateModel: async ctx => {
|
||||
const { model } = ctx.params;
|
||||
const { name, attributes = [] } = JSON.parse(ctx.request.body);
|
||||
const { name, description, connection, collectionName, attributes = [] } = JSON.parse(ctx.request.body);
|
||||
|
||||
if (!_.get(strapi.models, model)) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.unknow' }] }]);
|
||||
if (!name) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.name.missing' }] }]);
|
||||
if (!_.includes(Service.getConnections(), connection)) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.connection.unknow' }] }]);
|
||||
if (!strapi.models[name]) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.unknow' }] }]);
|
||||
if (!_.isNaN(parseFloat(name[0]))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.name' }] }]);
|
||||
|
||||
const [formatedAttributes, attributesErrors] = Service.formatAttributes(attributes);
|
||||
|
||||
if (!_.isEmpty(attributesErrors)) {
|
||||
return ctx.badRequest(null, [{ messages: attributesErrors }]);
|
||||
}
|
||||
|
||||
const [modelFilePath, modelFilePathErrors] = Service.getModelPath(model);
|
||||
|
||||
@ -86,7 +103,10 @@ module.exports = {
|
||||
try {
|
||||
const modelJSON = require(modelFilePath);
|
||||
|
||||
modelJSON.attributes = Service.formatAttributes(attributes);
|
||||
modelJSON.attributes = formatedAttributes;
|
||||
modelJSON.description = description;
|
||||
modelJSON.description = connection;
|
||||
modelJSON.collectionName = collectionName;
|
||||
|
||||
strapi.reload.isWatching = false;
|
||||
|
||||
@ -112,7 +132,6 @@ module.exports = {
|
||||
return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.write' }] }]);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.read' }] }]);
|
||||
}
|
||||
},
|
||||
|
@ -50,16 +50,10 @@ module.exports = {
|
||||
},
|
||||
|
||||
getConnections: () => {
|
||||
let connections = [];
|
||||
|
||||
_.forEach(_.keys(strapi.config.environments), env => {
|
||||
connections = _.assign(connections, _.keys(strapi.config.environments[env].database.connections));
|
||||
});
|
||||
|
||||
return _.compact(connections);
|
||||
return _.keys(strapi.config.currentEnvironment.database.connections);
|
||||
},
|
||||
|
||||
generateAPI: (name, connection, collectionName, attributes) => {
|
||||
generateAPI: (name, description, connection, collectionName, attributes) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const scope = {
|
||||
generatorType: 'api',
|
||||
@ -67,6 +61,7 @@ module.exports = {
|
||||
rootPath: strapi.config.appPath,
|
||||
args: {
|
||||
api: name,
|
||||
description,
|
||||
attributes,
|
||||
connection,
|
||||
collectionName: !_.isEmpty(collectionName) ? collectionName : undefined
|
||||
@ -124,13 +119,14 @@ module.exports = {
|
||||
},
|
||||
|
||||
formatAttributes: attributes => {
|
||||
const errors = [];
|
||||
const attrs = {};
|
||||
|
||||
_.forEach(attributes, attribute => {
|
||||
if (_.has(attribute, 'params.type')) {
|
||||
attrs[attribute.name] = _.get(attribute, 'params');
|
||||
attrs[attribute.name] = attribute.params;
|
||||
} else if (_.has(attribute, 'params.target')) {
|
||||
const relation = _.get(attribute, 'params');
|
||||
const relation = attribute.params;
|
||||
const attr = {
|
||||
required: relation.required,
|
||||
columnName: relation.columnName,
|
||||
@ -143,8 +139,8 @@ module.exports = {
|
||||
case 'manyToOne':
|
||||
attr.model = relation.target;
|
||||
break;
|
||||
case 'oneToMany':
|
||||
case 'manyToMany':
|
||||
case 'oneToMany':
|
||||
attr.collection = relation.target;
|
||||
break;
|
||||
default:
|
||||
@ -152,9 +148,18 @@ module.exports = {
|
||||
|
||||
attrs[attribute.name] = attr;
|
||||
}
|
||||
|
||||
if (!_.isNaN(parseFloat(attribute.name[0])) || !_.isNaN(parseFloat(_.get(attribute, 'params.key'), NaN))) {
|
||||
errors.push({
|
||||
id: 'request.error.attribute.values',
|
||||
params: {
|
||||
attribute
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return attrs;
|
||||
return [attrs, errors];
|
||||
},
|
||||
|
||||
clearRelations: model => {
|
||||
|
@ -65,4 +65,4 @@
|
||||
"npm": ">= 3.0.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
|
@ -167,7 +167,7 @@ module.exports = {
|
||||
nature: 'oneToOne',
|
||||
verbose: 'hasOne'
|
||||
};
|
||||
} else if (types.current === 'model' && types.other === 'collection') {
|
||||
} else if ((types.current === 'model' || types.current === 'modelD') && types.other === 'collection') {
|
||||
return {
|
||||
nature: 'oneToMany',
|
||||
verbose: 'belongsTo'
|
||||
|
Loading…
x
Reference in New Issue
Block a user