From 0813327f14a4bf476fce0aac8bc7ff583af2a929 Mon Sep 17 00:00:00 2001 From: rehabas Date: Tue, 31 Mar 2020 00:45:49 +0300 Subject: [PATCH 01/15] add new cohort relates #21 --- .eslintrc.json | 4 +++- server/controllers/index.js | 5 ++-- .../routes/admin/cohort/addCohort.js | 23 +++++++++++++++++++ .../controllers/routes/admin/cohort/index.js | 5 ++++ server/controllers/routes/admin/index.js | 6 +++++ server/database/queries/cohort/index.js | 3 +++ server/database/queries/cohort/postCohort.js | 9 ++++++++ server/database/queries/index.js | 3 +++ test/index.test.js | 4 +--- 9 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 server/controllers/routes/admin/cohort/addCohort.js create mode 100644 server/database/queries/cohort/postCohort.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..7e044659 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -1,7 +1,6 @@ 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/addCohort.js b/server/controllers/routes/admin/cohort/addCohort.js new file mode 100644 index 00000000..b6c36916 --- /dev/null +++ b/server/controllers/routes/admin/cohort/addCohort.js @@ -0,0 +1,23 @@ +const { + postCohort, +} = require('../../../../database/queries/cohort/postCohort'); + +const addCohort = async (req, res, next) => { + try { + const cohortInfo = req.body; + const result = await postCohort(cohortInfo); + return res.json({ + StatusCode: 201, + data: { + cohortId: result.rows[0].id, + message: 'Cohort added successfully', + }, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + addCohort, +}; diff --git a/server/controllers/routes/admin/cohort/index.js b/server/controllers/routes/admin/cohort/index.js index e69de29b..7f816bb0 100644 --- a/server/controllers/routes/admin/cohort/index.js +++ b/server/controllers/routes/admin/cohort/index.js @@ -0,0 +1,5 @@ +const { addCohort } = require('./addCohort'); + +module.exports = { + addCohort, +}; diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index e69de29b..03e2d6c0 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -0,0 +1,6 @@ +const router = require('express').Router(); +const { addCohort } = require('./cohort'); + +router.route('/cohorts').post(addCohort); + +module.exports = router; diff --git a/server/database/queries/cohort/index.js b/server/database/queries/cohort/index.js index e69de29b..48c9a05d 100644 --- a/server/database/queries/cohort/index.js +++ b/server/database/queries/cohort/index.js @@ -0,0 +1,3 @@ +const { postCohort } = require('./postCohort'); + +module.exports = { postCohort }; diff --git a/server/database/queries/cohort/postCohort.js b/server/database/queries/cohort/postCohort.js new file mode 100644 index 00000000..d0b4ddeb --- /dev/null +++ b/server/database/queries/cohort/postCohort.js @@ -0,0 +1,9 @@ +const connection = require('../../config/connection'); + +exports.postCohort = (reqData) => { + const { cName, cDescription, cImgUrl, cGithub } = reqData; + return connection.query( + 'INSERT INTO cohort (name, description, img_url, github_link) VALUES ($1, $2, $3, $4) RETURNING id;', + [cName, cDescription, cImgUrl, cGithub], + ); +}; diff --git a/server/database/queries/index.js b/server/database/queries/index.js index e69de29b..366d6a19 100644 --- a/server/database/queries/index.js +++ b/server/database/queries/index.js @@ -0,0 +1,3 @@ +const cohort = require('./cohort'); + +module.exports = { cohort }; diff --git a/test/index.test.js b/test/index.test.js index 1c71181d..8b137891 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,3 +1 @@ -test('Initial test', () => { - expect(1).toBe(1); -}); + From f9b023c1ef8fc7b0bf993e255ec2585712cd4213 Mon Sep 17 00:00:00 2001 From: rehabas Date: Tue, 31 Mar 2020 01:15:33 +0300 Subject: [PATCH 02/15] create a test for post cohort relates #21 --- test/index.test.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/index.test.js b/test/index.test.js index 8b137891..f0628c87 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1 +1,35 @@ +const request = require('supertest'); +const connection = require('../server/database/config/connection'); +const dbBuild = require('../server/database/config/build'); +const app = require('../server/app'); + +beforeAll(() => dbBuild()); + +afterAll(() => connection.end()); + +describe('Admin, Cohorts', () => { + test('Route /cohorts status 200, json header, data.message = Cohort added successfully', (done) => { + const reqData = { + cName: 'G6', + cDescription: 'GazaSkyGeeks Code Academy, 6th Cohort', + cImgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + cGithub: 'https://github.com/GSG-G8', + }; + return request(app) + .post('/api/v1/cohorts') + .send(reqData) + .expect(200) + .expect('Content-Type', /json/) + .end(async (err, res) => { + const { data } = res.body; + if (err) return done(err); + const result = await connection.query( + 'SELECT * from cohort WHERE id = 3', + ); + expect(result.rows[0].name).toBe('G6'); + expect(data.message).toBe('Cohort added successfully'); + done(); + }); + }); +}); From 8599802823ebbfec33a91dc8517652bd592b8989 Mon Sep 17 00:00:00 2001 From: rehabas Date: Tue, 31 Mar 2020 03:03:52 +0300 Subject: [PATCH 03/15] destructuring relates #21 --- .../routes/admin/cohort/addCohort.js | 5 +-- server/database/queries/cohort/postCohort.js | 3 +- test/index.test.js | 37 +++++++++---------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/server/controllers/routes/admin/cohort/addCohort.js b/server/controllers/routes/admin/cohort/addCohort.js index b6c36916..499b8caa 100644 --- a/server/controllers/routes/admin/cohort/addCohort.js +++ b/server/controllers/routes/admin/cohort/addCohort.js @@ -4,12 +4,11 @@ const { const addCohort = async (req, res, next) => { try { - const cohortInfo = req.body; - const result = await postCohort(cohortInfo); + const { rows } = await postCohort(req.body); return res.json({ StatusCode: 201, data: { - cohortId: result.rows[0].id, + cohortId: rows[0].id, message: 'Cohort added successfully', }, }); diff --git a/server/database/queries/cohort/postCohort.js b/server/database/queries/cohort/postCohort.js index d0b4ddeb..42e8cef0 100644 --- a/server/database/queries/cohort/postCohort.js +++ b/server/database/queries/cohort/postCohort.js @@ -1,9 +1,8 @@ const connection = require('../../config/connection'); exports.postCohort = (reqData) => { - const { cName, cDescription, cImgUrl, cGithub } = reqData; return connection.query( 'INSERT INTO cohort (name, description, img_url, github_link) VALUES ($1, $2, $3, $4) RETURNING id;', - [cName, cDescription, cImgUrl, cGithub], + [reqData.cName, reqData.cDescription, reqData.cImgUrl, reqData.cGithub], ); }; diff --git a/test/index.test.js b/test/index.test.js index f0628c87..261cafb8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -8,28 +8,25 @@ beforeAll(() => dbBuild()); afterAll(() => connection.end()); -describe('Admin, Cohorts', () => { - test('Route /cohorts status 200, json header, data.message = Cohort added successfully', (done) => { - const reqData = { +test('Post cohort route', (done) => { + request(app) + .post('/api/v1/cohorts') + .send({ cName: 'G6', cDescription: 'GazaSkyGeeks Code Academy, 6th Cohort', cImgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', cGithub: 'https://github.com/GSG-G8', - }; - return request(app) - .post('/api/v1/cohorts') - .send(reqData) - .expect(200) - .expect('Content-Type', /json/) - .end(async (err, res) => { - const { data } = res.body; - if (err) return done(err); - const result = await connection.query( - 'SELECT * from cohort WHERE id = 3', - ); - expect(result.rows[0].name).toBe('G6'); - expect(data.message).toBe('Cohort added successfully'); - done(); - }); - }); + }) + .expect(200) + .expect('Content-Type', /json/) + .end(async (err, res) => { + const { message } = res.body.data; + if (err) return done(err); + const result = await connection.query( + 'SELECT * from cohort WHERE id = 3', + ); + expect(result.rows[0].name).toBe('G6'); + expect(message).toBe('Cohort added successfully'); + done(); + }); }); From d5f1d895c3a2341fd0ad9a66e7e6c5ba0f132a50 Mon Sep 17 00:00:00 2001 From: rehabas Date: Tue, 31 Mar 2020 15:56:38 +0300 Subject: [PATCH 04/15] handle a case of the cohort is already exist relates #21 --- .../routes/admin/cohort/addCohort.js | 18 +++++++++++++++--- server/database/config/build.sql | 2 +- server/database/queries/cohort/postCohort.js | 3 ++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/server/controllers/routes/admin/cohort/addCohort.js b/server/controllers/routes/admin/cohort/addCohort.js index 499b8caa..f3803639 100644 --- a/server/controllers/routes/admin/cohort/addCohort.js +++ b/server/controllers/routes/admin/cohort/addCohort.js @@ -2,10 +2,10 @@ const { postCohort, } = require('../../../../database/queries/cohort/postCohort'); -const addCohort = async (req, res, next) => { +const addCohort = async (req, res) => { try { const { rows } = await postCohort(req.body); - return res.json({ + res.json({ StatusCode: 201, data: { cohortId: rows[0].id, @@ -13,10 +13,22 @@ const addCohort = async (req, res, next) => { }, }); } catch (err) { - next(err); + res.status(400).json({ + statusCode: 400, + data: { + message: 'Cohort already exists', + }, + }); } }; module.exports = { addCohort, }; + +// res.status(409).json({ +// StatusCode: 409, +// data: { +// message: 'Cohort already exists', +// }, +// }); diff --git a/server/database/config/build.sql b/server/database/config/build.sql index 1df90e3e..4b383698 100644 --- a/server/database/config/build.sql +++ b/server/database/config/build.sql @@ -10,7 +10,7 @@ CREATE TABLE admin ( CREATE TABLE cohort ( id SERIAL PRIMARY KEY, - name VARCHAR(100) NOT NULL, + name VARCHAR(100) NOT NULL UNIQUE, description TEXT NOT NULL, img_url TEXT NOT NULL, github_link TEXT NOT NULL diff --git a/server/database/queries/cohort/postCohort.js b/server/database/queries/cohort/postCohort.js index 42e8cef0..397ea517 100644 --- a/server/database/queries/cohort/postCohort.js +++ b/server/database/queries/cohort/postCohort.js @@ -1,8 +1,9 @@ const connection = require('../../config/connection'); exports.postCohort = (reqData) => { + const { name, description, imgUrl, githubLink } = reqData; return connection.query( 'INSERT INTO cohort (name, description, img_url, github_link) VALUES ($1, $2, $3, $4) RETURNING id;', - [reqData.cName, reqData.cDescription, reqData.cImgUrl, reqData.cGithub], + [name, description, imgUrl, githubLink], ); }; From 71948b79599dc64c5d068bf6818562d59bcbf509 Mon Sep 17 00:00:00 2001 From: rehabas Date: Tue, 31 Mar 2020 16:04:11 +0300 Subject: [PATCH 05/15] edit tests relates #21 --- test/index.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 261cafb8..f587fee4 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -12,10 +12,10 @@ test('Post cohort route', (done) => { request(app) .post('/api/v1/cohorts') .send({ - cName: 'G6', - cDescription: 'GazaSkyGeeks Code Academy, 6th Cohort', - cImgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', - cGithub: 'https://github.com/GSG-G8', + name: 'G6', + description: 'GazaSkyGeeks Code Academy, 6th Cohort', + imgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + githubLink: 'https://github.com/GSG-G8', }) .expect(200) .expect('Content-Type', /json/) From 56743bd8fed1521c46ce05f5bbe8cb8c3a6e2fb1 Mon Sep 17 00:00:00 2001 From: rehabas Date: Tue, 31 Mar 2020 17:52:05 +0300 Subject: [PATCH 06/15] validation schema relates #21 --- server/controllers/routes/admin/cohort/addCohort.js | 2 ++ server/validation/addNewCohort.js | 12 ++++++++++++ server/validation/index.js | 5 +++++ 3 files changed, 19 insertions(+) create mode 100644 server/validation/addNewCohort.js diff --git a/server/controllers/routes/admin/cohort/addCohort.js b/server/controllers/routes/admin/cohort/addCohort.js index f3803639..3b9654e8 100644 --- a/server/controllers/routes/admin/cohort/addCohort.js +++ b/server/controllers/routes/admin/cohort/addCohort.js @@ -1,9 +1,11 @@ const { postCohort, } = require('../../../../database/queries/cohort/postCohort'); +const { addNewCohort } = require('../../../../validation/addNewCohort'); const addCohort = async (req, res) => { try { + await addNewCohort.validate(req.body); const { rows } = await postCohort(req.body); res.json({ StatusCode: 201, diff --git a/server/validation/addNewCohort.js b/server/validation/addNewCohort.js new file mode 100644 index 00000000..d694e261 --- /dev/null +++ b/server/validation/addNewCohort.js @@ -0,0 +1,12 @@ +const yup = require('yup'); + +const addNewCohort = yup.object({ + name: yup.string().required(), + description: yup.string().required(), + imgUrl: yup.string().url().required(), + githubLink: yup.string().url().required(), +}); + +module.exports = { + addNewCohort, +}; diff --git a/server/validation/index.js b/server/validation/index.js index e69de29b..5e28ce09 100644 --- a/server/validation/index.js +++ b/server/validation/index.js @@ -0,0 +1,5 @@ +const addNewCohort = require('./addNewCohort'); + +module.exports = { + addNewCohort, +}; From acfff6befe9a9e0f31d4254a7b2d3e149650c161 Mon Sep 17 00:00:00 2001 From: rehabas Date: Tue, 31 Mar 2020 18:18:29 +0300 Subject: [PATCH 07/15] destructuring for test relates #21 --- test/index.test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index f587fee4..3b4de59e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -22,10 +22,12 @@ test('Post cohort route', (done) => { .end(async (err, res) => { 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 = 3', ); - expect(result.rows[0].name).toBe('G6'); + const { name } = rows[0]; + expect(rows).toHaveLength(1); + expect(name).toBe('G6'); expect(message).toBe('Cohort added successfully'); done(); }); From f549ce131407412926d562d880606b804eda2c96 Mon Sep 17 00:00:00 2001 From: rehabas Date: Tue, 31 Mar 2020 18:23:58 +0300 Subject: [PATCH 08/15] edit testing for thie route relates #21 --- test/index.test.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 3b4de59e..d565b1fc 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -25,9 +25,14 @@ test('Post cohort route', (done) => { const { rows } = await connection.query( 'SELECT * from cohort WHERE id = 3', ); - const { name } = rows[0]; expect(rows).toHaveLength(1); - expect(name).toBe('G6'); + expect(rows[0]).toEqual({ + id: 3, + name: 'G6', + description: 'GazaSkyGeeks Code Academy, 6th Cohort', + img_url: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + github_link: 'https://github.com/GSG-G8', + }); expect(message).toBe('Cohort added successfully'); done(); }); From e5c92bcf635be4b24355465a1c8a9cd90410d7f4 Mon Sep 17 00:00:00 2001 From: rehabas Date: Tue, 31 Mar 2020 18:48:16 +0300 Subject: [PATCH 09/15] make testing for adding an existing cohort relates #21 --- test/index.test.js | 81 ++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 27 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index d565b1fc..54ef2e79 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -8,32 +8,59 @@ beforeAll(() => dbBuild()); afterAll(() => connection.end()); -test('Post cohort route', (done) => { - request(app) - .post('/api/v1/cohorts') - .send({ - name: 'G6', - description: 'GazaSkyGeeks Code Academy, 6th Cohort', - imgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', - githubLink: 'https://github.com/GSG-G8', - }) - .expect(200) - .expect('Content-Type', /json/) - .end(async (err, res) => { - const { message } = res.body.data; - if (err) return done(err); - const { rows } = await connection.query( - 'SELECT * from cohort WHERE id = 3', - ); - expect(rows).toHaveLength(1); - expect(rows[0]).toEqual({ - id: 3, - name: 'G6', - description: 'GazaSkyGeeks Code Academy, 6th Cohort', - img_url: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', - github_link: 'https://github.com/GSG-G8', +describe('Cohort', () => { + const data = { + name: 'G6', + description: 'GazaSkyGeeks Code Academy, 6th Cohort', + imgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + githubLink: 'https://github.com/GSG-G8', + }; + const wrongData = { + name: 'G5', + description: 'GazaSkyGeeks Code Academy, 6th Cohort', + imgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + githubLink: 'githun link', + }; + test('POST Route /cohorts status 200, json header, send data ', (done) => { + request(app) + .post('/api/v1/cohorts') + .send(data) + .expect(200) + .expect('Content-Type', /json/) + .end(async (err, res) => { + const { message } = res.body.data; + if (err) return done(err); + const { rows } = await connection.query( + 'SELECT * from cohort WHERE id = 3', + ); + expect(rows).toHaveLength(1); + expect(rows[0]).toEqual({ + id: 3, + name: 'G6', + description: 'GazaSkyGeeks Code Academy, 6th Cohort', + img_url: + 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', + github_link: 'https://github.com/GSG-G8', + }); + expect(message).toBe('Cohort added successfully'); + done(); }); - expect(message).toBe('Cohort added successfully'); - done(); - }); + }); + test('POST Route /cohorts status 400, json header, send data ', (done) => { + request(app) + .post('/api/v1/cohorts') + .send(data) + .expect(400) + .expect('Content-Type', /json/) + .end(async (err, res) => { + const { message } = res.body.data; + if (err) return done(err); + const { rows } = await connection.query( + 'SELECT * from cohort WHERE id = 4', + ); + expect(rows).toHaveLength(0); + expect(message).toBe('Cohort already exists'); + done(); + }); + }); }); From f1c3af23be946051e76021d790d31111565c5532 Mon Sep 17 00:00:00 2001 From: rehabas Date: Tue, 31 Mar 2020 18:49:59 +0300 Subject: [PATCH 10/15] make testing for adding an existing cohort relates #21 --- test/index.test.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 54ef2e79..d3f6d1b4 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -15,12 +15,6 @@ describe('Cohort', () => { imgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', githubLink: 'https://github.com/GSG-G8', }; - const wrongData = { - name: 'G5', - description: 'GazaSkyGeeks Code Academy, 6th Cohort', - imgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', - githubLink: 'githun link', - }; test('POST Route /cohorts status 200, json header, send data ', (done) => { request(app) .post('/api/v1/cohorts') From b51b111c6814664b4e8658add340e581ae56dd7d Mon Sep 17 00:00:00 2001 From: rehabas Date: Wed, 1 Apr 2020 02:37:20 +0300 Subject: [PATCH 11/15] handle validation errors relates #21 --- .../routes/admin/cohort/addCohort.js | 35 +++++++++---------- server/database/queries/cohort/postCohort.js | 2 +- .../{addNewCohort.js => cohortSchema .js} | 4 +-- server/validation/index.js | 4 +-- test/index.test.js | 30 ++++++++++++++-- 5 files changed, 50 insertions(+), 25 deletions(-) rename server/validation/{addNewCohort.js => cohortSchema .js} (81%) diff --git a/server/controllers/routes/admin/cohort/addCohort.js b/server/controllers/routes/admin/cohort/addCohort.js index 3b9654e8..602339f1 100644 --- a/server/controllers/routes/admin/cohort/addCohort.js +++ b/server/controllers/routes/admin/cohort/addCohort.js @@ -1,36 +1,35 @@ const { postCohort, } = require('../../../../database/queries/cohort/postCohort'); -const { addNewCohort } = require('../../../../validation/addNewCohort'); +const { cohortSchema } = require('../../../../validation/cohortSchema '); -const addCohort = async (req, res) => { +const addCohort = async (req, res, next) => { try { - await addNewCohort.validate(req.body); - const { rows } = await postCohort(req.body); + await cohortSchema.validate(req.body, { abortEarly: false }); + const result = await postCohort(req.body); + const { rows } = result; res.json({ StatusCode: 201, data: { - cohortId: rows[0].id, + cohortId: rows[0], message: 'Cohort added successfully', }, }); } catch (err) { - res.status(400).json({ - statusCode: 400, - data: { - message: 'Cohort already exists', - }, - }); + if (err.errors) { + res.status(400).json({ statusCode: 400, data: { message: err.errors } }); + } else { + res.status(409).json({ + statusCode: 409, + data: { + message: 'Cohort already exists', + }, + }); + // next(err); + } } }; module.exports = { addCohort, }; - -// res.status(409).json({ -// StatusCode: 409, -// data: { -// message: 'Cohort already exists', -// }, -// }); diff --git a/server/database/queries/cohort/postCohort.js b/server/database/queries/cohort/postCohort.js index 397ea517..c9054b31 100644 --- a/server/database/queries/cohort/postCohort.js +++ b/server/database/queries/cohort/postCohort.js @@ -3,7 +3,7 @@ const connection = require('../../config/connection'); exports.postCohort = (reqData) => { const { name, description, imgUrl, githubLink } = reqData; return connection.query( - 'INSERT INTO cohort (name, description, img_url, github_link) VALUES ($1, $2, $3, $4) RETURNING id;', + 'INSERT INTO cohort (name, description, img_url, github_link) VALUES ($1, $2, $3, $4) RETURNING *;', [name, description, imgUrl, githubLink], ); }; diff --git a/server/validation/addNewCohort.js b/server/validation/cohortSchema .js similarity index 81% rename from server/validation/addNewCohort.js rename to server/validation/cohortSchema .js index d694e261..15ddaa31 100644 --- a/server/validation/addNewCohort.js +++ b/server/validation/cohortSchema .js @@ -1,6 +1,6 @@ const yup = require('yup'); -const addNewCohort = yup.object({ +const cohortSchema = yup.object({ name: yup.string().required(), description: yup.string().required(), imgUrl: yup.string().url().required(), @@ -8,5 +8,5 @@ const addNewCohort = yup.object({ }); module.exports = { - addNewCohort, + cohortSchema, }; diff --git a/server/validation/index.js b/server/validation/index.js index 5e28ce09..aa843437 100644 --- a/server/validation/index.js +++ b/server/validation/index.js @@ -1,5 +1,5 @@ -const addNewCohort = require('./addNewCohort'); +const cohortSchema = require('./cohortSchema '); module.exports = { - addNewCohort, + cohortSchema, }; diff --git a/test/index.test.js b/test/index.test.js index d3f6d1b4..0ca1455b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -15,6 +15,12 @@ describe('Cohort', () => { imgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', githubLink: 'https://github.com/GSG-G8', }; + const invalidData = { + name: 'G5', + description: 'GazaSkyGeeks Code Academy, 6th Cohort', + imgUrl: 'img url', + githubLink: 'github link', + }; test('POST Route /cohorts status 200, json header, send data ', (done) => { request(app) .post('/api/v1/cohorts') @@ -40,11 +46,11 @@ describe('Cohort', () => { done(); }); }); - test('POST Route /cohorts status 400, json header, send data ', (done) => { + test('POST Route /cohorts status 409, json header, send data ', (done) => { request(app) .post('/api/v1/cohorts') .send(data) - .expect(400) + .expect(409) .expect('Content-Type', /json/) .end(async (err, res) => { const { message } = res.body.data; @@ -57,4 +63,24 @@ describe('Cohort', () => { done(); }); }); + test('POST Route /cohorts status 400, json header, send invalid data ', (done) => { + request(app) + .post('/api/v1/cohorts') + .send(invalidData) + .expect(400) + .expect('Content-Type', /json/) + .end(async (err, res) => { + const { message } = res.body.data; + if (err) return done(err); + const { rows } = await connection.query( + 'SELECT * from cohort WHERE id = 4', + ); + expect(rows).toHaveLength(0); + expect(message).toEqual([ + 'imgUrl must be a valid URL', + 'githubLink must be a valid URL', + ]); + done(); + }); + }); }); From ad2e95785e0681a52cd0cc90aef8e8691f5d7146 Mon Sep 17 00:00:00 2001 From: rehabas Date: Wed, 1 Apr 2020 02:40:35 +0300 Subject: [PATCH 12/15] handle validation errors relates #21 --- server/controllers/routes/admin/cohort/addCohort.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/server/controllers/routes/admin/cohort/addCohort.js b/server/controllers/routes/admin/cohort/addCohort.js index 602339f1..3c20c596 100644 --- a/server/controllers/routes/admin/cohort/addCohort.js +++ b/server/controllers/routes/admin/cohort/addCohort.js @@ -3,11 +3,10 @@ const { } = require('../../../../database/queries/cohort/postCohort'); const { cohortSchema } = require('../../../../validation/cohortSchema '); -const addCohort = async (req, res, next) => { +const addCohort = async (req, res) => { try { await cohortSchema.validate(req.body, { abortEarly: false }); - const result = await postCohort(req.body); - const { rows } = result; + const { rows } = await postCohort(req.body); res.json({ StatusCode: 201, data: { From 1d2e3307bf5128b69aebb46b8e7f4f238a0fe57f Mon Sep 17 00:00:00 2001 From: rehabas Date: Wed, 1 Apr 2020 12:11:42 +0300 Subject: [PATCH 13/15] handle case when cohort already exit relates #21 --- .../routes/admin/cohort/addCohort.js | 13 +++++++------ .../controllers/routes/user/project/index.js | 3 +++ test/index.test.js | 18 +++++++++++++----- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/server/controllers/routes/admin/cohort/addCohort.js b/server/controllers/routes/admin/cohort/addCohort.js index 3c20c596..8870ff4d 100644 --- a/server/controllers/routes/admin/cohort/addCohort.js +++ b/server/controllers/routes/admin/cohort/addCohort.js @@ -3,28 +3,29 @@ const { } = require('../../../../database/queries/cohort/postCohort'); const { cohortSchema } = require('../../../../validation/cohortSchema '); -const addCohort = async (req, res) => { +const addCohort = async (req, res, next) => { try { await cohortSchema.validate(req.body, { abortEarly: false }); const { rows } = await postCohort(req.body); res.json({ StatusCode: 201, data: { - cohortId: rows[0], - message: 'Cohort added successfully', + cohort: rows[0], + message: `Cohort with Key (name)=(${rows[0].name}) added successfully`, }, }); } catch (err) { if (err.errors) { res.status(400).json({ statusCode: 400, data: { message: err.errors } }); - } else { + } else if (err.detail) { res.status(409).json({ statusCode: 409, data: { - message: 'Cohort already exists', + message: err.detail, }, }); - // next(err); + } else { + next(err); } } }; diff --git a/server/controllers/routes/user/project/index.js b/server/controllers/routes/user/project/index.js index e69de29b..543c4fc6 100644 --- a/server/controllers/routes/user/project/index.js +++ b/server/controllers/routes/user/project/index.js @@ -0,0 +1,3 @@ +const { getProjectById } = require('./getProjectById'); + +module.exports = { getProjectById }; diff --git a/test/index.test.js b/test/index.test.js index 0ca1455b..8519e6b9 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -28,8 +28,10 @@ describe('Cohort', () => { .expect(200) .expect('Content-Type', /json/) .end(async (err, res) => { - const { message } = res.body.data; if (err) return done(err); + const { + data: { message }, + } = res.body; const { rows } = await connection.query( 'SELECT * from cohort WHERE id = 3', ); @@ -42,7 +44,9 @@ describe('Cohort', () => { 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4', github_link: 'https://github.com/GSG-G8', }); - expect(message).toBe('Cohort added successfully'); + expect(message).toBe( + `Cohort with Key (name)=(${data.name}) added successfully`, + ); done(); }); }); @@ -53,13 +57,15 @@ describe('Cohort', () => { .expect(409) .expect('Content-Type', /json/) .end(async (err, res) => { - const { message } = res.body.data; if (err) return done(err); + const { + data: { message }, + } = res.body; const { rows } = await connection.query( 'SELECT * from cohort WHERE id = 4', ); expect(rows).toHaveLength(0); - expect(message).toBe('Cohort already exists'); + expect(message).toBe(`Key (name)=(${data.name}) already exists.`); done(); }); }); @@ -70,8 +76,10 @@ describe('Cohort', () => { .expect(400) .expect('Content-Type', /json/) .end(async (err, res) => { - const { message } = res.body.data; if (err) return done(err); + const { + data: { message }, + } = res.body; const { rows } = await connection.query( 'SELECT * from cohort WHERE id = 4', ); From 2de1fa13c34039775edd71fa89e675cfa475535a Mon Sep 17 00:00:00 2001 From: rehabas Date: Wed, 1 Apr 2020 21:59:52 +0300 Subject: [PATCH 14/15] add cohort testing relates #21 --- .../controllers/routes/admin/cohort/addCohort.js | 6 ++---- server/controllers/routes/admin/cohort/index.js | 2 +- server/controllers/routes/admin/index.js | 2 +- server/validation/cohortSchema .js | 4 +--- test/index.test.js | 14 ++++++++------ 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/server/controllers/routes/admin/cohort/addCohort.js b/server/controllers/routes/admin/cohort/addCohort.js index 8870ff4d..ee0c73c2 100644 --- a/server/controllers/routes/admin/cohort/addCohort.js +++ b/server/controllers/routes/admin/cohort/addCohort.js @@ -1,7 +1,7 @@ const { postCohort, } = require('../../../../database/queries/cohort/postCohort'); -const { cohortSchema } = require('../../../../validation/cohortSchema '); +const cohortSchema = require('../../../../validation/cohortSchema '); const addCohort = async (req, res, next) => { try { @@ -30,6 +30,4 @@ const addCohort = async (req, res, next) => { } }; -module.exports = { - addCohort, -}; +module.exports = addCohort; diff --git a/server/controllers/routes/admin/cohort/index.js b/server/controllers/routes/admin/cohort/index.js index 063e54af..773cab78 100644 --- a/server/controllers/routes/admin/cohort/index.js +++ b/server/controllers/routes/admin/cohort/index.js @@ -1,4 +1,4 @@ -const { addCohort } = require('./addCohort'); +const addCohort = require('./addCohort'); const deleteCohort = require('./deleteCohort'); module.exports = { diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index 60d9dd03..c8b942ff 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -2,7 +2,7 @@ const router = require('express').Router(); const { addCohort, deleteCohort } = require('./cohort'); const { addProject } = require('./project'); -router.route('/cohorts').post(addCohort); +router.post('/cohorts', addCohort); router .route('/cohorts/:cohortId') diff --git a/server/validation/cohortSchema .js b/server/validation/cohortSchema .js index 15ddaa31..8417b354 100644 --- a/server/validation/cohortSchema .js +++ b/server/validation/cohortSchema .js @@ -7,6 +7,4 @@ const cohortSchema = yup.object({ githubLink: yup.string().url().required(), }); -module.exports = { - cohortSchema, -}; +module.exports = cohortSchema; diff --git a/test/index.test.js b/test/index.test.js index efb3351c..6fdf0ac7 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -8,7 +8,7 @@ beforeAll(() => dbBuild()); afterAll(() => connection.end()); -describe('Cohort', () => { +describe('Admin, Add cohort', () => { const data = { name: 'G6', description: 'GazaSkyGeeks Code Academy, 6th Cohort', @@ -94,7 +94,9 @@ describe('Cohort', () => { done(); }); }); +}); +describe('Get Specific Cohort', () => { test('Route /cohorts/1 status 200, json header, data.name =G8 ', (done) => { return request(app) .get('/api/v1/cohorts/1') @@ -102,8 +104,8 @@ describe('Cohort', () => { .expect('Content-Type', /json/) .end((err, res) => { if (err) return done(err); - const { data: result } = res.body; - expect(result.name).toBe('G8'); + const { data } = res.body; + expect(data.name).toBe('G8'); done(); }); }); @@ -122,8 +124,8 @@ describe('Cohort', () => { }); }); -describe('Admin, Project', () => { - test('Route /projects status 200, json header, data.message = Project Added successfully ', (done) => { +describe('Admin, Post Project', () => { + test('Route /projects status 200, json header, data.message = Cohort Added successfully ', (done) => { const reqData = { name: 'Mohmmedzw851@', description: 'description', @@ -152,7 +154,7 @@ describe('Admin, Project', () => { }); }); -describe('Admin, (/cohorts/:cohortId)', () => { +describe('Admin, Delete Specific Cohort', () => { test('Route /cohorts/1 status 200, data.message = Cohort deleted successfully ', (done) => { return request(app) .delete('/api/v1/cohorts/1') From d2a0d586b8891984a3b379f7506e05d62fcce879 Mon Sep 17 00:00:00 2001 From: rehabas Date: Thu, 2 Apr 2020 16:36:59 +0300 Subject: [PATCH 15/15] destructre relates #12 --- server/controllers/routes/admin/cohort/addCohort.js | 7 +++---- server/database/queries/cohort/postCohort.js | 4 +++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/server/controllers/routes/admin/cohort/addCohort.js b/server/controllers/routes/admin/cohort/addCohort.js index afd4b771..e7511163 100644 --- a/server/controllers/routes/admin/cohort/addCohort.js +++ b/server/controllers/routes/admin/cohort/addCohort.js @@ -1,17 +1,16 @@ -const { - postCohort, -} = require('../../../../database/queries/cohort/postCohort'); +const { postCohort } = require('../../../../database/queries'); const cohortSchema = require('../../../../validation/cohortSchema '); const addCohort = async (req, res, next) => { try { await cohortSchema.validate(req.body, { abortEarly: false }); const { rows } = await postCohort(req.body); + const { name } = rows[0]; res.status(201).json({ StatusCode: 201, data: { cohort: rows[0], - message: `Cohort with Key (name)=(${rows[0].name}) added successfully`, + message: `Cohort with Key (name)=(${name}) added successfully`, }, }); } catch (err) { diff --git a/server/database/queries/cohort/postCohort.js b/server/database/queries/cohort/postCohort.js index c9054b31..8ca02e94 100644 --- a/server/database/queries/cohort/postCohort.js +++ b/server/database/queries/cohort/postCohort.js @@ -1,9 +1,11 @@ const connection = require('../../config/connection'); -exports.postCohort = (reqData) => { +const postCohort = (reqData) => { const { name, description, imgUrl, githubLink } = reqData; return connection.query( 'INSERT INTO cohort (name, description, img_url, github_link) VALUES ($1, $2, $3, $4) RETURNING *;', [name, description, imgUrl, githubLink], ); }; + +module.exports = postCohort;