Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GET | Get alumnus projects data #140

Merged
merged 5 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions server/controllers/getStudentProjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { getStudentProjectsQuery } = require('../database/queries');

const getStudentProjects = async (req, res, next) => {
try {
const { studentId } = req.params;
if (studentId > 0) {
const { rows } = await getStudentProjectsQuery(studentId);
if (rows[0]) {
res.json({ statusCode: 200, data: rows });
} else {
res.json({
statusCode: 200,
message: 'There is no projects for this student id',
});
}
} else {
res.status(404).json({
statusCode: 404,
message: 'Invalid id',
});
}
} catch (err) {
next(err);
}
};

module.exports = getStudentProjects;
2 changes: 2 additions & 0 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const getAlumniCohort = require('./getAlumniCohort');
const getAlumni = require('./getAlumni');
const getStudentById = require('./getStudentById');
const getCohortProjects = require('./getCohortProjects');
const getStudentProjects = require('./getStudentProjects');
const {
addCohort,
addProject,
Expand Down Expand Up @@ -44,4 +45,5 @@ module.exports = {
logout,
putStudentData,
getStats,
getStudentProjects,
};
2 changes: 2 additions & 0 deletions server/database/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
addStudent,
getStudentByIdQuery,
getSpecificAlumni,
getStudentProjectsQuery,
} = require('./student');

const {
Expand Down Expand Up @@ -47,4 +48,5 @@ module.exports = {
getStatsQuery,
loginQuery,
studentProjectQuery,
getStudentProjectsQuery,
};
8 changes: 8 additions & 0 deletions server/database/queries/student/getStudentProjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const connection = require('../../connection');

const getStudentProjectsQuery = (id) =>
connection.query(
'SELECT * FROM project INNER JOIN student_project ON project.id = student_project.student_id WHERE student_id = $1',
[id],
);
module.exports = getStudentProjectsQuery;
2 changes: 2 additions & 0 deletions server/database/queries/student/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const getSpecificAlumni = require('./getSpecificAlumni');
const putStudent = require('./putStudent');
const addStudent = require('./addStudent');
const getStudentByIdQuery = require('./getStudentByIdQuery');
const getStudentProjectsQuery = require('./getStudentProjects');

module.exports = {
getAlumniQuery,
Expand All @@ -12,4 +13,5 @@ module.exports = {
getSpecificAlumni,
putStudent,
getStudentByIdQuery,
getStudentProjectsQuery,
};
7 changes: 6 additions & 1 deletion server/routes/alumni.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const router = require('express').Router();

const { getAlumni, getStudentById } = require('../controllers');
const {
getAlumni,
getStudentById,
getStudentProjects,
} = require('../controllers');

router.get('/alumni', getAlumni);
router.get('/alumni/:studentId', getStudentById);
router.get('/alumni/:studentId/projects', getStudentProjects);

module.exports = router;
55 changes: 55 additions & 0 deletions test/alumni.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,58 @@ describe('Delete specific student by ID', () => {
});
});
});

describe('Get student projects', () => {
test('Route /alumni/1/projects status 200, json header, data ', (done) => {
return request(app)
.get('/api/v1/alumni/1/projects')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { data } = res.body;
expect(data[0]).toEqual({
id: 1,
name: 'ca-wiki',
description:
'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',
img_url: 'https://imgur.com/gVwD2Wi.png',
github_link:
'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3',
website_link:
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D',
project_type: 'Internal',
cohort_id: 1,
student_id: 1,
project_id: 1,
});
done();
});
});

test('Route /alumni/18/projects status 200, json header ', (done) => {
return request(app)
.get('/api/v1/alumni/18/projects')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe('There is no projects for this student id');
done();
});
});

test('Route /alumni/Alaa/projects status 404, json header ', (done) => {
return request(app)
.get('/api/v1/alumni/Alaa/projects')
.expect(404)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe('Invalid id');
done();
});
});
});