Skip to content

Commit

Permalink
Merge branch 'master' into 28-get-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
rehabas committed Apr 3, 2020
2 parents 20237b4 + bd3020f commit f1d5083
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dev": "NODE_ENV=development nodemon server/index.js",
"project-setup": "npm install && cd client && npm install",
"heroku-postbuild": "cd client && npm install && npm run build",
"test": "NODE_ENV=test jest --runInBand --coverage",
"test": "NODE_ENV=test jest --no-cache --runInBand --coverage",
"kill": "fuser -k 5000/tcp",
"lint": "eslint server/",
"lint-fix": "eslint server/ --fix",
Expand Down
8 changes: 5 additions & 3 deletions server/controllers/routes/user/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const router = require('express').Router();

const { getAlumni } = require('./student');
const { getProjectData, getProjectsData } = require('./project');
const { getCohortsData, getSpecificCohort } = require('./cohort');
const { getProjectData } = require('./project');

router.get('/cohorts/:cohortId', getSpecificCohort);
router.get('/alumni', getAlumni);
router.get('/projects', getProjectsData);
router.get('/cohorts', getCohortsData);
router.get('/cohorts/:cohortid', getSpecificCohort);
router.get('/cohorts/:cohortId', getSpecificCohort);
router.get('/projects/:projectId', getProjectData);

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

const getProjectsData = async (req, res, next) => {
try {
const { type } = req.query;
if (
type.toLowerCase() === 'internal' ||
type.toLowerCase() === 'remotely'
) {
const { rows } = await getProjects(type);
if (rows.length > 0) {
res.json({
statusCode: 200,
data: rows,
});
} else {
res.json({
statusCode: 200,
message: `There is no ${type} projects`,
});
}
} else {
res.status(404).json({
statusCode: 404,
message: 'Please enter valid type',
});
}
} catch (err) {
next(err);
}
};

module.exports = getProjectsData;
2 changes: 2 additions & 0 deletions server/controllers/routes/user/project/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const getProjectsData = require('./getProjectsByType');
const getProjectData = require('./getProjectById');

module.exports = {
getProjectsData,
getProjectData,
};
16 changes: 16 additions & 0 deletions server/controllers/routes/user/student/getAlumni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { getAlumniQuery } = require('../../../../database/queries');

const getAlumni = async (req, res, next) => {
try {
const { rows } = await getAlumniQuery();
if (rows.length > 0) {
res.json({ statusCode: 200, data: rows });
} else {
res.json({ statusCode: 200, message: 'No Data' });
}
} catch (err) {
next(err);
}
};

module.exports = getAlumni;
5 changes: 5 additions & 0 deletions server/controllers/routes/user/student/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const getAlumni = require('./getAlumni');

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

const {
putSpecificCohort,
getCohorts,
Expand All @@ -12,6 +13,7 @@ const {
getProjectById,
deleteProject,
getCohortProjectsQuery,
getProjects,
} = require('./project');

module.exports = {
Expand All @@ -21,10 +23,12 @@ module.exports = {
putSpecificCohort,
getCohortQuery,
addProjectQuery,
getAlumniQuery,
getCohortProjectsQuery,
editProjectQuery,
deleteStudentQuery,
putStudent,
getProjectById,
deleteProject,
getProjects,
};
9 changes: 9 additions & 0 deletions server/database/queries/project/getProjectsByType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const connection = require('../../config/connection');

const getProjects = (ProjectType) =>
connection.query(
'SELECT * FROM project WHERE project_type ilike $1 ORDER BY name',
[ProjectType],
);

module.exports = getProjects;
2 changes: 2 additions & 0 deletions server/database/queries/project/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const getProjects = require('./getProjectsByType');
const addProjectQuery = require('./addProject');
const getCohortProjectsQuery = require('./getCohortProjects');
const editProjectQuery = require('./editProject');
const getProjectById = require('./getProjectById');
const deleteProject = require('./deleteProject');

module.exports = {
getProjects,
addProjectQuery,
getCohortProjectsQuery,
editProjectQuery,
Expand Down
5 changes: 5 additions & 0 deletions server/database/queries/student/getAlumni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const connection = require('../../config/connection');

const getAlumniQuery = () => connection.query('SELECT * FROM student');

module.exports = getAlumniQuery;
2 changes: 2 additions & 0 deletions server/database/queries/student/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const getAlumniQuery = require('./getAlumni');
const deleteStudentQuery = require('./deleteStudentQuery');
const putStudent = require('./putStudent');

module.exports = {
getAlumniQuery,
deleteStudentQuery,
putStudent,
};
63 changes: 63 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,41 @@ describe('Admin, Post Project', () => {
});
});

describe('Admin, Delete Specific Cohort', () => {
test('Route /cohorts/1 status 200, data.message = Cohort deleted successfully ', (done) => {
return request(app)
.delete('/api/v1/cohorts/1')
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
const { message } = res.body.data;
if (err) return done(err);
const { rows } = await connection.query(
'SELECT * from cohort WHERE id = 1',
);
expect(rows).toHaveLength(0);
expect(message).toBe('Cohort deleted successfully');
done();
});
});
});

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

test('Route /projects/10 status 404, data.message = Project does not exist ', (done) => {
return request(app)
.delete('/api/v1/projects/10')
Expand Down Expand Up @@ -174,6 +209,34 @@ describe('Delete specific student by ID', () => {
});
});

describe('Get project by type', () => {
test('Route /projects?type=internal status 200, json header, rows[0].name = Applicants System ', (done) => {
return request(app)
.get('/api/v1/projects?type=internal')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { name } = res.body.data[0];
expect(name).toBe('Applicants System');
done();
});
});

test('Route /projects?type=g58g status 404, json header ', (done) => {
return request(app)
.get('/api/v1/projects?type=gg')
.expect(404)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe('Please enter valid type');
done();
});
});
});

describe('Admin, Put project', () => {
const validData = {
name: 'Rehab',
Expand Down

0 comments on commit f1d5083

Please sign in to comment.