Skip to content
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

Merged
merged 18 commits into from
Apr 2, 2020
Merged

23 put cohortid #49

merged 18 commits into from
Apr 2, 2020

Conversation

Mu7ammadAbed
Copy link
Member

@Mu7ammadAbed Mu7ammadAbed commented Mar 30, 2020

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 cohort
Relates #23

@Mu7ammadAbed Mu7ammadAbed added Awaiting Review This PR is ready and waiting for review Backend labels Mar 30, 2020
@@ -1,7 +1,9 @@
const router = require('express').Router();
const {
cohort: { putSpecificCohort },
Copy link
Member

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.

router.get('/', (req, res) => {
res.send('<h1>CA WIKI</h1>');
});
router.put('/cohort/:cohortid', validateEditCohort, putSpecificCohort);
Copy link
Member

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],
Copy link
Member

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 = {
Copy link
Member

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(),
Copy link
Member

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

@MohammedAlghazali MohammedAlghazali added Changes Requested This PR has some mandatory changes to be done in order for it to be accepted and removed Awaiting Review This PR is ready and waiting for review labels Mar 31, 2020
@Mu7ammadAbed Mu7ammadAbed added Awaiting Review This PR is ready and waiting for review and removed Changes Requested This PR has some mandatory changes to be done in order for it to be accepted labels Mar 31, 2020
Copy link
Member

@MohammedAlghazali MohammedAlghazali left a 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 ',

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.

rehabas
rehabas previously approved these changes Mar 31, 2020
AlaaSaadeddin
AlaaSaadeddin previously approved these changes Mar 31, 2020

exports.editCohort = async (req, res) => {
try {
await editCohortSchema.validate(req.body);
Copy link
Collaborator

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 :

Suggested change
await editCohortSchema.validate(req.body);
await editCohortSchema.validate(req.body, {abortEarly: false });

Comment on lines 17 to 19
} catch (err) {
res.status(400).json({ statusCode: 400, message: err.message });
}
Copy link
Collaborator

@ranasobeid95 ranasobeid95 Mar 31, 2020

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

Suggested change
} 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],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Destructuring !!

@ranasobeid95 ranasobeid95 added Changes Requested This PR has some mandatory changes to be done in order for it to be accepted and removed Awaiting Review This PR is ready and waiting for review labels Mar 31, 2020
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 ',

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.

Copy link
Member Author

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.

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,

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 });

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.

AlaaSaadeddin
AlaaSaadeddin previously approved these changes Apr 1, 2020
@MohammedAlghazali MohammedAlghazali merged commit 5b3a966 into master Apr 2, 2020
@MohammedAlghazali MohammedAlghazali deleted the 23-put-cohortid branch April 2, 2020 07:05
@ranasobeid95 ranasobeid95 added Done and removed Awaiting Review This PR is ready and waiting for review labels Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants