Skip to content

Commit

Permalink
Merge pull request #56 from GSG-G8/16-Get-cohort-Projects
Browse files Browse the repository at this point in the history
GET cohort/project?type=
  • Loading branch information
ranasobeid95 authored Apr 2, 2020
2 parents 8c93a5e + 6b3fd06 commit b73ba41
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 18 deletions.
6 changes: 3 additions & 3 deletions server/controllers/routes/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const router = require('express').Router();

const { editCohort } = require('./cohort');
const { deleteCohort } = require('./cohort');
const { addProject, editProject } = require('./project');
const { editCohort, deleteCohort } = require('./cohort');
const { addProject, editProject, getCohortProjects } = require('./project');
const { deleteStudent } = require('./student');

router
Expand All @@ -22,6 +21,7 @@ router

router.put('/projects/:projectId', editProject);
router.post('/projects', addProject);
router.get('/cohorts/:cohortId/projects', getCohortProjects);
router.delete('/alumni/:studentId', deleteStudent);

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

const getCohortProjects = async (req, res, next) => {
try {
const { cohortId } = req.params;
const projectType = req.query.type;
if (
(cohortId > 0 && projectType.toLowerCase() === 'internal') ||
projectType.toLowerCase() === 'remotely'
) {
const { rows } = await getCohortProjectsQuery(cohortId, projectType);
if (rows.length > 0) {
res.json({ statusCode: 200, data: rows });
} else {
res.json({ statusCode: 200, message: 'No Data' });
}
} else {
res.status(404).json({
statusCode: 404,
message: 'Please check cohort ID you entered or project type',
});
}
} catch (err) {
next(err);
}
};

module.exports = getCohortProjects;
2 changes: 2 additions & 0 deletions server/controllers/routes/admin/project/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const addProject = require('./addProject');
const getCohortProjects = require('./getCohortProjects');
const editProject = require('./editProject');

module.exports = {
addProject,
getCohortProjects,
editProject,
};
6 changes: 3 additions & 3 deletions server/controllers/routes/user/cohort/getSpecificCohort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const { getCohortQuery } = require('../../../../database/queries');

