From 2095e7b021b01b176ecc064e224bf6d399b71fca Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Tue, 31 Mar 2020 01:57:17 +0300 Subject: [PATCH 01/14] Add tests --- server/controllers/index.js | 6 +-- .../controllers/routes/admin/cohort/index.js | 3 ++ .../routes/admin/cohort/putSpecificCohort.js | 19 ++++++++ server/controllers/routes/admin/index.js | 3 ++ .../routes/user/cohort/getSpecificCohort.js | 21 --------- .../controllers/routes/user/cohort/index.js | 3 -- server/controllers/routes/user/index.js | 3 -- .../queries/cohort/getSpecificCohort.js | 4 -- server/database/queries/cohort/index.js | 4 +- .../queries/cohort/putSpecificCohort.js | 7 +++ test/index.test.js | 46 +++++++++++++++---- 11 files changed, 74 insertions(+), 45 deletions(-) create mode 100644 server/controllers/routes/admin/cohort/putSpecificCohort.js delete mode 100644 server/controllers/routes/user/cohort/getSpecificCohort.js delete mode 100644 server/database/queries/cohort/getSpecificCohort.js create mode 100644 server/database/queries/cohort/putSpecificCohort.js diff --git a/server/controllers/index.js b/server/controllers/index.js index fca0260c..20f1a4ab 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,8 +1,8 @@ const router = require('express').Router(); const { - cohort: { getSpecificCohort }, -} = require('./routes/user'); + cohort: { putSpecificCohort }, +} = require('./routes/admin'); -router.get('/cohort/:cohortid', getSpecificCohort); +router.put('/cohort/:cohortid', putSpecificCohort); module.exports = router; diff --git a/server/controllers/routes/admin/cohort/index.js b/server/controllers/routes/admin/cohort/index.js index e69de29b..7bfd23f3 100644 --- a/server/controllers/routes/admin/cohort/index.js +++ b/server/controllers/routes/admin/cohort/index.js @@ -0,0 +1,3 @@ +const { putSpecificCohort } = require('./putSpecificCohort'); + +module.exports = { putSpecificCohort }; diff --git a/server/controllers/routes/admin/cohort/putSpecificCohort.js b/server/controllers/routes/admin/cohort/putSpecificCohort.js new file mode 100644 index 00000000..416c2569 --- /dev/null +++ b/server/controllers/routes/admin/cohort/putSpecificCohort.js @@ -0,0 +1,19 @@ +const { + cohort: { putSpecificCohort }, +} = require('../../../../database/queries'); + +exports.putSpecificCohort = async (req, res, next) => { + try { + const result = await putSpecificCohort(req.params.cohortid, req.body); + if (result.rowCount === 1) { + res.json({ statusCode: 200, message: 'Changed Succefully' }); + } else { + res.status(404).json({ + statusCode: 404, + message: "Sorry There's no cohort for this id to change", + }); + } + } catch (err) { + next(err); + } +}; diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index e69de29b..366d6a19 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -0,0 +1,3 @@ +const cohort = require('./cohort'); + +module.exports = { cohort }; diff --git a/server/controllers/routes/user/cohort/getSpecificCohort.js b/server/controllers/routes/user/cohort/getSpecificCohort.js deleted file mode 100644 index 84ee170a..00000000 --- a/server/controllers/routes/user/cohort/getSpecificCohort.js +++ /dev/null @@ -1,21 +0,0 @@ -const { - cohort: { getSpecificCohort }, -} = require('../../../../database/queries'); - -exports.getSpecificCohort = async (req, res, next) => { - try { - const { cohortid } = req.params; - const { rows } = await getSpecificCohort(cohortid); - const data = { ...rows[0] }; - if (data.id) { - res.json({ statusCode: 200, data }); - } else { - res.status(404).json({ - statusCode: 404, - message: "Sorry There's no cohort for this id", - }); - } - } catch (err) { - next(err); - } -}; diff --git a/server/controllers/routes/user/cohort/index.js b/server/controllers/routes/user/cohort/index.js index c7df39f7..e69de29b 100644 --- a/server/controllers/routes/user/cohort/index.js +++ b/server/controllers/routes/user/cohort/index.js @@ -1,3 +0,0 @@ -const { getSpecificCohort } = require('./getSpecificCohort'); - -module.exports = { getSpecificCohort }; diff --git a/server/controllers/routes/user/index.js b/server/controllers/routes/user/index.js index 366d6a19..e69de29b 100644 --- a/server/controllers/routes/user/index.js +++ b/server/controllers/routes/user/index.js @@ -1,3 +0,0 @@ -const cohort = require('./cohort'); - -module.exports = { cohort }; diff --git a/server/database/queries/cohort/getSpecificCohort.js b/server/database/queries/cohort/getSpecificCohort.js deleted file mode 100644 index 0006d50b..00000000 --- a/server/database/queries/cohort/getSpecificCohort.js +++ /dev/null @@ -1,4 +0,0 @@ -const connection = require('../../config/connection'); - -exports.getSpecificCohort = (id) => - connection.query('select * from cohort where id = $1', [id]); diff --git a/server/database/queries/cohort/index.js b/server/database/queries/cohort/index.js index c7df39f7..7bfd23f3 100644 --- a/server/database/queries/cohort/index.js +++ b/server/database/queries/cohort/index.js @@ -1,3 +1,3 @@ -const { getSpecificCohort } = require('./getSpecificCohort'); +const { putSpecificCohort } = require('./putSpecificCohort'); -module.exports = { getSpecificCohort }; +module.exports = { putSpecificCohort }; diff --git a/server/database/queries/cohort/putSpecificCohort.js b/server/database/queries/cohort/putSpecificCohort.js new file mode 100644 index 00000000..f526f49e --- /dev/null +++ b/server/database/queries/cohort/putSpecificCohort.js @@ -0,0 +1,7 @@ +const connection = require('../../config/connection'); + +exports.putSpecificCohort = (id, req) => + connection.query( + 'UPDATE cohort SET name = $2, description = $3, img_url = $4, github_link = $5 WHERE id = $1 ', + [id, req.name, req.description, req.img_url, req.github_link], + ); \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 0602b209..fa75fc9e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -13,28 +13,56 @@ afterAll(() => { }); describe('Cohort', () => { - test('Route /cohort/1 status 200, json header, data.name =G8 ', (done) => { + test('PUT Route /cohort/1 status 200, json header, send data ', (done) => { return request(app) - .get('/api/v1/cohort/1') + .put('/api/v1/cohort/1') + .send({ + name: 'G1', + description: 'Code GazaSkyGeeksAcademy, 1st Cohort', + img_url: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + github_link: 'https://github.com/GSG-G1', + }) .expect(200) .expect('Content-Type', /json/) - .end((err, res) => { + .end(async (err, res) => { if (err) return done(err); - const { data } = res.body; - expect(data.name).toBe('G8'); + const { message } = res.body; + const { rows } = await connection.query( + 'SELECT * from cohort WHERE id = 1', + ); + expect(message).toBe('Changed Succefully'); + expect(rows).toHaveLength(1); + expect(rows[0]).toEqual({ + id: 1, + name: 'G1', + description: 'Code GazaSkyGeeksAcademy, 1st Cohort', + img_url: + 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + github_link: 'https://github.com/GSG-G1', + }); done(); }); }); - test('Route /cohort/10 status 404, json header, data.message = "Sorry There\'s no cohort for this id" ', (done) => { + test('PUT Route /cohort/4 status 404, json header, send data ', (done) => { return request(app) - .get('/api/v1/cohort/10') + .put('/api/v1/cohort/4') + .send({ + name: 'G4', + description: 'Code GazaSkyGeeksAcademy, 4th Cohort', + img_url: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + github_link: 'https://github.com/GSG-G4', + }) .expect(404) .expect('Content-Type', /json/) - .end((err, res) => { + .end(async (err, res) => { if (err) return done(err); const { message } = res.body; - expect(message).toBe("Sorry There's no cohort for this id"); + const { rows } = await connection.query( + 'SELECT * from cohort WHERE id = 4', + ); + expect(message).toBe("Sorry There's no cohort for this id to change"); + expect(rows).toHaveLength(0); done(); }); }); From 03f4a78a70123ee99bf58b0b8583e777b2c51617 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Tue, 31 Mar 2020 02:03:14 +0300 Subject: [PATCH 02/14] Add new line at the end of putSpecificCohort.js file --- server/database/queries/cohort/putSpecificCohort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/database/queries/cohort/putSpecificCohort.js b/server/database/queries/cohort/putSpecificCohort.js index f526f49e..d62011e8 100644 --- a/server/database/queries/cohort/putSpecificCohort.js +++ b/server/database/queries/cohort/putSpecificCohort.js @@ -4,4 +4,4 @@ exports.putSpecificCohort = (id, req) => connection.query( 'UPDATE cohort SET name = $2, description = $3, img_url = $4, github_link = $5 WHERE id = $1 ', [id, req.name, req.description, req.img_url, req.github_link], - ); \ No newline at end of file + ); From f9ff73003025db7bd1d0c4705110098cc3efe2b3 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Tue, 31 Mar 2020 02:13:26 +0300 Subject: [PATCH 03/14] define sent data as a const --- test/index.test.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index fa75fc9e..517d1834 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -13,15 +13,17 @@ afterAll(() => { }); describe('Cohort', () => { + const data = { + name: 'G1', + description: 'Code GazaSkyGeeksAcademy, 1st Cohort', + img_url: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + github_link: 'https://github.com/GSG-G1', + }; + test('PUT Route /cohort/1 status 200, json header, send data ', (done) => { return request(app) .put('/api/v1/cohort/1') - .send({ - name: 'G1', - description: 'Code GazaSkyGeeksAcademy, 1st Cohort', - img_url: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', - github_link: 'https://github.com/GSG-G1', - }) + .send(data) .expect(200) .expect('Content-Type', /json/) .end(async (err, res) => { @@ -47,12 +49,7 @@ describe('Cohort', () => { test('PUT Route /cohort/4 status 404, json header, send data ', (done) => { return request(app) .put('/api/v1/cohort/4') - .send({ - name: 'G4', - description: 'Code GazaSkyGeeksAcademy, 4th Cohort', - img_url: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', - github_link: 'https://github.com/GSG-G4', - }) + .send(data) .expect(404) .expect('Content-Type', /json/) .end(async (err, res) => { From 10a98715eb08e21863bb43e08ab70e34afa66ccb Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Tue, 31 Mar 2020 11:45:02 +0300 Subject: [PATCH 04/14] Add data validation --- server/controllers/index.js | 3 ++- server/validation/index.js | 5 +++++ server/validation/validateEditCohort.js | 27 +++++++++++++++++++++++++ test/index.test.js | 23 +++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 server/validation/validateEditCohort.js diff --git a/server/controllers/index.js b/server/controllers/index.js index 20f1a4ab..3e97afa5 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -2,7 +2,8 @@ const router = require('express').Router(); const { cohort: { putSpecificCohort }, } = require('./routes/admin'); +const { validateEditCohort } = require('../validation/index'); -router.put('/cohort/:cohortid', putSpecificCohort); +router.put('/cohort/:cohortid', validateEditCohort, putSpecificCohort); module.exports = router; diff --git a/server/validation/index.js b/server/validation/index.js index e69de29b..6c256cae 100644 --- a/server/validation/index.js +++ b/server/validation/index.js @@ -0,0 +1,5 @@ +const validateEditCohort = require('./validateEditCohort'); + +module.exports = { + validateEditCohort, +}; diff --git a/server/validation/validateEditCohort.js b/server/validation/validateEditCohort.js new file mode 100644 index 00000000..3c5d3fcf --- /dev/null +++ b/server/validation/validateEditCohort.js @@ -0,0 +1,27 @@ +const yup = require('yup'); + +const validateEditCohort = (req, res, next) => { + const schema = yup.object({ + name: yup.string().required(), + description: yup.string().required(), + img_url: yup.string().url().required(), + github_link: yup.string().url().required(), + }); + const projectData = { + name: req.body.name, + description: req.body.description, + img_url: req.body.img_url, + github_link: req.body.github_link, + }; + + schema + .validate(projectData) + .then(() => { + next(); + }) + .catch((err) => { + res.status(400).json({ statusCode: 400, message: err.message }); + }); +}; + +module.exports = validateEditCohort; diff --git a/test/index.test.js b/test/index.test.js index 517d1834..9a4a8fd4 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -19,6 +19,12 @@ describe('Cohort', () => { img_url: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', github_link: 'https://github.com/GSG-G1', }; + const wrongData = { + name: 'G2', + description: 'Code GazaSkyGeeksAcademy, 2nd Cohort', + img_url: 'This is cohort Image', + github_link: 'https://github.com/GSG-G1', + }; test('PUT Route /cohort/1 status 200, json header, send data ', (done) => { return request(app) @@ -63,4 +69,21 @@ describe('Cohort', () => { done(); }); }); + + test('PUT Route /cohort/1 status 400, json header, send wrong data and test the received message', (done) => { + return request(app) + .put('/api/v1/cohort/1') + .send(wrongData) + .expect(400) + .expect('Content-Type', /json/) + .end(async (err, res) => { + if (err) return done(err); + const { message } = res.body; + const { rows } = await connection.query( + 'SELECT * from cohort WHERE id = 1', + ); + expect(message).toBe('img_url must be a valid URL'); + done(); + }); + }); }); From 9e1ff323bb64d4e71fb9f2bb05a09945c5c6f0f4 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Tue, 31 Mar 2020 11:58:34 +0300 Subject: [PATCH 05/14] Edit beforeAll/afterAll function --- test/index.test.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 9a4a8fd4..edf35abf 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('Cohort', () => { const data = { From 29a7f8757f369baa2ee3eb8095d3431030840ee5 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Tue, 31 Mar 2020 13:50:17 +0300 Subject: [PATCH 06/14] destructure validation + change /cohort route into /cohorts --- server/controllers/index.js | 6 ++---- .../cohort/{putSpecificCohort.js => editCohort.js} | 6 ++---- server/controllers/routes/admin/cohort/index.js | 4 ++-- server/controllers/routes/admin/index.js | 4 ++-- server/database/queries/index.js | 4 ++-- server/validation/validateEditCohort.js | 8 +------- test/index.test.js | 12 ++++++------ 7 files changed, 17 insertions(+), 27 deletions(-) rename server/controllers/routes/admin/cohort/{putSpecificCohort.js => editCohort.js} (72%) diff --git a/server/controllers/index.js b/server/controllers/index.js index 3e97afa5..60177cab 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,9 +1,7 @@ const router = require('express').Router(); -const { - cohort: { putSpecificCohort }, -} = require('./routes/admin'); +const { editCohort } = require('./routes/admin'); const { validateEditCohort } = require('../validation/index'); -router.put('/cohort/:cohortid', validateEditCohort, putSpecificCohort); +router.put('/cohorts/:cohortid', validateEditCohort, editCohort); module.exports = router; diff --git a/server/controllers/routes/admin/cohort/putSpecificCohort.js b/server/controllers/routes/admin/cohort/editCohort.js similarity index 72% rename from server/controllers/routes/admin/cohort/putSpecificCohort.js rename to server/controllers/routes/admin/cohort/editCohort.js index 416c2569..ab916b28 100644 --- a/server/controllers/routes/admin/cohort/putSpecificCohort.js +++ b/server/controllers/routes/admin/cohort/editCohort.js @@ -1,8 +1,6 @@ -const { - cohort: { putSpecificCohort }, -} = require('../../../../database/queries'); +const { putSpecificCohort } = require('../../../../database/queries'); -exports.putSpecificCohort = async (req, res, next) => { +exports.editCohort = async (req, res, next) => { try { const result = await putSpecificCohort(req.params.cohortid, req.body); if (result.rowCount === 1) { diff --git a/server/controllers/routes/admin/cohort/index.js b/server/controllers/routes/admin/cohort/index.js index 7bfd23f3..34170ecf 100644 --- a/server/controllers/routes/admin/cohort/index.js +++ b/server/controllers/routes/admin/cohort/index.js @@ -1,3 +1,3 @@ -const { putSpecificCohort } = require('./putSpecificCohort'); +const { editCohort } = require('./editCohort'); -module.exports = { putSpecificCohort }; +module.exports = { editCohort }; diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index 366d6a19..c19bb1a1 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -1,3 +1,3 @@ -const cohort = require('./cohort'); +const { editCohort } = require('./cohort'); -module.exports = { cohort }; +module.exports = { editCohort }; diff --git a/server/database/queries/index.js b/server/database/queries/index.js index 366d6a19..4a6e3f6d 100644 --- a/server/database/queries/index.js +++ b/server/database/queries/index.js @@ -1,3 +1,3 @@ -const cohort = require('./cohort'); +const { putSpecificCohort } = require('./cohort'); -module.exports = { cohort }; +module.exports = { putSpecificCohort }; diff --git a/server/validation/validateEditCohort.js b/server/validation/validateEditCohort.js index 3c5d3fcf..30982997 100644 --- a/server/validation/validateEditCohort.js +++ b/server/validation/validateEditCohort.js @@ -7,15 +7,9 @@ const validateEditCohort = (req, res, next) => { img_url: yup.string().url().required(), github_link: yup.string().url().required(), }); - const projectData = { - name: req.body.name, - description: req.body.description, - img_url: req.body.img_url, - github_link: req.body.github_link, - }; schema - .validate(projectData) + .validate(req.body) .then(() => { next(); }) diff --git a/test/index.test.js b/test/index.test.js index edf35abf..34d0df70 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -22,9 +22,9 @@ describe('Cohort', () => { github_link: 'https://github.com/GSG-G1', }; - test('PUT Route /cohort/1 status 200, json header, send data ', (done) => { + test('PUT Route /cohorts/1 status 200, json header, send data ', (done) => { return request(app) - .put('/api/v1/cohort/1') + .put('/api/v1/cohorts/1') .send(data) .expect(200) .expect('Content-Type', /json/) @@ -48,9 +48,9 @@ describe('Cohort', () => { }); }); - test('PUT Route /cohort/4 status 404, json header, send data ', (done) => { + test('PUT Route /cohorts/4 status 404, json header, send data ', (done) => { return request(app) - .put('/api/v1/cohort/4') + .put('/api/v1/cohorts/4') .send(data) .expect(404) .expect('Content-Type', /json/) @@ -66,9 +66,9 @@ describe('Cohort', () => { }); }); - test('PUT Route /cohort/1 status 400, json header, send wrong data and test the received message', (done) => { + test('PUT Route /cohorts/1 status 400, json header, send wrong data and test the received message', (done) => { return request(app) - .put('/api/v1/cohort/1') + .put('/api/v1/cohorts/1') .send(wrongData) .expect(400) .expect('Content-Type', /json/) From f9a833bb2c55f9f72aa67efef5f7dfcf47baa750 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Tue, 31 Mar 2020 13:54:34 +0300 Subject: [PATCH 07/14] Change keys name to camlCase --- server/database/queries/cohort/putSpecificCohort.js | 2 +- server/validation/validateEditCohort.js | 4 ++-- test/index.test.js | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/server/database/queries/cohort/putSpecificCohort.js b/server/database/queries/cohort/putSpecificCohort.js index d62011e8..249ca569 100644 --- a/server/database/queries/cohort/putSpecificCohort.js +++ b/server/database/queries/cohort/putSpecificCohort.js @@ -3,5 +3,5 @@ const connection = require('../../config/connection'); exports.putSpecificCohort = (id, req) => connection.query( 'UPDATE cohort SET name = $2, description = $3, img_url = $4, github_link = $5 WHERE id = $1 ', - [id, req.name, req.description, req.img_url, req.github_link], + [id, req.name, req.description, req.imgUrl, req.githubLink], ); diff --git a/server/validation/validateEditCohort.js b/server/validation/validateEditCohort.js index 30982997..e3d22fa1 100644 --- a/server/validation/validateEditCohort.js +++ b/server/validation/validateEditCohort.js @@ -4,8 +4,8 @@ const validateEditCohort = (req, res, next) => { const schema = yup.object({ name: yup.string().required(), description: yup.string().required(), - img_url: yup.string().url().required(), - github_link: yup.string().url().required(), + imgUrl: yup.string().url().required(), + githubLink: yup.string().url().required(), }); schema diff --git a/test/index.test.js b/test/index.test.js index 34d0df70..a9b84803 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -12,14 +12,14 @@ describe('Cohort', () => { const data = { name: 'G1', description: 'Code GazaSkyGeeksAcademy, 1st Cohort', - img_url: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', - github_link: 'https://github.com/GSG-G1', + imgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + githubLink: 'https://github.com/GSG-G1', }; const wrongData = { name: 'G2', description: 'Code GazaSkyGeeksAcademy, 2nd Cohort', - img_url: 'This is cohort Image', - github_link: 'https://github.com/GSG-G1', + imgUrl: 'This is cohort Image', + githubLink: 'https://github.com/GSG-G1', }; test('PUT Route /cohorts/1 status 200, json header, send data ', (done) => { @@ -78,7 +78,7 @@ describe('Cohort', () => { const { rows } = await connection.query( 'SELECT * from cohort WHERE id = 1', ); - expect(message).toBe('img_url must be a valid URL'); + expect(message).toBe('imgUrl must be a valid URL'); done(); }); }); From 84eff3ce254d1d5c1d22960f7ab165738986ad46 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Tue, 31 Mar 2020 14:32:47 +0300 Subject: [PATCH 08/14] Edit validation schema --- server/controllers/index.js | 3 +-- .../routes/admin/cohort/editCohort.js | 7 +++++-- server/validation/editCohort.js | 8 +++++++ server/validation/index.js | 6 ++---- server/validation/validateEditCohort.js | 21 ------------------- 5 files changed, 16 insertions(+), 29 deletions(-) create mode 100644 server/validation/editCohort.js delete mode 100644 server/validation/validateEditCohort.js diff --git a/server/controllers/index.js b/server/controllers/index.js index 60177cab..a0294f20 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,7 +1,6 @@ const router = require('express').Router(); const { editCohort } = require('./routes/admin'); -const { validateEditCohort } = require('../validation/index'); -router.put('/cohorts/:cohortid', validateEditCohort, editCohort); +router.put('/cohorts/:cohortid', editCohort); module.exports = router; diff --git a/server/controllers/routes/admin/cohort/editCohort.js b/server/controllers/routes/admin/cohort/editCohort.js index ab916b28..264dcdf5 100644 --- a/server/controllers/routes/admin/cohort/editCohort.js +++ b/server/controllers/routes/admin/cohort/editCohort.js @@ -1,7 +1,10 @@ const { putSpecificCohort } = require('../../../../database/queries'); +const { editCohortSchema } = require('../../../../validation/index'); -exports.editCohort = async (req, res, next) => { +exports.editCohort = async (req, res) => { try { + await editCohortSchema.validate(req.body); + const result = await putSpecificCohort(req.params.cohortid, req.body); if (result.rowCount === 1) { res.json({ statusCode: 200, message: 'Changed Succefully' }); @@ -12,6 +15,6 @@ exports.editCohort = async (req, res, next) => { }); } } catch (err) { - next(err); + res.status(400).json({ statusCode: 400, message: err.message }); } }; diff --git a/server/validation/editCohort.js b/server/validation/editCohort.js new file mode 100644 index 00000000..a14ce9b1 --- /dev/null +++ b/server/validation/editCohort.js @@ -0,0 +1,8 @@ +const yup = require('yup'); + +exports.editCohortSchema = yup.object({ + name: yup.string().required(), + description: yup.string().required(), + imgUrl: yup.string().url().required(), + githubLink: yup.string().url().required(), +}); diff --git a/server/validation/index.js b/server/validation/index.js index 6c256cae..64142511 100644 --- a/server/validation/index.js +++ b/server/validation/index.js @@ -1,5 +1,3 @@ -const validateEditCohort = require('./validateEditCohort'); +const { editCohortSchema } = require('./editCohort'); -module.exports = { - validateEditCohort, -}; +module.exports = { editCohortSchema }; diff --git a/server/validation/validateEditCohort.js b/server/validation/validateEditCohort.js deleted file mode 100644 index e3d22fa1..00000000 --- a/server/validation/validateEditCohort.js +++ /dev/null @@ -1,21 +0,0 @@ -const yup = require('yup'); - -const validateEditCohort = (req, res, next) => { - const schema = yup.object({ - name: yup.string().required(), - description: yup.string().required(), - imgUrl: yup.string().url().required(), - githubLink: yup.string().url().required(), - }); - - schema - .validate(req.body) - .then(() => { - next(); - }) - .catch((err) => { - res.status(400).json({ statusCode: 400, message: err.message }); - }); -}; - -module.exports = validateEditCohort; From 457389aaa733819a4c18d3328971a6fca75e312b Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Wed, 1 Apr 2020 03:18:18 +0300 Subject: [PATCH 09/14] Handle error + destructure query function --- server/controllers/index.js | 2 +- .../controllers/routes/admin/cohort/editCohort.js | 14 +++++++++----- .../database/queries/cohort/putSpecificCohort.js | 10 ++++++---- server/validation/editCohort.js | 9 +++++---- test/index.test.js | 6 ++---- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/server/controllers/index.js b/server/controllers/index.js index a0294f20..a0c189ec 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,6 +1,6 @@ const router = require('express').Router(); const { editCohort } = require('./routes/admin'); -router.put('/cohorts/:cohortid', editCohort); +router.put('/cohorts/:cohortId', editCohort); module.exports = router; diff --git a/server/controllers/routes/admin/cohort/editCohort.js b/server/controllers/routes/admin/cohort/editCohort.js index 264dcdf5..1156602c 100644 --- a/server/controllers/routes/admin/cohort/editCohort.js +++ b/server/controllers/routes/admin/cohort/editCohort.js @@ -1,11 +1,11 @@ const { putSpecificCohort } = require('../../../../database/queries'); const { editCohortSchema } = require('../../../../validation/index'); -exports.editCohort = async (req, res) => { +exports.editCohort = async (req, res, next) => { try { - await editCohortSchema.validate(req.body); - - const result = await putSpecificCohort(req.params.cohortid, req.body); + req.body.cohortId = req.params.cohortId; + await editCohortSchema.validate(req.body, { abortEarly: false }); + const result = await putSpecificCohort(req.body); if (result.rowCount === 1) { res.json({ statusCode: 200, message: 'Changed Succefully' }); } else { @@ -15,6 +15,10 @@ exports.editCohort = async (req, res) => { }); } } catch (err) { - res.status(400).json({ statusCode: 400, message: err.message }); + if (err.errors) { + res.status(400).json({ statusCode: 400, message: err.errors }); + } else { + next(err); + } } }; diff --git a/server/database/queries/cohort/putSpecificCohort.js b/server/database/queries/cohort/putSpecificCohort.js index 249ca569..d6567e97 100644 --- a/server/database/queries/cohort/putSpecificCohort.js +++ b/server/database/queries/cohort/putSpecificCohort.js @@ -1,7 +1,9 @@ const connection = require('../../config/connection'); -exports.putSpecificCohort = (id, req) => - connection.query( - 'UPDATE cohort SET name = $2, description = $3, img_url = $4, github_link = $5 WHERE id = $1 ', - [id, req.name, req.description, req.imgUrl, req.githubLink], +exports.putSpecificCohort = (req) => { + const { name, description, imgUrl, githubLink, cohortId } = req; + return connection.query( + 'UPDATE cohort SET name = $1, description = $2, img_url = $3, github_link = $4 WHERE id = $5 ', + [name, description, imgUrl, githubLink, cohortId], ); +}; diff --git a/server/validation/editCohort.js b/server/validation/editCohort.js index a14ce9b1..c0321e30 100644 --- a/server/validation/editCohort.js +++ b/server/validation/editCohort.js @@ -1,8 +1,9 @@ const yup = require('yup'); exports.editCohortSchema = yup.object({ - name: yup.string().required(), - description: yup.string().required(), - imgUrl: yup.string().url().required(), - githubLink: yup.string().url().required(), + cohortId: yup.number().required(), + name: yup.string(), + description: yup.string(), + imgUrl: yup.string().url(), + githubLink: yup.string().url(), }); diff --git a/test/index.test.js b/test/index.test.js index a9b84803..9f4f258c 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -75,10 +75,8 @@ describe('Cohort', () => { .end(async (err, res) => { if (err) return done(err); const { message } = res.body; - const { rows } = await connection.query( - 'SELECT * from cohort WHERE id = 1', - ); - expect(message).toBe('imgUrl must be a valid URL'); + await connection.query('SELECT * from cohort WHERE id = 1'); + expect(message[0]).toBe('imgUrl must be a valid URL'); done(); }); }); From 17db212482b7f4f7366882a33457dfcd9677fd27 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Wed, 1 Apr 2020 19:02:29 +0300 Subject: [PATCH 10/14] Move getSpecific Cohort into user route --- server/controllers/index.js | 2 -- server/controllers/routes/user/cohort/getCohortsData.js | 2 +- server/controllers/routes/user/cohort/index.js | 7 +++++-- server/controllers/routes/user/index.js | 2 ++ 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/server/controllers/index.js b/server/controllers/index.js index ba77f55a..f83e52ef 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,9 +1,7 @@ const router = require('express').Router(); const user = require('./routes/user'); const admin = require('./routes/admin'); -const { getSpecificCohort } = require('./routes/user/cohort'); -router.get('/cohorts/:cohortid', getSpecificCohort); router.use(user); router.use(admin); diff --git a/server/controllers/routes/user/cohort/getCohortsData.js b/server/controllers/routes/user/cohort/getCohortsData.js index 6f49917d..f6b83c81 100644 --- a/server/controllers/routes/user/cohort/getCohortsData.js +++ b/server/controllers/routes/user/cohort/getCohortsData.js @@ -8,4 +8,4 @@ const getCohortsData = async (req, res, next) => { next(err); } }; -module.exports = { getCohortsData }; +module.exports = getCohortsData; diff --git a/server/controllers/routes/user/cohort/index.js b/server/controllers/routes/user/cohort/index.js index a7833d7f..31168f17 100644 --- a/server/controllers/routes/user/cohort/index.js +++ b/server/controllers/routes/user/cohort/index.js @@ -1,4 +1,7 @@ -const { getCohortsData } = require('./getCohortsData'); +const getCohortsData = require('./getCohortsData'); const getSpecificCohort = require('./getSpecificCohort'); -module.exports = { getCohortsData, getSpecificCohort }; +module.exports = { + getCohortsData, + getSpecificCohort, +}; diff --git a/server/controllers/routes/user/index.js b/server/controllers/routes/user/index.js index 4090798d..cf64a99f 100644 --- a/server/controllers/routes/user/index.js +++ b/server/controllers/routes/user/index.js @@ -1,7 +1,9 @@ const router = require('express').Router(); const { getCohortsData } = require('./cohort'); +const { getSpecificCohort } = require('./cohort'); router.get('/cohorts', getCohortsData); +router.get('/cohorts/:cohortid', getSpecificCohort); module.exports = router; From 6cd155c28be2f8a1753d1ce976f12448d4ac7155 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Wed, 1 Apr 2020 19:07:47 +0300 Subject: [PATCH 11/14] Move app.use(compression()) to the top of the app.js file and edit 'Edit project schema' --- server/app.js | 2 +- server/validation/editCohort.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/app.js b/server/app.js index feeac997..c7a73a26 100644 --- a/server/app.js +++ b/server/app.js @@ -8,12 +8,12 @@ const { serverError } = require('./controllers/middlewares/errorHandle'); const app = express(); +app.use(compression()); app.disable('x-powered-by'); app.set('port', process.env.PORT || 5000); app.use(express.json()); app.use(express.urlencoded({ extended: false })); -app.use(compression()); app.use('/api/v1/', controller); app.use(express.static(join(__dirname, '..', 'client', 'build'))); diff --git a/server/validation/editCohort.js b/server/validation/editCohort.js index c0321e30..16cb7376 100644 --- a/server/validation/editCohort.js +++ b/server/validation/editCohort.js @@ -2,8 +2,8 @@ const yup = require('yup'); exports.editCohortSchema = yup.object({ cohortId: yup.number().required(), - name: yup.string(), - description: yup.string(), - imgUrl: yup.string().url(), - githubLink: yup.string().url(), + name: yup.string().required(), + description: yup.string().required(), + imgUrl: yup.string().url().required(), + githubLink: yup.string().url().required(), }); From f9a46ad91d9017b638172d492f19a078b03db106 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Wed, 1 Apr 2020 19:11:34 +0300 Subject: [PATCH 12/14] remove commented line --- test/index.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/index.test.js b/test/index.test.js index d5d1f8f9..e66c135e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -4,7 +4,6 @@ const dbBuild = require('../server/database/config/build'); const app = require('../server/app'); -// beforeAll(() => dbBuild()); beforeEach(() => dbBuild()); afterAll(() => connection.end()); From 98d40d06860777ae15fdd0f013e6c5f8a70e208e Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Wed, 1 Apr 2020 21:26:19 +0300 Subject: [PATCH 13/14] destructure cohortID and sent data --- server/controllers/routes/admin/cohort/editCohort.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/controllers/routes/admin/cohort/editCohort.js b/server/controllers/routes/admin/cohort/editCohort.js index 32f37644..c18c4026 100644 --- a/server/controllers/routes/admin/cohort/editCohort.js +++ b/server/controllers/routes/admin/cohort/editCohort.js @@ -3,9 +3,10 @@ const { editCohortSchema } = require('../../../../validation/index'); const editCohort = async (req, res, next) => { try { - req.body.cohortId = req.params.cohortId; - await editCohortSchema.validate(req.body, { abortEarly: false }); - const result = await putSpecificCohort(req.body); + const { cohortId } = req.params; + const data = { ...req.body, cohortId }; + await editCohortSchema.validate(data, { abortEarly: false }); + const result = await putSpecificCohort(data); if (result.rowCount === 1) { res.json({ statusCode: 200, message: 'Changed Succefully' }); } else { From ff5e87185da5257f27fbcb3bf9b568a5cf682144 Mon Sep 17 00:00:00 2001 From: mu7ammadabed Date: Wed, 1 Apr 2020 22:29:15 +0300 Subject: [PATCH 14/14] Edit destructure --- server/controllers/routes/admin/cohort/editCohort.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/server/controllers/routes/admin/cohort/editCohort.js b/server/controllers/routes/admin/cohort/editCohort.js index c18c4026..fe922e51 100644 --- a/server/controllers/routes/admin/cohort/editCohort.js +++ b/server/controllers/routes/admin/cohort/editCohort.js @@ -4,8 +4,12 @@ const { editCohortSchema } = require('../../../../validation/index'); const editCohort = async (req, res, next) => { try { const { cohortId } = req.params; - const data = { ...req.body, cohortId }; - await editCohortSchema.validate(data, { abortEarly: false }); + const data = await editCohortSchema.validate( + { ...req.body, cohortId }, + { + abortEarly: false, + }, + ); const result = await putSpecificCohort(data); if (result.rowCount === 1) { res.json({ statusCode: 200, message: 'Changed Succefully' });