From 9cd9189ddd54ca0226f8440411b496ffc0a95631 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 00:44:51 +0300 Subject: [PATCH 01/11] handle Delete cohort route relates #29 --- .eslintrc.json | 4 +++- server/controllers/index.js | 3 +++ .../routes/admin/cohort/deleteCohort.js | 16 ++++++++++++++ .../controllers/routes/admin/cohort/index.js | 5 +++++ server/controllers/routes/admin/index.js | 22 +++++++++++++++++++ .../database/queries/cohort/deleteCohort.js | 6 +++++ 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 server/controllers/routes/admin/cohort/deleteCohort.js create mode 100644 server/database/queries/cohort/deleteCohort.js diff --git a/.eslintrc.json b/.eslintrc.json index c3bb1f31..538da47c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -34,6 +34,8 @@ "jest/no-focused-tests": "error", "jest/no-identical-title": "error", "jest/prefer-to-have-length": "warn", - "jest/valid-expect": "error" + "jest/valid-expect": "error", + "consistent-return":"off", + "jest/no-test-callback":"off" } } diff --git a/server/controllers/index.js b/server/controllers/index.js index 403948e7..24822543 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,7 +1,10 @@ const router = require('express').Router(); +const admin = require('./routes/admin'); router.get('/', (req, res) => { res.send('

CA WIKI

