2016-08-09 11:47:48 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
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');
|
|
|
|
|
|
|
|
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}
|
|
|
|
*/
|
|
|
|
|
2017-01-11 18:24:26 +01:00
|
|
|
fetchAll: (params) => {
|
2017-10-11 17:30:08 +02:00
|
|
|
const convertedParams = strapi.utils.models.convertParams('<%= globalID.toLowerCase() %>', params);
|
2017-09-12 17:58:31 +02:00
|
|
|
|
2017-10-10 15:14:20 +02:00
|
|
|
return <%= globalID %>
|
|
|
|
.find()
|
2017-10-11 17:30:08 +02:00
|
|
|
.where(convertedParams.where)
|
|
|
|
.sort(convertedParams.sort)
|
|
|
|
.skip(convertedParams.start)
|
|
|
|
.limit(convertedParams.limit)
|
2017-10-25 16:24:35 +02:00
|
|
|
.populate(_.keys(_.groupBy(_.reject(strapi.models.<%= id %>.associations, {autoPopulate: false}), 'alias')).join(' '));
|
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) => {
|
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)))
|
2017-10-25 16:24:35 +02:00
|
|
|
.populate(_.keys(_.groupBy(_.reject(strapi.models.<%= id %>.associations, {autoPopulate: false}), 'alias')).join(' '));
|
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) => {
|
|
|
|
const data = await <%= globalID %>.create(_.omit(values, _.keys(_.groupBy(strapi.models.<%= id %>.associations, 'alias'))));
|
2017-11-01 15:16:28 +01:00
|
|
|
await strapi.hook.mongoose.manageRelations('<%= id %>', _.merge(_.clone(data), { values }));
|
2017-10-30 17:59:53 +01:00
|
|
|
return data;
|
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) => {
|
2017-01-11 18:24:26 +01:00
|
|
|
// Note: The current method will return the full response of Mongo.
|
|
|
|
// To get the updated object, you have to execute the `findOne()` method
|
|
|
|
// or use the `findOneOrUpdate()` method with `{ new:true }` option.
|
2017-11-01 15:16:28 +01:00
|
|
|
await strapi.hook.mongoose.manageRelations('<%= id %>', _.merge(_.clone(params), { values }));
|
2017-01-11 18:24:26 +01:00
|
|
|
return <%= globalID %>.update(params, values, { multi: true });
|
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 => {
|
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.
|
2017-10-31 11:25:13 +01:00
|
|
|
const data = await <%= globalID %>.findOneAndRemove(params, {})
|
|
|
|
.populate(_.keys(_.groupBy(_.reject(strapi.models.<%= id %>.associations, {autoPopulate: false}), 'alias')).join(' '));
|
|
|
|
|
|
|
|
_.forEach(<%= globalID %>.associations, async association => {
|
|
|
|
const search = (_.endsWith(association.nature, 'One')) ? { [association.via]: data._id } : { [association.via]: { $in: [data._id] } };
|
|
|
|
const update = (_.endsWith(association.nature, 'One')) ? { [association.via]: null } : { $pull: { [association.via]: data._id } };
|
|
|
|
|
|
|
|
await strapi.models[association.model || association.collection].update(
|
|
|
|
search,
|
|
|
|
update,
|
|
|
|
{ multi: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
2016-08-09 11:47:48 +02:00
|
|
|
}
|
|
|
|
};
|