Merge pull request #1307 from okaris/count-route-controller-service-bookshelf

Added count to api template for bookshelf
This commit is contained in:
Jim LAURIE 2018-06-01 10:58:08 +02:00 committed by GitHub
commit d55749cde2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -28,6 +28,16 @@ module.exports = {
return strapi.services.<%= id %>.fetch(ctx.params);
},
/**
* Count <%= id %> records.
*
* @return {Number}
*/
count: async (ctx) => {
return strapi.services.<%= id %>.count(ctx.query);
},
/**
* Create a/an <%= id %> record.
*

View File

@ -67,6 +67,29 @@ module.exports = {
});
},
/**
* Promise to count a/an <%= id %>.
*
* @return {Promise}
*/
count: (params) => {
// Convert `params` object to filters compatible with Bookshelf.
const filters = strapi.utils.models.convertParams('<%= globalID.toLowerCase() %>', params);
return <%= globalID %>.query(function(qb) {
_.forEach(filters.where, (where, key) => {
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);
}
});
}).count();
},
/**
* Promise to add a/an <%= id %>.
*