From 2f681a78621f80f49fb6cdd150c95260d4427dc2 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Sat, 4 Apr 2020 17:59:43 +0300 Subject: [PATCH] change route name and abstract its' function realtes #27 --- server/controllers/routes/user/index.js | 2 +- .../routes/user/student/getAlumniCohort.js | 20 ++++++------------- .../queries/student/getSpecificAlumni.js | 2 +- test/index.test.js | 20 +++++++++---------- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/server/controllers/routes/user/index.js b/server/controllers/routes/user/index.js index 32059ab4..a996aceb 100644 --- a/server/controllers/routes/user/index.js +++ b/server/controllers/routes/user/index.js @@ -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); diff --git a/server/controllers/routes/user/student/getAlumniCohort.js b/server/controllers/routes/user/student/getAlumniCohort.js index 8e2d2c37..a4f08fee 100644 --- a/server/controllers/routes/user/student/getAlumniCohort.js +++ b/server/controllers/routes/user/student/getAlumniCohort.js @@ -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); diff --git a/server/database/queries/student/getSpecificAlumni.js b/server/database/queries/student/getSpecificAlumni.js index 836ca119..7ed02801 100644 --- a/server/database/queries/student/getSpecificAlumni.js +++ b/server/database/queries/student/getSpecificAlumni.js @@ -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; diff --git a/test/index.test.js b/test/index.test.js index d6baeea2..fe1fde5f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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(); }); }); @@ -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) => {