diff --git a/package.json b/package.json index 7668c4d0..14ce6ad2 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "dev": "NODE_ENV=development nodemon server/index.js", "project-setup": "npm install && cd client && npm install", "heroku-postbuild": "cd client && npm install && npm run build", - "test": "NODE_ENV=test jest --coverage", + "test": "NODE_ENV=test jest --runInBand --coverage", "kill": "fuser -k 5000/tcp", "lint": "eslint server/", "lint-fix": "eslint server/ --fix", diff --git a/server/controllers/routes/admin/index.js b/server/controllers/routes/admin/index.js index 6739e7e7..097370b4 100644 --- a/server/controllers/routes/admin/index.js +++ b/server/controllers/routes/admin/index.js @@ -1,6 +1,6 @@ const router = require('express').Router(); const { deleteCohort } = require('./cohort'); -const { addProject } = require('./project'); +const { addProject, editProject } = require('./project'); router .route('/cohorts/:cohortId') @@ -19,6 +19,7 @@ router }) .delete(deleteCohort); +router.put('/projects/:projectId', editProject); router.post('/projects', addProject); module.exports = router; diff --git a/server/controllers/routes/admin/project/editProject.js b/server/controllers/routes/admin/project/editProject.js new file mode 100644 index 00000000..98b6ab88 --- /dev/null +++ b/server/controllers/routes/admin/project/editProject.js @@ -0,0 +1,27 @@ +const { editProjectQuery } = require('../../../../database/queries'); +const { projectSchema } = require('../../../../validation'); + +const editProject = async (req, res, next) => { + try { + await projectSchema.validate( + { ...req.body, projectId: req.params.projectId }, + { abortEarly: false }, + ); + await editProjectQuery(req.body); + res.json({ + StatusCode: 200, + data: { message: 'project updated successfully' }, + }); + } catch (err) { + if (err.errors) { + res.json({ + StatusCode: 400, + data: { message: err.errors }, + }); + } else { + next(err); + } + } +}; + +module.exports = editProject; diff --git a/server/controllers/routes/admin/project/index.js b/server/controllers/routes/admin/project/index.js index be72b8c8..9eddbc62 100644 --- a/server/controllers/routes/admin/project/index.js +++ b/server/controllers/routes/admin/project/index.js @@ -1,5 +1,7 @@ const addProject = require('./addProject'); +const editProject = require('./editProject'); module.exports = { addProject, + editProject, }; diff --git a/server/database/config/fakeData.sql b/server/database/config/fakeData.sql index 6114ab3d..9a4a8310 100644 --- a/server/database/config/fakeData.sql +++ b/server/database/config/fakeData.sql @@ -14,6 +14,22 @@ INSERT INTO project (name , description , img_url , github_link , website_link , 'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA', 'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3', 'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal project',1), +('ca-wiki', +'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page', +'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA', +'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3', +'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal project',2), +('ca-wiki', +'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page', +'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA', +'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3', +'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal project',1), +('ca-wiki', +'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page', +'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA', +'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3', +'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal project',2), + ('events-booker', 'This app to help GSG organization in organizing the registration for the events that they do. So When they announced for an event, it will be known who would like to attend and they can ensure if this person attended or not. By this way, the GSG can have information about the people who attended and they can use it in the future as they want.', 'https://d2slcw3kip6qmk.cloudfront.net/marketing/blog/2017Q2/project-planning-header@2x.png', diff --git a/server/database/queries/index.js b/server/database/queries/index.js index 77ce4965..1d8e7c42 100644 --- a/server/database/queries/index.js +++ b/server/database/queries/index.js @@ -1,9 +1,10 @@ const { getCohortQuery } = require('./cohort'); const { deleteCohortQuery } = require('./cohort'); -const { addProjectQuery } = require('./project'); +const { addProjectQuery, editProjectQuery } = require('./project'); module.exports = { deleteCohortQuery, getCohortQuery, addProjectQuery, + editProjectQuery, }; diff --git a/server/database/queries/project/editProject.js b/server/database/queries/project/editProject.js new file mode 100644 index 00000000..03370db4 --- /dev/null +++ b/server/database/queries/project/editProject.js @@ -0,0 +1,28 @@ +const connection = require('../../config/connection'); + +const editProjectQuery = (data) => { + const { + name, + description, + imgUrl, + githubLink, + websiteLink, + projectType, + cohortId, + projectId, + } = data; + connection.query( + 'UPDATE project SET name=$1, description=$2, img_url=$3, github_link=$4, website_link=$5, project_type=$6, cohort_id=$7 WHERE id = $8', + [ + name, + description, + imgUrl, + githubLink, + websiteLink, + projectType, + cohortId, + projectId, + ], + ); +}; +module.exports = editProjectQuery; diff --git a/server/database/queries/project/index.js b/server/database/queries/project/index.js index 27dce37a..127b02bf 100644 --- a/server/database/queries/project/index.js +++ b/server/database/queries/project/index.js @@ -1,5 +1,7 @@ const addProjectQuery = require('./addProject'); +const editProjectQuery = require('./editProject'); module.exports = { addProjectQuery, + editProjectQuery, }; diff --git a/server/validation/projectSchema.js b/server/validation/projectSchema.js index a837873a..37edb611 100644 --- a/server/validation/projectSchema.js +++ b/server/validation/projectSchema.js @@ -8,6 +8,7 @@ const projectSchema = yup.object({ websiteLink: yup.string().url().required(), projectType: yup.mixed().oneOf(['internal', 'remotely']), cohortId: yup.number().integer().positive().required(), + projectId: yup.number().integer().positive(), }); module.exports = projectSchema; diff --git a/test/index.test.js b/test/index.test.js index 839f2a01..2f6fac20 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -56,7 +56,7 @@ describe('Admin, Project', () => { const { message } = res.body.data; if (err) return done(err); const { rows } = await connection.query( - 'SELECT * from project WHERE id = 3', + 'SELECT * from project WHERE id = 6', ); expect(rows[0].name).toBe('Mohmmedzw851@'); expect(message).toBe('Project Added successfully'); @@ -83,3 +83,28 @@ describe('Admin, (/cohorts/:cohortId)', () => { }); }); }); + +describe('Admin, (/projects/:projectId)', () => { + test('PUT Route /projects/1 status 200, json header, message:Cohort updated successfully', (done) => { + const testData = { + name: 'Mooooot', + description: 'project test', + imgUrl: 'https://github.com/GSG-G1', + githubLink: 'https://github.com/GSG-G1', + websiteLink: 'https://github.com/GSG-G1', + projectType: 'remotely', + cohortId: '2', + }; + return request(app) + .put('/api/v1/projects/5') + .send(testData) + .expect(200) + .expect('Content-Type', /json/) + .end(async (err, res) => { + if (err) return done(err); + const { message } = res.body.data; + expect(message).toBe('project updated successfully'); + done(); + }); + }); +});