Skip to content

Commit

Permalink
feat(tasks): init stats example ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Jul 16, 2020
1 parent f91dddf commit e809d9a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions modules/tasks/controllers/tasks.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ exports.delete = async (req, res) => {
}
};

/**
* @desc Endpoint to get stats of tasks and return data
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.stats = async (req, res) => {
const data = await TasksService.stats();
if (!data.err) {
responses.success(res, 'tasks stats')(data);
} else {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(data.err))(data.err);
}
};

/**
* @desc MiddleWare to ask the service the task for this id
* @param {Object} req - Express request object
Expand Down
3 changes: 3 additions & 0 deletions modules/tasks/policies/tasks.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ exports.invokeRolesPolicies = () => {
}, {
roles: ['guest'],
allows: [{
resources: '/api/tasks/stats',
permissions: ['get'],
}, {
resources: '/api/tasks',
permissions: ['get'],
}, {
Expand Down
6 changes: 6 additions & 0 deletions modules/tasks/repositories/tasks.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ exports.deleteMany = (filter) => {
if (filter) return Task.deleteMany(filter).exec();
};

/**
* @desc Function to get collection stats
* @return {Object} scrap
*/
exports.stats = () => Task.countDocuments();

/**
* @desc Function to import list of tasks in db
* @param {[Object]} tasks
Expand Down
4 changes: 4 additions & 0 deletions modules/tasks/routes/tasks.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const tasksSchema = require('../models/tasks.schema');
* Routes
*/
module.exports = (app) => {
// stats
app.route('/api/tasks/stats').all(policy.isAllowed)
.get(tasks.stats);

// list & post
app.route('/api/tasks')
.get(tasks.list) // list
Expand Down
9 changes: 9 additions & 0 deletions modules/tasks/services/tasks.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ exports.delete = async (task) => {
const result = await TasksRepository.delete(task);
return Promise.resolve(result);
};

/**
* @desc Function to get all stats of db
* @return {Promise} All stats
*/
exports.stats = async () => {
const result = await TasksRepository.stats();
return Promise.resolve(result);
};

0 comments on commit e809d9a

Please sign in to comment.