Skip to content

Commit

Permalink
get stats route
Browse files Browse the repository at this point in the history
relates #28
  • Loading branch information
rehabas committed Apr 3, 2020
1 parent ef66b41 commit 20237b4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/controllers/routes/admin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
deleteProjectData,
getCohortProjects,
} = require('./project');
const getStats = require('./stats');

router.post('/cohorts', addCohort);

Expand All @@ -32,5 +33,6 @@ router.get('/cohorts/:cohortId/projects', getCohortProjects);
router.delete('/alumni/:studentId', deleteStudent);
router.put('/alumni/:studentId', putStudentData);
router.delete('/projects/:projectId', deleteProjectData);
router.get('/stats', getStats);

module.exports = router;
22 changes: 22 additions & 0 deletions server/controllers/routes/admin/stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { getCohorts } = require('../../../database/queries');

const getStats = async (req, res, next) => {
try {
const cohorts = await getCohorts();
const cohortsCount = cohorts.rowCount;
const projectsCount = 16;
const studentsCount = 45;
res.json({
StatusCode: 200,
data: [
{ numOfCohorts: cohortsCount },
{ numOfProjects: projectsCount },
{ numOfStudents: studentsCount },
],
});
} catch (err) {
next(err);
}
};

module.exports = getStats;
15 changes: 15 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,18 @@ describe('Admin, Put project', () => {
});
});
});

describe('Get stats', () => {
test('Route /stats status 200, json header ', (done) => {
return request(app)
.get('/api/v1/stats')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { data } = res.body;
expect(data[0].numOfCohorts).toBe(2);
done();
});
});
});

0 comments on commit 20237b4

Please sign in to comment.