const getSpecificCohort = async (req, res, next) => {
try {
const { cohortid } = req.params;
if (cohortid > 0) {
const { rows } = await getCohortQuery(cohortid);
const { cohortId } = req.params;
if (cohortId > 0) {
const { rows } = await getCohortQuery(cohortId);
const data = { ...rows[0] };
if (data.id) {
res.json({ statusCode: 200, data });
Expand Down
1 change: 1 addition & 0 deletions server/controllers/routes/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const router = require('express').Router();
const { getCohortsData } = require('./cohort');
const { getSpecificCohort } = require('./cohort');

router.get('/cohorts/:cohortId', getSpecificCohort);
router.get('/cohorts', getCohortsData);
router.get('/cohorts/:cohortid', getSpecificCohort);

Expand Down
20 changes: 15 additions & 5 deletions server/database/config/fakeData.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,37 @@ INSERT INTO project (name , description , img_url , github_link , website_link ,
'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page',
'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA',
'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3',
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal project',1),
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal',1),
('ca-wiki',
'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page',
'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA',
'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3',
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal project',2),
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal',2),
('ca-wiki',
'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page',
'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA',
'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3',
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal project',1),
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal',1),
('ca-wiki',
'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page',
'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA',
'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3',
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal project',2),
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal',2),

('events-booker',
'This app to help GSG organization in organizing the registration for the events that they do. So When they announced for an event, it will be known who would like to attend and they can ensure if this person attended or not. By this way, the GSG can have information about the people who attended and they can use it in the future as they want.',
'https://d2slcw3kip6qmk.cloudfront.net/marketing/blog/2017Q2/[email protected]',
'https://github.com/GSG-G8/events-booker',
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Remotely project',2);
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Remotely',2),
('room-booker',
'Gaza sky Geeks (GSG) has many rooms and many staff members with the ability to book any room but there is no way to know who booked what',
'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA',
'https://github.com/GSG-G8/room-booker',
'https://github.com/GSG-G8/room-booker','Remotely',1),
('Applicants System',
'An application to facilitate code academy application process for applicants and help them to complete their applications easily and on time, also help code academy team to track , filter and create applications.',
'https://d2slcw3kip6qmk.cloudfront.net/marketing/blog/2017Q2/[email protected]',
'https://github.com/GSG-G8/applicants-system',
'https://github.com/GSG-G8/applicants-system','Internal',2);

INSERT INTO student_project (student_id , project_id) VALUES (1 , 1) , (2 , 2);
17 changes: 12 additions & 5 deletions server/database/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const { putSpecificCohort } = require('./cohort');
const { getCohorts } = require('./cohort');
const { getCohortQuery } = require('./cohort');
const { deleteCohortQuery } = require('./cohort');
const { addProjectQuery, editProjectQuery } = require('./project');
const {
getCohorts,
getCohortQuery,
deleteCohortQuery,
putSpecificCohort,
} = require('./cohort');
const {
addProjectQuery,
editProjectQuery,
getCohortProjectsQuery,
} = require('./project');
const { deleteStudentQuery } = require('./student');

module.exports = {
Expand All @@ -11,6 +17,7 @@ module.exports = {
putSpecificCohort,
getCohortQuery,
addProjectQuery,
getCohortProjectsQuery,
editProjectQuery,
deleteStudentQuery,
};
9 changes: 9 additions & 0 deletions server/database/queries/project/getCohortProjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const connection = require('../../config/connection');

const getCohortProjectsQuery = (cohortId, projectType) =>
connection.query(
'SELECT * FROM project WHERE cohort_id = $1 AND project_type ilike $2',
[cohortId, projectType],
);

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

module.exports = {
addProjectQuery,
getCohortProjectsQuery,
editProjectQuery,
};
78 changes: 76 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,81 @@ describe('Get Specific Cohort', () => {
});
});

describe('Get Specific Cohort Projects', () => {
test('Route /cohorts/1/projects?type=internal status 200, json header, data.length=1', (done) => {
return request(app)
.get('/api/v1/cohorts/1/projects?type=internal')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { data } = res.body;
expect(data).toHaveLength(2);
expect(data[0].name).toBe('ca-wiki');
expect(data[0].project_type).toBe('Internal');
done();
});
});

test('Route /cohorts/1/projects?type=remotely status 200, json header, data.length=1', (done) => {
return request(app)
.get('/api/v1/cohorts/1/projects?type=remotely')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { data } = res.body;
expect(data).toHaveLength(1);
expect(data[0].name).toBe('room-booker');
expect(data[0].project_type).toBe('Remotely');
done();
});
});

test('Route /cohorts/10/projects?type=internal status 200, json header, data.message = "No Data" ', (done) => {
return request(app)
.get('/api/v1/cohorts/10/projects?type=internal')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe('No Data');
done();
});
});

test('Route /cohorts/G1/projects?type=internal status 404, json header, data.message = "You enterd wrong cohort ID" ', (done) => {
return request(app)
.get('/api/v1/cohorts/G1/projects?type=internal')
.expect(404)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe(
'Please check cohort ID you entered or project type',
);
done();
});
});

test('Route /cohorts/1/projects?type=international status 404, json header, data.message = "You enterd wrong cohort ID" ', (done) => {
return request(app)
.get('/api/v1/cohorts/G1/projects?type=international')
.expect(404)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe(
'Please check cohort ID you entered or project type',
);
done();
});
});
});

describe('Post Cohort', () => {
const data = {
name: 'G1',
Expand Down Expand Up @@ -144,7 +219,6 @@ describe('Admin, (/cohorts/:cohortId)', () => {
});
});


describe('Admin, Post Project', () => {
test('Route /projects status 200, json header, data.message = Cohort Added successfully ', (done) => {
const reqData = {
Expand All @@ -166,7 +240,7 @@ describe('Admin, Post Project', () => {
const { message } = res.body.data;
if (err) return done(err);
const { rows } = await connection.query(
'SELECT * from project WHERE id = 6',
'SELECT * from project WHERE id = 8',
);
expect(rows[0].name).toBe('Mohmmedzw851@');
expect(message).toBe('Project Added successfully');
Expand Down

0 comments on commit b73ba41

Please sign in to comment.