Skip to content

Add PUT /users/:id endpoint to update a user - #142

Open
kfhawas wants to merge 2 commits into
mate-academy:mainfrom
kfhawas:add-update-user-endpoint
Open

Add PUT /users/:id endpoint to update a user#142
kfhawas wants to merge 2 commits into
mate-academy:mainfrom
kfhawas:add-update-user-endpoint

Conversation

@kfhawas

@kfhawas kfhawas commented Sep 8, 2026

Copy link
Copy Markdown

What

Adds PUT /users/:id to update an existing user's name and email.

  • db/store.js — new updateUser(id, { name, email }) helper. Reuses getUserById and returns undefined for an unknown id, mirroring how getUserById already signals "not found".
  • routes/users.js — new PUT /:id route. Validates that name and email are present (400 otherwise, same response shape as POST /users), then returns 404 if the store reports no such user, else 200 with the updated user.

Behavior notes

  • Validation runs before the not-found check, so a request that is both malformed and for an unknown id returns 400, not 404. This matches the existing POST /users style rather than inventing a new convention for this route.
  • Only name and email are destructured from the body, so a client cannot overwrite id through the request payload.
  • Non-numeric ids are safe: Number("abc") is NaN, which never matches a real id, so the request falls through to 404.

Testing

npm test — 9/9 passing, including the three cases for this endpoint (successful update, 404 for unknown id, 400 for a missing field).

Commits

  • Add PUT /users/:id endpoint to update a user — the feature (store helper + route together, since the route is broken without the helper).
  • Add NOTES.md write-up deliverable — the task write-up, kept separate from the endpoint logic.

🤖 Generated with Claude Code

kfhawas and others added 2 commits September 8, 2026 17:45
Adds an update-user route alongside the existing users actions. The route
validates that name and email are present (400 otherwise), delegates to a
new store.updateUser helper, and returns 404 when no user has that id.

store.updateUser reuses getUserById and mutates the matched record in
place, returning undefined for an unknown id so the route can branch on
it the same way GET /users/:id does.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019xjAHNy1M6tER5BtovcVb1
NOTES.md is the write-up deliverable for this task: it records the
approved plan, model choice, commit-split rationale, and what the
self-review caught. It's kept as its own commit, separate from the
endpoint code, since it's documentation rather than part of the
feature's logic.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KPzJqMgRQzUCwFcQoGfqyw

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

I am approving your solution. The PUT /users/:id endpoint correctly validates name and email, returns 404 for unknown users, and responds with the updated user on success, matching the existing patterns. The db/store.js updateUser helper reuses getUserById, updates only allowed fields, and returns undefined when the user is not found, aligning with the repository’s conventions. NOTES.md is clear and covers the plan, model choice, commit rationale, and review findings.

Minor suggestions (non-blocking):

  • Consider trimming inputs and optionally adding basic email format checks to prevent accepting whitespace-only or malformed emails.
  • For clarity, you could explicitly handle non-numeric :id (e.g., isNaN) before lookup and decide whether to return 400 or 404 per your API conventions.

Overall, well done—clean, consistent, and aligned with the requirements.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants