-
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
Conversation
server/controllers/index.js
Outdated
@@ -1,7 +1,9 @@ | |||
const router = require('express').Router(); | |||
const { | |||
cohort: { putSpecificCohort }, |
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.
as I comment in PR #41
I suggest making destructure of getSpecificCohort in
/database/queries/index
as :const { getSpecificCohort } = require('./cohort'); module.exports = { getSpecificCohort };as you make in
server/database/queries/cohort/index.js
file,and here in this file you can make require to getSpecificCohort as
so we get more benefit from
/database/queries/index
file.
server/controllers/index.js
Outdated
router.get('/', (req, res) => { | ||
res.send('<h1>CA WIKI</h1>'); | ||
}); | ||
router.put('/cohort/:cohortid', validateEditCohort, putSpecificCohort); |
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.
based on issue #5 please change the route to be
/cohorts/:cohortId
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], |
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.
I suggest to make destructure for req data as:
const { name, description, img_url, github_link} = req;
connection.query(
'UPDATE cohort SET name = $2, description = $3, img_url = $4, github_link = $5 WHERE id = $1 ',
[id, name, description, img_url, github_link],
img_url: yup.string().url().required(), | ||
github_link: yup.string().url().required(), | ||
}); | ||
const projectData = { |
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.
I suggest making destructure for req.body
const schema = yup.object({ | ||
name: yup.string().required(), | ||
description: yup.string().required(), | ||
img_url: yup.string().url().required(), |
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.
I suggest using camel case to name the key here
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.
Nice Work
|
||
exports.putSpecificCohort = (id, req) => | ||
connection.query( | ||
'UPDATE cohort SET name = $2, description = $3, img_url = $4, github_link = $5 WHERE id = $1 ', |
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 I added a comment to another PR which might be helpful here too:
https://github.com/GSG-G8/ca-wiki/pull/47/files#r400924612
It describes how an ideal design for PUT requests is to have them handle requests that don't contain every single argument, so frontends can choose to PUT just a new name.
|
||
exports.editCohort = async (req, res) => { | ||
try { | ||
await editCohortSchema.validate(req.body); |
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.
If you test this editCohortSchema.validate(req.body)
by default it's aborts all the validations steps once it hit the first validation error , to get all error you would just add the optional second parameter {abortEarly: false }
to the call to editCohortSchema.validate(req.body)
which look like as I suggested :
await editCohortSchema.validate(req.body); | |
await editCohortSchema.validate(req.body, {abortEarly: false }); | |
} catch (err) { | ||
res.status(400).json({ statusCode: 400, message: err.message }); | ||
} |
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.
Here there is two types of error Server error, Client Error
so you need to handle each of them
} catch (err) { | |
res.status(400).json({ statusCode: 400, message: err.message }); | |
} | |
} catch (err) { | |
if(err.errors){ | |
res.status(400).json({ statusCode: 400, message: err.message }); | |
}else { | |
next(err) | |
} | |
} |
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], |
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.
Destructuring !!
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide me with a resource for it ?
I saw what you send to us yesterday and I didn't really get it, I tried so much and searched a lot without reaching any place.
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.
Here's an example of the query syntax: https://stackoverflow.com/a/19358694
You can use a question mark in the query and then pass an object in the arguments array to match with the question mark. In your code, I think that means it could look like this:
return connection.query('UPDATE cohort SET ? WHERE id = ?', [req, req.cohortId]);
Once you do that, you should be able PUT any number / combination of the arguments (for example, only name
or only img_url
or both name
and description
together)
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.
res.json({ statusCode: 200, message: 'Changed Succefully' }); | ||
} else { | ||
res.status(404).json({ | ||
statusCode: 404, |
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.
} | ||
} 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 comment
The reason will be displayed to describe this comment to others. Learn more.
@Mu7ammadAbed Here also you could remove statusCode from the json body.
98d40d0
I created
PUT
method to edit an existed cohort data and I added 2 tests one for an attempt to edit exist cohort and the other non-exist cohortRelates #23