From 20237b4a280fd1a4edb1e011350063f8503c9489 Mon Sep 17 00:00:00 2001 From: rehabas Date: Fri, 3 Apr 2020 22:41:27 +0300 Subject: [PATCH] get stats route relates #28 --- server/controllers/routes/admin/index.js | 2 ++ server/controllers/routes/admin/stats.js | 22 ++++++++++++++++++++++ test/index.test.js | 15 +++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 server/controllers/routes/admin/stats.js diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index 857fc784..883f4332 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -8,6 +8,7 @@ const { deleteProjectData, getCohortProjects, } = require('./project'); +const getStats = require('./stats'); router.post('/cohorts', addCohort); @@ -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; diff --git a/server/controllers/routes/admin/stats.js b/server/controllers/routes/admin/stats.js new file mode 100644 index 00000000..408a99e0 --- /dev/null +++ b/server/controllers/routes/admin/stats.js @@ -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; diff --git a/test/index.test.js b/test/index.test.js index 4c49f86c..7ed40422 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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(); + }); + }); +});