Skip to content

Commit

Permalink
create query for get number of cohorts, students and projects
Browse files Browse the repository at this point in the history
relates #28
  • Loading branch information
rehabas committed Apr 4, 2020
1 parent 10d270e commit dc46d97
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
35 changes: 17 additions & 18 deletions server/controllers/routes/admin/stats.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
const { getCohorts } = require('../../../database/queries');
const { getAlumniQuery } = require('../../../database/queries');
const { getAllProjects } = require('../../../database/queries');
const { getStatsQuery } = require('../../../database/queries');

const getStats = async (req, res, next) => {
try {
const cohorts = await getCohorts();
const cohortsCount = cohorts.rowCount;
const projects = await getAllProjects();
const projectsCount = projects.rowCount;
const students = await getAlumniQuery();
const studentsCount = students.rowCount;

res.json({
StatusCode: 200,
data: [
{ numOfCohorts: cohortsCount },
{ numOfProjects: projectsCount },
{ numOfStudents: studentsCount },
],
});
const { rows } = await getStatsQuery();
if (rows.length > 0) {
res.json({
StatusCode: 200,
data: {
cohortsCount: rows[0].count,
projectsCount: rows[1].count,
studentsCount: rows[2].count,
},
});
} else {
res.json({
StatusCode: 200,
message: 'No data',
});
}
} catch (err) {
next(err);
}
Expand Down
8 changes: 8 additions & 0 deletions server/database/queries/getStatsQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const connection = require('../config/connection');

const getStatsQuery = () =>
connection.query(
'SELECT COUNT(id) FROM cohort UNION ALL SELECT COUNT(id) FROM project UNION ALL SELECT COUNT(id) FROM student',
);

module.exports = getStatsQuery;
5 changes: 2 additions & 3 deletions server/database/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { getAlumniQuery, deleteStudentQuery, putStudent } = require('./student');

const {
putSpecificCohort,
getCohorts,
Expand All @@ -14,8 +13,8 @@ const {
deleteProject,
getCohortProjectsQuery,
getProjects,
getAllProjects,
} = require('./project');
const getStatsQuery = require('./getStatsQuery');

module.exports = {
postCohort,
Expand All @@ -32,5 +31,5 @@ module.exports = {
getProjectById,
deleteProject,
getProjects,
getAllProjects,
getStatsQuery,
};
5 changes: 0 additions & 5 deletions server/database/queries/project/getAllProjects.js

This file was deleted.

2 changes: 0 additions & 2 deletions server/database/queries/project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const getCohortProjectsQuery = require('./getCohortProjects');
const editProjectQuery = require('./editProject');
const getProjectById = require('./getProjectById');
const deleteProject = require('./deleteProject');
const getAllProjects = require('./getAllProjects');

module.exports = {
getProjects,
Expand All @@ -13,5 +12,4 @@ module.exports = {
editProjectQuery,
getProjectById,
deleteProject,
getAllProjects,
};

0 comments on commit dc46d97

Please sign in to comment.