2016-08-09 11:47:48 +02:00
|
|
|
'use strict';
|
|
|
|
|
2019-03-20 00:02:05 +01:00
|
|
|
/* global <%= globalID %> */
|
|
|
|
|
2016-08-09 11:47:48 +02:00
|
|
|
/**
|
2017-01-11 18:24:26 +01:00
|
|
|
* <%= filename %> service
|
|
|
|
*
|
|
|
|
* @description: A set of functions similar to controller's actions to avoid code duplication.
|
2016-08-09 11:47:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Public dependencies.
|
|
|
|
const _ = require('lodash');
|
2019-03-13 19:27:18 +01:00
|
|
|
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
|
2016-08-09 11:47:48 +02:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to fetch all <%= humanizeIdPluralized %>.
|
2016-08-09 11:47:48 +02:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2019-02-02 13:19:05 +01:00
|
|
|
fetchAll: (params, populate) => {
|
2019-03-13 19:27:18 +01:00
|
|
|
const filters = convertRestQueryParams(params);
|
|
|
|
const populateOpt = populate || <%= globalID %>.associations
|
|
|
|
.filter(ast => ast.autoPopulate !== false)
|
|
|
|
.map(ast => ast.alias)
|
2017-09-12 17:58:31 +02:00
|
|
|
|
2019-03-13 19:27:18 +01:00
|
|
|
return buildQuery({
|
|
|
|
model: <%= globalID %>,
|
|
|
|
filters,
|
|
|
|
populate: populateOpt,
|
|
|
|
});
|
2016-08-09 11:47:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to fetch a/an <%= id %>.
|
2016-08-09 11:47:48 +02:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-01-11 18:24:26 +01:00
|
|
|
fetch: (params) => {
|
2018-05-09 12:52:32 +02:00
|
|
|
// Select field to populate.
|
|
|
|
const populate = <%= globalID %>.associations
|
|
|
|
.filter(ast => ast.autoPopulate !== false)
|
|
|
|
.map(ast => ast.alias)
|
|
|
|
.join(' ');
|
|
|
|
|
2017-10-10 15:14:20 +02:00
|
|
|
return <%= globalID %>
|
2017-12-20 13:45:53 +01:00
|
|
|
.findOne(_.pick(params, _.keys(<%= globalID %>.schema.paths)))
|
2018-05-09 12:52:32 +02:00
|
|
|
.populate(populate);
|
2016-08-09 11:47:48 +02:00
|
|
|
},
|
|
|
|
|
2018-05-29 15:48:07 +03:00
|
|
|
/**
|
|
|
|
* Promise to count <%= humanizeIdPluralized %>.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
|
|
|
count: (params) => {
|
2019-03-13 19:27:18 +01:00
|
|
|
const filters = convertRestQueryParams(params);
|
|
|
|
|
|
|
|
return buildQuery({
|
2019-03-29 16:53:25 +01:00
|
|
|
model: <%= globalID %>,
|
2019-03-13 19:27:18 +01:00
|
|
|
filters: { where: filters.where },
|
|
|
|
})
|
2019-03-26 14:57:02 +01:00
|
|
|
.count()
|
2018-05-29 15:48:07 +03:00
|
|
|
},
|
|
|
|
|
2016-08-09 11:47:48 +02:00
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to add a/an <%= id %>.
|
2016-08-09 11:47:48 +02:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-10-30 17:59:53 +01:00
|
|
|
add: async (values) => {
|
2018-05-09 12:52:32 +02:00
|
|
|
// Extract values related to relational data.
|
|
|
|
const relations = _.pick(values, <%= globalID %>.associations.map(ast => ast.alias));
|
|
|
|
const data = _.omit(values, <%= globalID %>.associations.map(ast => ast.alias));
|
2018-04-23 16:57:59 +02:00
|
|
|
|
2018-05-09 12:52:32 +02:00
|
|
|
// Create entry with no-relational data.
|
|
|
|
const entry = await <%= globalID %>.create(data);
|
2018-04-23 16:57:59 +02:00
|
|
|
|
2018-05-09 12:52:32 +02:00
|
|
|
// Create relational data and return the entry.
|
2018-07-19 14:46:03 +02:00
|
|
|
return <%= globalID %>.updateRelations({ _id: entry.id, values: relations });
|
2016-08-09 11:47:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to edit a/an <%= id %>.
|
2016-08-09 11:47:48 +02:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-10-25 16:24:35 +02:00
|
|
|
edit: async (params, values) => {
|
2018-05-09 12:52:32 +02:00
|
|
|
// Extract values related to relational data.
|
|
|
|
const relations = _.pick(values, <%= globalID %>.associations.map(a => a.alias));
|
|
|
|
const data = _.omit(values, <%= globalID %>.associations.map(a => a.alias));
|
|
|
|
|
|
|
|
// Update entry with no-relational data.
|
2019-01-23 16:56:04 +01:00
|
|
|
const entry = await <%= globalID %>.updateOne(params, data, { multi: true });
|
2018-05-09 12:52:32 +02:00
|
|
|
|
|
|
|
// Update relational data and return the entry.
|
|
|
|
return <%= globalID %>.updateRelations(Object.assign(params, { values: relations }));
|
2016-08-09 11:47:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to remove a/an <%= id %>.
|
2016-08-09 11:47:48 +02:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-10-31 11:25:13 +01:00
|
|
|
remove: async params => {
|
2018-05-09 12:52:32 +02:00
|
|
|
// Select field to populate.
|
|
|
|
const populate = <%= globalID %>.associations
|
|
|
|
.filter(ast => ast.autoPopulate !== false)
|
|
|
|
.map(ast => ast.alias)
|
|
|
|
.join(' ');
|
|
|
|
|
2017-01-11 18:24:26 +01:00
|
|
|
// Note: To get the full response of Mongo, use the `remove()` method
|
|
|
|
// or add spent the parameter `{ passRawResult: true }` as second argument.
|
2018-05-09 12:52:32 +02:00
|
|
|
const data = await <%= globalID %>
|
|
|
|
.findOneAndRemove(params, {})
|
|
|
|
.populate(populate);
|
|
|
|
|
2018-05-09 17:41:19 +02:00
|
|
|
if (!data) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-05-09 12:52:32 +02:00
|
|
|
await Promise.all(
|
|
|
|
<%= globalID %>.associations.map(async association => {
|
2019-02-12 09:00:39 +01:00
|
|
|
if (!association.via || !data._id || association.dominant) {
|
2018-10-08 12:25:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-09 12:52:32 +02:00
|
|
|
const search = _.endsWith(association.nature, 'One') || association.nature === 'oneToMany' ? { [association.via]: data._id } : { [association.via]: { $in: [data._id] } };
|
|
|
|
const update = _.endsWith(association.nature, 'One') || association.nature === 'oneToMany' ? { [association.via]: null } : { $pull: { [association.via]: data._id } };
|
|
|
|
|
|
|
|
// Retrieve model.
|
|
|
|
const model = association.plugin ?
|
|
|
|
strapi.plugins[association.plugin].models[association.model || association.collection] :
|
|
|
|
strapi.models[association.model || association.collection];
|
|
|
|
|
|
|
|
return model.update(search, update, { multi: true });
|
|
|
|
})
|
|
|
|
);
|
2017-10-31 11:25:13 +01:00
|
|
|
|
|
|
|
return data;
|
2018-07-05 10:22:43 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Promise to search a/an <%= id %>.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2018-07-05 11:28:55 +02:00
|
|
|
search: async (params) => {
|
2018-07-05 10:22:43 +02:00
|
|
|
// Convert `params` object to filters compatible with Mongo.
|
|
|
|
const filters = strapi.utils.models.convertParams('<%= globalID.toLowerCase() %>', params);
|
|
|
|
// Select field to populate.
|
|
|
|
const populate = <%= globalID %>.associations
|
|
|
|
.filter(ast => ast.autoPopulate !== false)
|
|
|
|
.map(ast => ast.alias)
|
|
|
|
.join(' ');
|
|
|
|
|
|
|
|
const $or = Object.keys(<%= globalID %>.attributes).reduce((acc, curr) => {
|
|
|
|
switch (<%= globalID %>.attributes[curr].type) {
|
|
|
|
case 'integer':
|
|
|
|
case 'float':
|
|
|
|
case 'decimal':
|
|
|
|
if (!_.isNaN(_.toNumber(params._q))) {
|
|
|
|
return acc.concat({ [curr]: params._q });
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
case 'string':
|
|
|
|
case 'text':
|
|
|
|
case 'password':
|
|
|
|
return acc.concat({ [curr]: { $regex: params._q, $options: 'i' } });
|
|
|
|
case 'boolean':
|
|
|
|
if (params._q === 'true' || params._q === 'false') {
|
|
|
|
return acc.concat({ [curr]: params._q === 'true' });
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
default:
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return <%= globalID %>
|
|
|
|
.find({ $or })
|
|
|
|
.sort(filters.sort)
|
|
|
|
.skip(filters.start)
|
|
|
|
.limit(filters.limit)
|
|
|
|
.populate(populate);
|
2016-08-09 11:47:48 +02:00
|
|
|
}
|
|
|
|
};
|