Skip to content

Instantly share code, notes, and snippets.

@kasramp

kasramp/index.js Secret

Created May 21, 2020 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasramp/2de72bb8d62473f6757e7b5833e5a4a5 to your computer and use it in GitHub Desktop.
Save kasramp/2de72bb8d62473f6757e7b5833e5a4a5 to your computer and use it in GitHub Desktop.
router.put("/v1/users/:id", async (context) => {
if (!context.params || !context.params.id ||
isNaN(parseInt(context.params.id) || !context.request.hasBody)) {
context.response.status = 400;
context.response.body = { error: "Invalid request" };
} else {
const result = getById(context.params.id);
if (result.length < 1) {
context.response.status = 404;
context.response.body = { error: `User ${context.params.id} not found` };
} else {
const id = context.params.id;
const { firstName, lastName, age } = (await context.request.body()).value
const ageInt = parseInt(age);
if (firstName && lastName && ageInt) {
dummyUsers.splice(id - 1, 1, { id: id, firstName: firstName, lastName: lastName, age: ageInt });
context.response.status = 200;
context.response.body = dummyUsers[id-1];
} else {
context.response.status = 400;
context.response.body = { error: "Invalid payload's provided" };
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment