Add PUT /users/:id to update a user - #115
Conversation
Adds store.updateUser(id, { name, email }) alongside the other data
helpers. It looks the user up with the existing getUserById, updates
name and email in place, and returns undefined when no user has that
id so callers can branch on a falsy result the same way they do for
getUserById.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019LQYyxNEbgzmiBWqaq8o9f
Wires the update-a-user endpoint into the users resource. It rejects a missing name or email with 400 (same check as POST /users), coerces the id param to a number to match the store's strict id comparison, returns 404 when store.updateUser finds no such user, and responds with the updated user on success. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019LQYyxNEbgzmiBWqaq8o9f
Explains the approved plan and the one edit made before approving it, the model choice, the three-commit split, and what the review of the live endpoint caught. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019LQYyxNEbgzmiBWqaq8o9f
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Your implementation fully satisfies all task requirements. The PUT /users/:id endpoint correctly validates name/email presence (returning 400), returns 404 for non-existent users, and delegates all data access through db/store.js with the new updateUser helper. The one documented design decision — accepting empty strings as valid values to stay consistent with the existing POST /users behavior — is intentional and well-reasoned, not a blocker. All 9 tests pass, npm run lint is clean, and the three logical commits (helper → route → docs) follow a clean dependency order. Well done on the thorough self-review and for catching the non-numeric id edge case before opening the PR.
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
What changed
Adds the "update a user" endpoint to the users resource.
routes/users.js— newPUT /users/:id. Validates thatnameandemailare both present (400 if not), coerces the:idparam to a number, updates the user via the store, returns 404 when no user has that id, and responds200with the updated user.db/store.js— newupdateUser(id, { name, email })helper. Looks the user up with the existinggetUserById, updates the fields in place, and returnsundefinedwhen there's no match so callers branch on a falsy result the same way they do forgetUserById.NOTES.md— write-up of the plan, model choice, commit split, and review.Three commits: store helper, then the route that calls it, then the notes.
Why
The endpoint was specified but not implemented, so
PUTrequests fell through to Express's default handler and always returned 404. The change follows the existing patterns in the file: the// VERB /path — descriptioncomment,return res.status(N).json({ error })for failures, error strings reused verbatim from thePOSTandGEThandlers, and all data access going throughdb/store.jsrather than touching theusersarray directly.server.jsneeded no changes — the users router is already mounted there.Validation is deliberately the same truthiness check as
POST /users— nothing stricter — so the two routes on the same resource stay consistent. A whitespace-only or non-string value is not rejected; if that's wanted it should be added to both routes together.What a reviewer should test
npm test(all 9 green) andnpm run lint(clean). Then against a running server (npm run dev):PUT /users/1with{"name":"Ada L.","email":"ada@new.com"}→200and the updated user in the body. A follow-upGET /users/1shows the change persisted.PUT /users/9999with a valid body →404 {"error":"User not found"}.PUT /users/abcwith a valid body →404, no crash (Number("abc")isNaN, which never matches an id).PUT /users/1with{"name":"Only a name"}→400 {"error":"name and email are required"}.PUT /users/1with no body →400.PUT /users/1with[1,2,3]→400, no crash.PUT /users/1with{"name":"a","email":"b","id":999}→200, id stays1.🤖 Generated with Claude Code