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 = {
|
|
|
|
|
|
|
|
/**
|
2016-12-20 20:41:42 +01:00
|
|
|
* Promise to fetch all <%= humanizeSubIdPluralized || humanizeSubId || humanizeIdPluralized %>.
|
2016-08-09 11:47:48 +02:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-01-11 18:24:26 +01:00
|
|
|
fetchAll: (params) => {
|
|
|
|
return <%= globalID %>.find(params).populate(_.keys(_.pickBy(strapi.models.<%= humanizeSubId || humanizeId %>.attributes, { autoPopulate: true })).join(' '));
|
2016-08-09 11:47:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-12-20 20:41:42 +01:00
|
|
|
* Promise to fetch a/an <%= subId || id %>.
|
2016-08-09 11:47:48 +02:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-01-11 18:24:26 +01:00
|
|
|
fetch: (params) => {
|
|
|
|
return <%= globalID %>.findOne(params).populate(_.keys(_.pickBy(strapi.models.<%= humanizeSubId || humanizeId %>.attributes, { autoPopulate: true })).join(' '));
|
2016-08-09 11:47:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-12-20 20:41:42 +01:00
|
|
|
* Promise to add a/an <%= subId || id %>.
|
2016-08-09 11:47:48 +02:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-01-11 18:24:26 +01:00
|
|
|
add: (values) => {
|
|
|
|
return <%= humanizeSubId || humanizeId %>.create(values);
|
2016-08-09 11:47:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-12-20 20:41:42 +01:00
|
|
|
* Promise to edit a/an <%= subId || 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
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-12-20 20:41:42 +01:00
|
|
|
* Promise to remove a/an <%= subId || 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
|
|
|
}
|
|
|
|
};
|