-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
23 put cohortid #49
23 put cohortid #49
Changes from 16 commits
2095e7b
03f4a78
f9ff730
10a9871
9e1ff32
29a7f87
f9a833b
84eff3c
457389a
1862e6e
5ea99cb
bee0f4b
98b875b
17db212
6cd155c
f9a46ad
98d40d0
ff5e871
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { putSpecificCohort } = require('../../../../database/queries'); | ||
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); | ||
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 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Mu7ammadAbed Here also you could remove statusCode from the json body. |
||
} else { | ||
next(err); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = editCohort; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
const editCohort = require('./editCohort'); | ||
const deleteCohort = require('./deleteCohort'); | ||
|
||
module.exports = { deleteCohort }; | ||
module.exports = { | ||
deleteCohort, | ||
editCohort, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
const { getCohortsData } = require('./getCohortsData'); | ||
const getCohortsData = require('./getCohortsData'); | ||
const getSpecificCohort = require('./getSpecificCohort'); | ||
|
||
module.exports = { getCohortsData, getSpecificCohort }; | ||
module.exports = { | ||
getCohortsData, | ||
getSpecificCohort, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Mu7ammadAbed My comment here might be helpful your endpoint - you can design PUT endpoints to accept any number of arguments, instead of always expecting every argument to be provided by the frontend. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide me with a resource for it ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's an example of the query syntax: https://stackoverflow.com/a/19358694 Once you do that, you should be able PUT any number / combination of the arguments (for example, only Hopefully that works - and if not, sorry for sending you on the wrong path. This change isn't incredibly important for this project, but maybe it would help for a future project. |
||
[name, description, imgUrl, githubLink, cohortId], | ||
); | ||
}; | ||
|
||
module.exports = putSpecificCohort; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
const { editCohortSchema } = require('./editCohort'); | ||
|
||
const projectSchema = require('./projectSchema'); | ||
|
||
module.exports = { | ||
projectSchema, | ||
editCohortSchema, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mu7ammadAbed You could remove statusCode from the json body because you already called
status(404)
on the response.