2016-03-18 11:12:50 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-25 22:22:34 +01:00
|
|
|
/**
|
2017-01-11 17:53:40 +01:00
|
|
|
* <%= filename %> service
|
|
|
|
*
|
|
|
|
* @description: A set of functions similar to controller's actions to avoid code duplication.
|
2016-03-25 22:22:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Public dependencies.
|
2016-03-18 11:12:50 +01:00
|
|
|
const _ = require('lodash');
|
2016-03-25 22:22:34 +01:00
|
|
|
|
|
|
|
// Strapi utilities.
|
2016-03-22 16:12:18 +01:00
|
|
|
const utils = require('strapi-bookshelf/lib/utils/');
|
2016-03-18 11:12:50 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to fetch all <%= idPluralized %>.
|
2016-03-18 11:12:50 +01:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-01-11 17:53:40 +01:00
|
|
|
fetchAll: (params) => {
|
2018-05-09 16:08:58 +02:00
|
|
|
// Convert `params` object to filters compatible with Bookshelf.
|
|
|
|
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);
|
2017-09-13 12:18:54 +02:00
|
|
|
|
|
|
|
return <%= globalID %>.query(function(qb) {
|
2018-05-09 16:08:58 +02:00
|
|
|
_.forEach(filters.where, (where, key) => {
|
2018-04-03 12:38:33 +02:00
|
|
|
if (_.isArray(where.value)) {
|
|
|
|
for (const value in where.value) {
|
|
|
|
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qb.where(key, where.symbol, where.value);
|
|
|
|
}
|
2017-09-13 12:18:54 +02:00
|
|
|
});
|
|
|
|
|
2018-05-09 16:08:58 +02:00
|
|
|
if (filters.sort) {
|
|
|
|
qb.orderBy(filters.sort.key, filters.sort.order);
|
2017-10-26 17:21:23 +02:00
|
|
|
}
|
2017-09-13 12:18:54 +02:00
|
|
|
|
2018-05-09 16:08:58 +02:00
|
|
|
qb.offset(filters.start);
|
|
|
|
qb.limit(filters.limit);
|
2017-09-13 12:18:54 +02:00
|
|
|
}).fetchAll({
|
2018-05-09 16:08:58 +02:00
|
|
|
withRelated: populate
|
2016-03-18 11:12:50 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to fetch a/an <%= id %>.
|
2016-03-18 11:12:50 +01:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-01-11 17:53:40 +01:00
|
|
|
fetch: (params) => {
|
2018-05-09 16:08:58 +02:00
|
|
|
// Select field to populate.
|
|
|
|
const populate = <%= globalID %>.associations
|
|
|
|
.filter(ast => ast.autoPopulate !== false)
|
|
|
|
.map(ast => ast.alias);
|
|
|
|
|
2017-01-11 17:53:40 +01:00
|
|
|
return <%= globalID %>.forge(_.pick(params, 'id')).fetch({
|
2018-05-09 16:08:58 +02:00
|
|
|
withRelated: populate
|
2016-03-18 11:12:50 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to add a/an <%= id %>.
|
2016-03-18 11:12:50 +01:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-10-30 14:02:54 +01:00
|
|
|
add: async (values) => {
|
2018-05-09 16:08:58 +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));
|
|
|
|
|
|
|
|
// Create entry with no-relational data.
|
|
|
|
const entry = await <%= globalID %>.forge(data).save();
|
|
|
|
|
|
|
|
// Create relational data and return the entry.
|
|
|
|
return <%= globalID %>.updateRelations({ id: entry.id , values: relations });
|
2016-03-18 11:12:50 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to edit a/an <%= id %>.
|
2016-03-18 11:12:50 +01:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2017-10-26 17:21:23 +02:00
|
|
|
edit: async (params, values) => {
|
2018-05-09 16:08:58 +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));
|
|
|
|
|
|
|
|
// Create entry with no-relational data.
|
|
|
|
const entry = <%= globalID %>.forge(params).save(data, { path: true });
|
|
|
|
|
|
|
|
// Create relational data and return the entry.
|
|
|
|
return <%= globalID %>.updateRelations(Object.assign(params, { values: relations }));
|
2016-03-18 11:12:50 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-02-14 01:10:37 +01:00
|
|
|
* Promise to remove a/an <%= id %>.
|
2016-03-18 11:12:50 +01:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
|
2018-05-09 16:08:58 +02:00
|
|
|
remove: async (params) => {
|
|
|
|
await Promise.all(
|
|
|
|
<%= globalID %>.associations.map(association =>
|
|
|
|
<%= globalID %>.forge(params)[association.alias]().detach()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2017-01-11 17:53:40 +01:00
|
|
|
return <%= globalID %>.forge(params).destroy();
|
2016-03-18 11:12:50 +01:00
|
|
|
}
|
|
|
|
};
|