-
Notifications
You must be signed in to change notification settings - Fork 8
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
Sciper checking #206
Sciper checking #206
Conversation
Pull Request Test Coverage Report for Build 3452171632Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
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 little PR. I've added a few suggestions for improvement.
web/backend/src/Server.ts
Outdated
// The sciper has to contain 6 numbers | ||
if (sciper < 999999 && sciper > 100000) { | ||
// call https://search-api.epfl.ch/api/ldap?q=228271 if the answer | ||
// empty then sciper invalid | ||
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`).then((response) => { | ||
if (response.data.length === 0) { | ||
res.status(400).send('Unknown sciper'); | ||
} else { | ||
usersDB.put(sciper, role).catch((error) => { | ||
res.status(500).send('Failed to add role'); | ||
console.log(error); | ||
}); | ||
} | ||
}); | ||
} else { | ||
res.status(400).send('sciper length incorrect'); | ||
} |
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.
In general, you can reduce indentation by using guard clauses and making the code more readable.
Just a suggestion:
// The sciper has to contain 6 numbers | |
if (sciper < 999999 && sciper > 100000) { | |
// call https://search-api.epfl.ch/api/ldap?q=228271 if the answer | |
// empty then sciper invalid | |
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`).then((response) => { | |
if (response.data.length === 0) { | |
res.status(400).send('Unknown sciper'); | |
} else { | |
usersDB.put(sciper, role).catch((error) => { | |
res.status(500).send('Failed to add role'); | |
console.log(error); | |
}); | |
} | |
}); | |
} else { | |
res.status(400).send('sciper length incorrect'); | |
} | |
// The sciper has to contain 6 numbers | |
if (sciper > 999999 || sciper < 100000) { | |
res.status(400).send('sciper length incorrect'); | |
return; | |
} | |
// call https://search-api.epfl.ch/api/ldap?q=228271 | |
// if the answer is empty then the sciper is invalid | |
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`).then((response) => { | |
if (response.data.length === 0) { | |
res.status(400).send('Unknown sciper'); | |
} else { | |
usersDB.put(sciper, role).catch((error) => { | |
res.status(500).send('Failed to add role'); | |
console.log(error); | |
}); | |
} | |
}); |
web/backend/src/Server.ts
Outdated
if (sciper < 999999 && sciper > 100000) { | ||
// call https://search-api.epfl.ch/api/ldap?q=228271 if the answer | ||
// empty then sciper invalid | ||
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`).then((response) => { |
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.
what if the get
fails ? it seems you don't catch that case.
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.
Yes I missed this one, I just added a catch in a new commit
web/backend/src/Server.ts
Outdated
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`).then((response) => { | ||
if (response.data.length === 0) { | ||
res.status(400).send('Unknown sciper'); | ||
} else { | ||
usersDB.put(sciper, role).catch((error) => { | ||
res.status(500).send('Failed to add role'); | ||
console.log(error); | ||
}); | ||
} | ||
}); |
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.
For you information, you can also concatenate promises, by returning a promise from within the body of a then
.
This is sometimes more readable, and you can group all the errors in one catch.
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`).then((response) => { | |
if (response.data.length === 0) { | |
res.status(400).send('Unknown sciper'); | |
} else { | |
usersDB.put(sciper, role).catch((error) => { | |
res.status(500).send('Failed to add role'); | |
console.log(error); | |
}); | |
} | |
}); | |
axios.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`) | |
.then((response) => { | |
if (response.data.length === 0) { | |
res.status(400).send('Unknown sciper'); | |
return; | |
} | |
return usersDB.put(sciper, role); | |
}) | |
.catch((error) => { | |
res.status(500).send('Failed to add role'); | |
console.log(error); | |
});; |
web/backend/src/Server.ts
Outdated
console.log(error); | ||
}); | ||
|
||
res.status(200).send('Role added'); |
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.
this will also execute in the case of the catch
above, don't you want to put in a then
(before the catch
) ?
Kudos, SonarCloud Quality Gate passed! |
No description provided.