mirror of
https://github.com/strapi/strapi.git
synced 2025-10-17 02:53:22 +00:00
Merge pull request #1307 from okaris/count-route-controller-service-bookshelf
Added count to api template for bookshelf
This commit is contained in:
commit
d55749cde2
@ -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.
|
||||
*
|
||||
|
@ -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 %>.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user