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-09-13 10:30:37 +02:00
|
|
|
params = 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()
|
|
|
|
.where(params.where)
|
|
|
|
.sort(params.sort)
|
|
|
|
.skip(params.start)
|
|
|
|
.limit(params.limit)
|
|
|
|
.populate(_.keys(_.pickBy(strapi.models.<%= humanizeId %>.attributes, { autoPopulate: true })).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 %>
|
|
|
|
.findOne(params)
|
|
|
|
.populate(_.keys(_.pickBy(strapi.models.<%= humanizeId %>.attributes, { autoPopulate: true })).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-01-11 18:24:26 +01:00
|
|
|
add: (values) => {
|
2017-02-03 16:36:59 +00:00
|
|
|
return <%= globalID %>.create(values);
|
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}
|
|
|
|
*/
|
|
|
|
|
|
|
|
edit: (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.
|
|
|
|
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}
|
|
|
|
*/
|
|
|
|
|
|
|
|
remove: 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.
|
|
|
|
return <%= globalID %>.findOneAndRemove(params, {});
|
2016-08-09 11:47:48 +02:00
|
|
|
}
|
|
|
|
};
|