'); }); +router.use(admin); + module.exports = router; diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js new file mode 100644 index 00000000..e3cc713a --- /dev/null +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -0,0 +1,16 @@ +const deleteCohort = require('../../../../database/queries/cohort/deleteCohort'); + +const cohortDelete = (req, res, next) => { + deleteCohort(req.params.cohortId).then(() => + res + .json({ + StatusCode: 200, + data: { message: 'Cohort deleted successfully' }, + }) + .catch(next), + ); +}; + +module.exports = { + cohortDelete, +}; diff --git a/server/controllers/routes/admin/cohort/index.js b/server/controllers/routes/admin/cohort/index.js index e69de29b..5e658610 100644 --- a/server/controllers/routes/admin/cohort/index.js +++ b/server/controllers/routes/admin/cohort/index.js @@ -0,0 +1,5 @@ +const { cohortDelete } = require('./deleteCohort'); + +module.exports = { + cohortDelete, +}; diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index e69de29b..097affbd 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -0,0 +1,22 @@ +const router = require('express').Router(); +const { cohortDelete } = require('./cohort'); + +router + .route('/cohorts/:cohortId') + .all((req, res, next) => { + // runs for all HTTP verbs first + // think of it as route specific middleware! + next(); + }) + .get((req, res, next) => { + next(new Error('not implemented')); + }) + .put((req, res, next) => { + next(new Error('not implemented')); + }) + .post((req, res, next) => { + next(new Error('not implemented')); + }) + .delete(cohortDelete); + +module.exports = router; diff --git a/server/database/queries/cohort/deleteCohort.js b/server/database/queries/cohort/deleteCohort.js new file mode 100644 index 00000000..f9da6f74 --- /dev/null +++ b/server/database/queries/cohort/deleteCohort.js @@ -0,0 +1,6 @@ +const connection = require('../../config/connection'); + +const deleteCohort = (cohortId) => + connection.query('DELETE FROM cohort WHERE id = $1', [cohortId]); + +module.exports = deleteCohort; From b0d6b20bac0d135176a9a720840f499052a55985 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 11:41:08 +0300 Subject: [PATCH 02/11] fix error in catch relates #29 --- .../controllers/routes/admin/cohort/deleteCohort.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index e3cc713a..bf408c87 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -1,14 +1,14 @@ const deleteCohort = require('../../../../database/queries/cohort/deleteCohort'); const cohortDelete = (req, res, next) => { - deleteCohort(req.params.cohortId).then(() => - res - .json({ + deleteCohort(req.params.cohortId) + .then(() => + res.json({ StatusCode: 200, data: { message: 'Cohort deleted successfully' }, - }) - .catch(next), - ); + }), + ) + .catch(next); }; module.exports = { From 9bbdfdf91acffafe6a1ccd2fff5df73868872d21 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 11:42:34 +0300 Subject: [PATCH 03/11] make test of admin delete route /cohort/:cohortId relates #29 --- test/index.test.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 1c71181d..1bf35312 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,3 +1,28 @@ -test('Initial test', () => { - expect(1).toBe(1); +const request = require('supertest'); +const connection = require('../server/database/config/connection'); +const dbBuild = require('../server/database/config/build'); + +const app = require('../server/app'); + +beforeAll(() => { + return dbBuild(); +}); + +afterAll(() => { + return connection.end(); +}); + +describe('Admin, (/cohorts/:cohortId)', () => { + test('Route /cohorts/1 status 200, json header, data.message = Cohort deleted successfully ', (done) => { + return request(app) + .delete('/api/v1/cohorts/1') + .expect(200) + .expect('Content-Type', /json/) + .end((err, res) => { + const { data } = res.body; + if (err) return done(err); + expect(data.message).toBe('Cohort deleted successfully'); + done(); + }); + }); }); From fa8c6f0c47c4aeae5df2256a9b688f3a4af40192 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 12:31:55 +0300 Subject: [PATCH 04/11] convert promise to async/await in deleteCohort.js relates #29 --- .../routes/admin/cohort/deleteCohort.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index bf408c87..c65377fb 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -1,14 +1,15 @@ const deleteCohort = require('../../../../database/queries/cohort/deleteCohort'); -const cohortDelete = (req, res, next) => { - deleteCohort(req.params.cohortId) - .then(() => - res.json({ - StatusCode: 200, - data: { message: 'Cohort deleted successfully' }, - }), - ) - .catch(next); +const cohortDelete = async (req, res, next) => { + try { + await deleteCohort(req.params.cohortId); + res.json({ + StatusCode: 200, + data: { message: 'Cohort deleted successfully' }, + }); + } catch (err) { + next(err); + } }; module.exports = { From c613d5b35cd639169d987985138e4ea30a3b38f2 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 20:37:37 +0300 Subject: [PATCH 05/11] check if cohort id exist or not relates #29 --- .../routes/admin/cohort/deleteCohort.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index c65377fb..8d904bb2 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -2,11 +2,17 @@ const deleteCohort = require('../../../../database/queries/cohort/deleteCohort') const cohortDelete = async (req, res, next) => { try { - await deleteCohort(req.params.cohortId); - res.json({ - StatusCode: 200, - data: { message: 'Cohort deleted successfully' }, - }); + const check = await deleteCohort(req.params.cohortId); + if (check.rowCount !== 0) { + res.json({ + StatusCode: 200, + data: { message: 'Cohort deleted successfully' }, + }); + } else { + const err = new Error(); + err.message = 'cohort id does not exist'; + next(err); + } } catch (err) { next(err); } From 01cc88be9b15adeea53b4a86b00bd3a9d545c491 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 20:41:56 +0300 Subject: [PATCH 06/11] explanation comment for route.all using relates #29 --- server/controllers/routes/admin/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index 097affbd..ca4ceaa7 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -4,8 +4,7 @@ const { cohortDelete } = require('./cohort'); router .route('/cohorts/:cohortId') .all((req, res, next) => { - // runs for all HTTP verbs first - // think of it as route specific middleware! + // ToDo: make middleware to check authentication next(); }) .get((req, res, next) => { From df31b0aac51d2816cc825ef666a0c969dd29d2a8 Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 20:54:46 +0300 Subject: [PATCH 07/11] Modify some default export and link the file with index in database/query relates #29 --- server/controllers/routes/admin/cohort/deleteCohort.js | 6 ++---- server/controllers/routes/admin/cohort/index.js | 2 +- server/database/queries/cohort/index.js | 5 +++++ server/database/queries/index.js | 5 +++++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index 8d904bb2..a70452a2 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -1,4 +1,4 @@ -const deleteCohort = require('../../../../database/queries/cohort/deleteCohort'); +const { deleteCohort } = require('../../../../database/queries'); const cohortDelete = async (req, res, next) => { try { @@ -18,6 +18,4 @@ const cohortDelete = async (req, res, next) => { } }; -module.exports = { - cohortDelete, -}; +module.exports = cohortDelete; diff --git a/server/controllers/routes/admin/cohort/index.js b/server/controllers/routes/admin/cohort/index.js index 5e658610..036fb583 100644 --- a/server/controllers/routes/admin/cohort/index.js +++ b/server/controllers/routes/admin/cohort/index.js @@ -1,4 +1,4 @@ -const { cohortDelete } = require('./deleteCohort'); +const cohortDelete = require('./deleteCohort'); module.exports = { cohortDelete, diff --git a/server/database/queries/cohort/index.js b/server/database/queries/cohort/index.js index e69de29b..f3feddab 100644 --- a/server/database/queries/cohort/index.js +++ b/server/database/queries/cohort/index.js @@ -0,0 +1,5 @@ +const deleteCohort = require('./deleteCohort'); + +module.exports = { + deleteCohort, +}; diff --git a/server/database/queries/index.js b/server/database/queries/index.js index e69de29b..15658d72 100644 --- a/server/database/queries/index.js +++ b/server/database/queries/index.js @@ -0,0 +1,5 @@ +const { deleteCohort } = require('./cohort'); + +module.exports = { + deleteCohort, +}; From d2afa98a9a6a604943f9ad12c2cb07bd0e29494b Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Mon, 30 Mar 2020 21:29:54 +0300 Subject: [PATCH 08/11] add query test fot /cohorts/:cohortId route to be sure the delete done relates #29 --- test/index.test.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 1bf35312..89f1040b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -13,14 +13,18 @@ afterAll(() => { }); describe('Admin, (/cohorts/:cohortId)', () => { - test('Route /cohorts/1 status 200, json header, data.message = Cohort deleted successfully ', (done) => { + 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((err, res) => { + .end(async (err, res) => { const { data } = res.body; if (err) return done(err); + const result = await connection.query( + 'SELECT * from cohort WHERE id = 1', + ); + expect(result.rows).toHaveLength(0); expect(data.message).toBe('Cohort deleted successfully'); done(); }); From acf5b2bcbe5c9382c5759fb49f0ef0a7f2d1f3ee Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 14:54:34 +0300 Subject: [PATCH 09/11] change query function name and route function relates #29 --- server/controllers/routes/admin/cohort/deleteCohort.js | 8 ++++---- server/controllers/routes/admin/cohort/index.js | 4 ++-- server/controllers/routes/admin/index.js | 4 ++-- server/database/queries/cohort/deleteCohort.js | 4 ++-- server/database/queries/cohort/index.js | 4 ++-- server/database/queries/index.js | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index a70452a2..78a23b91 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -1,8 +1,8 @@ -const { deleteCohort } = require('../../../../database/queries'); +const { deleteCohortQuery } = require('../../../../database/queries'); -const cohortDelete = async (req, res, next) => { +const deleteCohort = async (req, res, next) => { try { - const check = await deleteCohort(req.params.cohortId); + const check = await deleteCohortQuery(req.params.cohortId); if (check.rowCount !== 0) { res.json({ StatusCode: 200, @@ -18,4 +18,4 @@ const cohortDelete = async (req, res, next) => { } }; -module.exports = cohortDelete; +module.exports = deleteCohort; diff --git a/server/controllers/routes/admin/cohort/index.js b/server/controllers/routes/admin/cohort/index.js index 036fb583..f3feddab 100644 --- a/server/controllers/routes/admin/cohort/index.js +++ b/server/controllers/routes/admin/cohort/index.js @@ -1,5 +1,5 @@ -const cohortDelete = require('./deleteCohort'); +const deleteCohort = require('./deleteCohort'); module.exports = { - cohortDelete, + deleteCohort, }; diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index ca4ceaa7..465de719 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -1,5 +1,5 @@ const router = require('express').Router(); -const { cohortDelete } = require('./cohort'); +const { deleteCohort } = require('./cohort'); router .route('/cohorts/:cohortId') @@ -16,6 +16,6 @@ router .post((req, res, next) => { next(new Error('not implemented')); }) - .delete(cohortDelete); + .delete(deleteCohort); module.exports = router; diff --git a/server/database/queries/cohort/deleteCohort.js b/server/database/queries/cohort/deleteCohort.js index f9da6f74..a349750b 100644 --- a/server/database/queries/cohort/deleteCohort.js +++ b/server/database/queries/cohort/deleteCohort.js @@ -1,6 +1,6 @@ const connection = require('../../config/connection'); -const deleteCohort = (cohortId) => +const deleteCohortQuery = (cohortId) => connection.query('DELETE FROM cohort WHERE id = $1', [cohortId]); -module.exports = deleteCohort; +module.exports = deleteCohortQuery; diff --git a/server/database/queries/cohort/index.js b/server/database/queries/cohort/index.js index f3feddab..9044c80d 100644 --- a/server/database/queries/cohort/index.js +++ b/server/database/queries/cohort/index.js @@ -1,5 +1,5 @@ -const deleteCohort = require('./deleteCohort'); +const deleteCohortQuery = require('./deleteCohort'); module.exports = { - deleteCohort, + deleteCohortQuery, }; diff --git a/server/database/queries/index.js b/server/database/queries/index.js index 15658d72..c0d36a24 100644 --- a/server/database/queries/index.js +++ b/server/database/queries/index.js @@ -1,5 +1,5 @@ -const { deleteCohort } = require('./cohort'); +const { deleteCohortQuery } = require('./cohort'); module.exports = { - deleteCohort, + deleteCohortQuery, }; From a4b2af5880cbe57080e335c12e173bd98e93fbbf Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 20:47:56 +0300 Subject: [PATCH 10/11] handle error and make some destructure reelates #29 --- .../routes/admin/cohort/deleteCohort.js | 10 ++++++---- test/index.test.js | 16 ++++++---------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index 78a23b91..bbf52d0b 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -2,16 +2,18 @@ const { deleteCohortQuery } = require('../../../../database/queries'); const deleteCohort = async (req, res, next) => { try { - const check = await deleteCohortQuery(req.params.cohortId); + const { cohortId } = req.params; + const check = await deleteCohortQuery(cohortId); if (check.rowCount !== 0) { res.json({ StatusCode: 200, data: { message: 'Cohort deleted successfully' }, }); } else { - const err = new Error(); - err.message = 'cohort id does not exist'; - next(err); + res.status(400).json({ + StatusCode: 400, + data: { message: 'Cohort does not exist' }, + }); } } catch (err) { next(err); diff --git a/test/index.test.js b/test/index.test.js index 89f1040b..eb16632b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -4,13 +4,9 @@ const dbBuild = require('../server/database/config/build'); const app = require('../server/app'); -beforeAll(() => { - return dbBuild(); -}); +beforeAll(() => dbBuild()); -afterAll(() => { - return connection.end(); -}); +afterAll(() => connection.end()); describe('Admin, (/cohorts/:cohortId)', () => { test('Route /cohorts/1 status 200, data.message = Cohort deleted successfully ', (done) => { @@ -19,13 +15,13 @@ describe('Admin, (/cohorts/:cohortId)', () => { .expect(200) .expect('Content-Type', /json/) .end(async (err, res) => { - const { data } = res.body; + const { message } = res.body.data; if (err) return done(err); - const result = await connection.query( + const { rows } = await connection.query( 'SELECT * from cohort WHERE id = 1', ); - expect(result.rows).toHaveLength(0); - expect(data.message).toBe('Cohort deleted successfully'); + expect(rows).toHaveLength(0); + expect(message).toBe('Cohort deleted successfully'); done(); }); }); From d131a1efe37948d1949c9c2c0cd854a79fe631db Mon Sep 17 00:00:00 2001 From: MohammedAlghazali Date: Tue, 31 Mar 2020 21:10:25 +0300 Subject: [PATCH 11/11] modify status code for error relates #29 --- server/controllers/routes/admin/cohort/deleteCohort.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/controllers/routes/admin/cohort/deleteCohort.js b/server/controllers/routes/admin/cohort/deleteCohort.js index bbf52d0b..07709373 100644 --- a/server/controllers/routes/admin/cohort/deleteCohort.js +++ b/server/controllers/routes/admin/cohort/deleteCohort.js @@ -10,8 +10,8 @@ const deleteCohort = async (req, res, next) => { data: { message: 'Cohort deleted successfully' }, }); } else { - res.status(400).json({ - StatusCode: 400, + res.status(404).json({ + StatusCode: 404, data: { message: 'Cohort does not exist' }, }); }