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/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/admin/cohort/editCohort.js b/server/controllers/routes/admin/cohort/editCohort.js new file mode 100644 index 00000000..fe922e51 --- /dev/null +++ b/server/controllers/routes/admin/cohort/editCohort.js @@ -0,0 +1,31 @@ +const { putSpecificCohort } = require('../../../../database/queries'); +const { editCohortSchema } = require('../../../../validation/index'); + +const editCohort = async (req, res, next) => { + try { + const { cohortId } = req.params; + 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' }); + } else { + res.status(404).json({ + statusCode: 404, + message: "Sorry There's no cohort for this id to change", + }); + } + } catch (err) { + if (err.errors) { + res.status(400).json({ statusCode: 400, message: err.errors }); + } else { + next(err); + } + } +}; + +module.exports = editCohort; diff --git a/server/controllers/routes/admin/cohort/index.js b/server/controllers/routes/admin/cohort/index.js index 697fa6e2..c123f946 100644 --- a/server/controllers/routes/admin/cohort/index.js +++ b/server/controllers/routes/admin/cohort/index.js @@ -1,3 +1,7 @@ +const editCohort = require('./editCohort'); const deleteCohort = require('./deleteCohort'); -module.exports = { deleteCohort }; +module.exports = { + deleteCohort, + editCohort, +}; diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index 097370b4..ce6c70f2 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -1,4 +1,6 @@ const router = require('express').Router(); + +const { editCohort } = require('./cohort'); const { deleteCohort } = require('./cohort'); const { addProject, editProject } = require('./project'); @@ -11,9 +13,7 @@ router .get((req, res, next) => { next(new Error('not implemented')); }) - .put((req, res, next) => { - next(new Error('not implemented')); - }) + .put(editCohort) .post((req, res, next) => { next(new Error('not implemented')); }) 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; diff --git a/server/database/queries/cohort/index.js b/server/database/queries/cohort/index.js index 235b21cc..f846dbdf 100644 --- a/server/database/queries/cohort/index.js +++ b/server/database/queries/cohort/index.js @@ -1,11 +1,11 @@ +const putSpecificCohort = require('./putSpecificCohort'); const getCohorts = require('./getCohorts'); - const getCohortQuery = require('./getSpecificCohort'); - const deleteCohortQuery = require('./deleteCohort'); module.exports = { getCohorts, deleteCohortQuery, + putSpecificCohort, getCohortQuery, }; diff --git a/server/database/queries/cohort/putSpecificCohort.js b/server/database/queries/cohort/putSpecificCohort.js new file mode 100644 index 00000000..26bb930d --- /dev/null +++ b/server/database/queries/cohort/putSpecificCohort.js @@ -0,0 +1,11 @@ +const connection = require('../../config/connection'); + +const 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], + ); +}; + +module.exports = putSpecificCohort; diff --git a/server/database/queries/index.js b/server/database/queries/index.js index b07dd77c..665cbad3 100644 --- a/server/database/queries/index.js +++ b/server/database/queries/index.js @@ -1,3 +1,4 @@ +const { putSpecificCohort } = require('./cohort'); const { getCohorts } = require('./cohort'); const { getCohortQuery } = require('./cohort'); const { deleteCohortQuery } = require('./cohort'); @@ -6,6 +7,7 @@ const { addProjectQuery, editProjectQuery } = require('./project'); module.exports = { getCohorts, deleteCohortQuery, + putSpecificCohort, getCohortQuery, addProjectQuery, editProjectQuery, diff --git a/server/validation/editCohort.js b/server/validation/editCohort.js new file mode 100644 index 00000000..16cb7376 --- /dev/null +++ b/server/validation/editCohort.js @@ -0,0 +1,9 @@ +const yup = require('yup'); + +exports.editCohortSchema = yup.object({ + cohortId: yup.number().required(), + 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 672f7cac..33081cde 100644 --- a/server/validation/index.js +++ b/server/validation/index.js @@ -1,5 +1,8 @@ +const { editCohortSchema } = require('./editCohort'); + const projectSchema = require('./projectSchema'); module.exports = { projectSchema, + editCohortSchema, }; diff --git a/test/index.test.js b/test/index.test.js index ec1c2e0a..c4083381 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -51,6 +51,100 @@ describe('Get Specific Cohort', () => { }); }); +describe('Post Cohort', () => { + const data = { + name: 'G1', + description: 'Code GazaSkyGeeksAcademy, 1st Cohort', + 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', + imgUrl: 'This is cohort Image', + githubLink: 'https://github.com/GSG-G1', + }; + + test('PUT Route /cohorts/1 status 200, json header, send data ', (done) => { + return request(app) + .put('/api/v1/cohorts/1') + .send(data) + .expect(200) + .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('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('PUT Route /cohorts/4 status 404, json header, send data ', (done) => { + return request(app) + .put('/api/v1/cohorts/4') + .send(data) + .expect(404) + .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 = 4', + ); + expect(message).toBe("Sorry There's no cohort for this id to change"); + expect(rows).toHaveLength(0); + 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/cohorts/1') + .send(wrongData) + .expect(400) + .expect('Content-Type', /json/) + .end(async (err, res) => { + if (err) return done(err); + const { message } = res.body; + await connection.query('SELECT * from cohort WHERE id = 1'); + expect(message[0]).toBe('imgUrl must be a valid URL'); + done(); + }); + }); +}); + +describe('Admin, (/cohorts/:cohortId)', () => { + 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(async (err, res) => { + const { message } = res.body.data; + if (err) return done(err); + const { rows } = await connection.query( + 'SELECT * from cohort WHERE id = 1', + ); + expect(rows).toHaveLength(0); + expect(message).toBe('Cohort deleted successfully'); + done(); + }); + }); +}); + + describe('Admin, Post Project', () => { test('Route /projects status 200, json header, data.message = Cohort Added successfully ', (done) => { const reqData = {