Add PUT /users/:id endpoint for updating a user - #114
Open
MishaLehotskyi wants to merge 3 commits into
Open
Conversation
Updates an existing user's name and email in place and returns the updated user. Returns undefined when no user has that id so route handlers can tell "not found" apart from a successful update, matching how getUserById behaves. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Validates that name and email are non-empty strings and answers 400 when either is missing, blank, or the wrong type; answers 404 when no user has that id, including a non-numeric id, instead of crashing. Data access goes through the new store.updateUser helper like the other routes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Covers the plan and the one edit made before approving it, the model choice, why the work is split into these three commits, and what the self-review caught or confirmed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Adds the 'update a user' endpoint to the users resource.
db/store.js— newupdateUser(id, { name, email }). It reusesgetUserById, returnsundefinedwhen no user has that id, and otherwise updates the record and returns it.routes/users.js— newPUT /users/:id. Validates the body, then goes through the store for the lookup and update.200with the updated user on success400 { error: "name and email are required" }whennameoremailis missing, blank, or not a string404 { error: "User not found" }when no user has that idNOTES.md— the write-up the project asks for (plan, model choice, commit split, review).Why
tests/update-user.test.jswas already in the repo and red. This makes it green without touching the tests.Two judgement calls worth a reviewer's attention:
POST /users. The existing create route uses a truthy check, which accepts{ "name": 42 }. The new route uses anisNonEmptyStringhelper instead. I deliberately did not changePOSTto match — that would alter the behaviour of an endpoint this change isn't about. Worth a follow-up.What to test
npm testis green (9/9) andnpm run lintis clean. Beyond the provided tests, I checked these by hand:PUT /users/1with{ "name": "A", "email": "a@e.com" }PUT /users/9999with a valid bodyPUT /users/abcwith a valid bodyNumber("abc")isNaN, misses the lookupPUT /users/1with{ "name": "Only a name" }PUT /users/1with{ "name": " ", "email": "a@e.com" }PUT /users/1with{ "name": 42, "email": "a@e.com" }PUT /users/1with no bodyPUT /users/1with{ "name": "A", "email": "a@e.com", "id": 99, "admin": true }The commits are split by layer (store helper → endpoint → notes), so they read cleanly one at a time.
🤖 Generated with Claude Code