-
-
Save kasramp/20c5d97d16b8ca3cfac6fed37997818d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = await storage.getUserById(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) { | |
context.response.status = 200; | |
context.response.body = (await storage.updateUser(id, firstName, lastName, ageInt))[0]; | |
} 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