Skip to content

Commit

Permalink
change route name and abstract its' function
Browse files Browse the repository at this point in the history
realtes #27
  • Loading branch information
MohammedAlghazali committed Apr 4, 2020
1 parent 58343b6 commit 2f681a7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion server/controllers/routes/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { getProjectData, getProjectsData } = require('./project');
const { getCohortsData, getSpecificCohort } = require('./cohort');
const { getAlumniCohort } = require('./student');

router.get('/alumni/cohorts/:cohortId', getAlumniCohort);
router.get('/cohorts/:cohortId/alumni', getAlumniCohort);
router.get('/alumni', getAlumni);
router.get('/projects', getProjectsData);
router.get('/cohorts', getCohortsData);
Expand Down
20 changes: 6 additions & 14 deletions server/controllers/routes/user/student/getAlumniCohort.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
const { getSpecificAlumni } = require('../../../../database/queries');
const { getCohortQuery } = require('../../../../database/queries');

const getAlumniCohort = async (req, res, next) => {
try {
const { cohortId } = req.params;
if (cohortId > 0) {
const checkCohort = await getCohortQuery(cohortId);
if (checkCohort.rowCount !== 0) {
const { rows } = await getSpecificAlumni(cohortId);
res.json({ statusCode: 200, data: rows });
} else {
res.status(404).json({
statusCode: 404,
message: 'cohort does not exists',
});
}
const { rows } = await getSpecificAlumni(cohortId);
res.json({ statusCode: 200, data: rows });
} else {
res
.status(404)
.json({ statusCode: 404, message: 'You enterd wrong cohort ID' });
res.status(404).json({
statusCode: 404,
message: 'cohort does not exists',
});
}
} catch (err) {
next(err);
Expand Down
2 changes: 1 addition & 1 deletion server/database/queries/student/getSpecificAlumni.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const connection = require('../../config/connection');

const getSpecificAlumni = (id) =>
connection.query('select * from student where cohort_id = $1', [id]);
connection.query('SELECT * FROM student WHERE cohort_id = $1', [id]);

module.exports = getSpecificAlumni;
20 changes: 10 additions & 10 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,27 @@ describe('Delete specific student by ID', () => {
});

describe('alumni for cohort', () => {
test('Route /alumni/cohorts/10 status 404, json header, data.message = "cohort does not exists" ', (done) => {
test('Route /cohorts/10/alumni status 200, json header ', (done) => {
return request(app)
.get('/api/v1/alumni/cohorts/10')
.expect(404)
.get('/api/v1/cohorts/10/alumni')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe('cohort does not exists');
const { data } = res.body;
expect(data).toHaveLength(0);
done();
});
});
test('Route /alumni/cohorts/g status 404, json header, data.message = "You enterd wrong cohort ID" ', (done) => {
test('Route /cohorts/g/alumni status 404, json header, data.message = "cohort does not exists" ', (done) => {
return request(app)
.get('/api/v1/alumni/cohorts/g')
.get('/api/v1/cohorts/g/alumni')
.expect(404)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe('You enterd wrong cohort ID');
expect(message).toBe('cohort does not exists');
done();
});
});
Expand Down Expand Up @@ -411,9 +411,9 @@ describe('Admin, Put project', () => {
});
});

test('Route /alumni/cohorts/2 status 200, json header, data.name =Alaa ', (done) => {
test('Route /cohorts/2/alumni status 200, json header, data.name =Alaa ', (done) => {
return request(app)
.get('/api/v1/alumni/cohorts/2')
.get('/api/v1/cohorts/2/alumni')
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
Expand Down

0 comments on commit 2f681a7

Please sign in to comment.