2017-12-11 18:23:15 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A set of functions called "actions" for `ContentManager`
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
fetchAll: async (params, query) => {
|
2018-11-14 19:51:05 -06:00
|
|
|
const { limit, skip, sort, query : request, queryAttribute, source, populate = [] } = query;
|
2018-05-16 17:10:43 +02:00
|
|
|
const filters = strapi.utils.models.convertParams(params.model, query);
|
2019-01-24 12:05:33 +01:00
|
|
|
const { where = {} } = !_.isEmpty(request) ? strapi.utils.models.convertParams(params.model, request) : filters;
|
2018-05-23 11:27:39 +02:00
|
|
|
|
2017-12-11 18:23:15 +01:00
|
|
|
// Find entries using `queries` system
|
|
|
|
return await strapi.query(params.model, source).find({
|
2018-05-25 17:03:43 +02:00
|
|
|
limit: limit || filters.limit,
|
|
|
|
skip: skip || filters.start || 0,
|
|
|
|
sort: sort || filters.sort,
|
2019-01-24 12:05:33 +01:00
|
|
|
where,
|
2018-04-03 11:30:39 +02:00
|
|
|
queryAttribute,
|
|
|
|
}, populate);
|
2017-12-11 18:23:15 +01:00
|
|
|
},
|
|
|
|
|
2018-06-13 17:56:41 +02:00
|
|
|
search: async (params, query) => {
|
|
|
|
const { limit, skip, sort, source, _q, populate = [] } = query; // eslint-disable-line no-unused-vars
|
|
|
|
const filters = strapi.utils.models.convertParams(params.model, query);
|
|
|
|
|
|
|
|
// Find entries using `queries` system
|
|
|
|
return await strapi.query(params.model, source).search({
|
|
|
|
limit: limit || filters.limit,
|
|
|
|
skip: skip || filters.start || 0,
|
|
|
|
sort: sort || filters.sort,
|
|
|
|
search: _q
|
|
|
|
}, populate);
|
|
|
|
},
|
|
|
|
|
|
|
|
countSearch: async (params, query) => {
|
|
|
|
const { source, _q } = query;
|
|
|
|
|
|
|
|
return await strapi.query(params.model, source).countSearch({ search: _q });
|
|
|
|
},
|
|
|
|
|
2018-05-22 17:11:35 +02:00
|
|
|
count: async (params, query) => {
|
|
|
|
const { source } = query;
|
|
|
|
const filters = strapi.utils.models.convertParams(params.model, query);
|
2018-05-22 18:23:03 +02:00
|
|
|
|
2018-05-22 17:11:35 +02:00
|
|
|
return await strapi.query(params.model, source).count({ where: filters.where });
|
2017-12-11 18:23:15 +01:00
|
|
|
},
|
|
|
|
|
2018-04-05 15:20:24 +02:00
|
|
|
fetch: async (params, source, populate, raw = true) => {
|
2017-12-11 18:23:15 +01:00
|
|
|
return await strapi.query(params.model, source).findOne({
|
|
|
|
id: params.id
|
2018-04-05 15:20:24 +02:00
|
|
|
}, populate, raw);
|
2017-12-11 18:23:15 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
add: async (params, values, source) => {
|
2018-03-01 17:39:31 +01:00
|
|
|
// Multipart/form-data.
|
|
|
|
if (values.hasOwnProperty('fields') && values.hasOwnProperty('files')) {
|
|
|
|
// Silent recursive parser.
|
|
|
|
const parser = (value) => {
|
|
|
|
try {
|
|
|
|
value = JSON.parse(value);
|
|
|
|
} catch (e) {
|
|
|
|
// Silent.
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.isArray(value) ? value.map(obj => parser(obj)) : value;
|
|
|
|
};
|
|
|
|
|
|
|
|
const files = values.files;
|
|
|
|
|
|
|
|
// Parse stringify JSON data.
|
|
|
|
values = Object.keys(values.fields).reduce((acc, current) => {
|
|
|
|
acc[current] = parser(values.fields[current]);
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
// Update JSON fields.
|
|
|
|
const entry = await strapi.query(params.model, source).create({
|
|
|
|
values
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then, request plugin upload.
|
2018-04-17 17:50:23 +02:00
|
|
|
if (strapi.plugins.upload && Object.keys(files).length > 0) {
|
2018-03-01 17:39:31 +01:00
|
|
|
// Upload new files and attach them to this entity.
|
|
|
|
await strapi.plugins.upload.services.upload.uploadToEntity({
|
|
|
|
id: entry.id || entry._id,
|
|
|
|
model: params.model
|
|
|
|
}, files, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
return strapi.query(params.model, source).findOne({
|
|
|
|
id: entry.id || entry._id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-11 18:23:15 +01:00
|
|
|
// Create an entry using `queries` system
|
|
|
|
return await strapi.query(params.model, source).create({
|
|
|
|
values
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: async (params, values, source) => {
|
2018-02-27 11:52:18 +01:00
|
|
|
// Multipart/form-data.
|
|
|
|
if (values.hasOwnProperty('fields') && values.hasOwnProperty('files')) {
|
2018-02-27 17:38:59 +01:00
|
|
|
// Silent recursive parser.
|
|
|
|
const parser = (value) => {
|
|
|
|
try {
|
|
|
|
value = JSON.parse(value);
|
|
|
|
} catch (e) {
|
|
|
|
// Silent.
|
|
|
|
}
|
|
|
|
|
2018-02-28 12:33:32 +01:00
|
|
|
return _.isArray(value) ? value.map(obj => parser(obj)) : value;
|
2018-02-27 17:38:59 +01:00
|
|
|
};
|
|
|
|
|
2018-02-28 12:33:32 +01:00
|
|
|
const files = values.files;
|
|
|
|
|
2018-09-04 17:57:10 +03:00
|
|
|
// set empty attributes if old values was cleared
|
|
|
|
_.difference(Object.keys(files), Object.keys(values.fields)).forEach(attr => {
|
|
|
|
values.fields[attr] = [];
|
|
|
|
});
|
|
|
|
|
2018-02-27 16:53:06 +01:00
|
|
|
// Parse stringify JSON data.
|
2018-02-28 12:33:32 +01:00
|
|
|
values = Object.keys(values.fields).reduce((acc, current) => {
|
2018-02-27 17:38:59 +01:00
|
|
|
acc[current] = parser(values.fields[current]);
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
2018-02-28 15:49:28 +01:00
|
|
|
// Update JSON fields.
|
|
|
|
await strapi.query(params.model, source).update({
|
|
|
|
id: params.id,
|
|
|
|
values
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then, request plugin upload.
|
2018-02-27 11:52:18 +01:00
|
|
|
if (strapi.plugins.upload) {
|
2018-02-28 15:49:28 +01:00
|
|
|
// Upload new files and attach them to this entity.
|
|
|
|
await strapi.plugins.upload.services.upload.uploadToEntity(params, files, source);
|
2018-02-27 11:52:18 +01:00
|
|
|
}
|
2018-02-28 15:49:28 +01:00
|
|
|
|
|
|
|
return strapi.query(params.model, source).findOne({
|
|
|
|
id: params.id
|
|
|
|
});
|
2018-02-27 11:52:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Raw JSON.
|
2017-12-11 18:23:15 +01:00
|
|
|
return strapi.query(params.model, source).update({
|
|
|
|
id: params.id,
|
|
|
|
values
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
delete: async (params, { source }) => {
|
2018-06-05 18:35:19 +02:00
|
|
|
const query = strapi.query(params.model, source);
|
|
|
|
const primaryKey = query.primaryKey;
|
|
|
|
const response = await query.findOne({
|
2017-12-11 18:23:15 +01:00
|
|
|
id: params.id
|
|
|
|
});
|
|
|
|
|
2018-11-27 14:54:34 +01:00
|
|
|
if (!response) {
|
|
|
|
throw `This resource doesn't exist.`;
|
|
|
|
}
|
|
|
|
|
2018-06-05 18:35:19 +02:00
|
|
|
params[primaryKey] = response[primaryKey];
|
2017-12-11 18:23:15 +01:00
|
|
|
params.values = Object.keys(JSON.parse(JSON.stringify(response))).reduce((acc, current) => {
|
|
|
|
const association = (strapi.models[params.model] || strapi.plugins[source].models[params.model]).associations.filter(x => x.alias === current)[0];
|
|
|
|
|
|
|
|
// Remove relationships.
|
|
|
|
if (association) {
|
|
|
|
acc[current] = _.isArray(response[current]) ? [] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
if (!_.isEmpty(params.values)) {
|
|
|
|
// Run update to remove all relationships.
|
|
|
|
await strapi.query(params.model, source).update(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete an entry using `queries` system
|
|
|
|
return await strapi.query(params.model, source).delete({
|
|
|
|
id: params.id
|
|
|
|
});
|
|
|
|
},
|
2018-06-05 18:35:19 +02:00
|
|
|
|
|
|
|
deleteMany: async (params, query) => {
|
|
|
|
const { source } = query;
|
|
|
|
const { model } = params;
|
2018-06-14 18:27:44 +02:00
|
|
|
|
2018-06-05 18:35:19 +02:00
|
|
|
const primaryKey = strapi.query(model, source).primaryKey;
|
|
|
|
const toRemove = Object.keys(query).reduce((acc, curr) => {
|
|
|
|
if (curr !== 'source') {
|
|
|
|
return acc.concat([query[curr]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const filters = strapi.utils.models.convertParams(model, { [`${primaryKey}_in`]: toRemove });
|
|
|
|
const entries = await strapi.query(model, source).find({ where: filters.where }, null, true);
|
|
|
|
const associations = strapi.query(model, source).associations;
|
2018-06-06 16:20:52 +02:00
|
|
|
|
2018-06-05 18:35:19 +02:00
|
|
|
for (let i = 0; i < entries.length; ++i) {
|
|
|
|
const entry = entries[i];
|
|
|
|
|
|
|
|
associations.forEach(association => {
|
|
|
|
if (entry[association.alias]) {
|
|
|
|
switch (association.nature) {
|
|
|
|
case 'oneWay':
|
|
|
|
case 'oneToOne':
|
|
|
|
case 'manyToOne':
|
2018-06-12 11:26:40 +02:00
|
|
|
case 'oneToManyMorph':
|
2018-06-05 18:35:19 +02:00
|
|
|
entry[association.alias] = null;
|
|
|
|
break;
|
|
|
|
case 'oneToMany':
|
|
|
|
case 'manyToMany':
|
2018-06-12 11:26:40 +02:00
|
|
|
case 'manyToManyMorph':
|
2018-06-05 18:35:19 +02:00
|
|
|
entry[association.alias] = [];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
await strapi.query(model, source).update({
|
|
|
|
[primaryKey]: entry[primaryKey],
|
|
|
|
values: _.pick(entry, associations.map(a => a.alias))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return strapi.query(model, source).deleteMany({
|
|
|
|
[primaryKey]: toRemove,
|
|
|
|
});
|
|
|
|
}
|
2017-12-11 18:23:15 +01:00
|
|
|
};
|