Add PUT /users/:id to update a user - #91
Open
terray-fb wants to merge 3 commits into
Open
Conversation
Replaces a user's name and email in place and returns the updated record, or undefined when no user has that id - the same "missing means undefined" contract getUserById already uses, so callers can handle not-found without a second lookup.
Validates that name and email are both non-empty strings and returns 400 with a clear message when they aren't, returns 404 when no user has that id, and otherwise responds 200 with the updated user. All data access goes through db/store.js, matching the other routes. Turns the tests in tests/update-user.test.js green.
Covers the four write-up questions from the project brief: what the approved plan contained and what I changed in it, which models I used for planning versus implementation, why the work is split the way it is, and what the pre-push review flagged or confirmed.
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
PUT /users/:idto update an existing user's name and email.updateUser(id, { name, email })helper todb/store.jsfor the data access, following the same pattern as the existinggetUserById/createUserhelpers.NOTES.mdcovering the approved plan, model choice, commit split, and review.Why
The users resource had no update action.
tests/update-user.test.jsalready shipped with the tests for this endpoint, starting red; this PR turns them green without modifying that file.How it works
nameandemailare both non-empty strings (rejects missing, empty, whitespace-only, and non-string values) and returns400with a clear message if not.db/store.js; returns404with{ "error": "User not found" }if no user has that id (including non-numeric ids, which becomeNaNand simply don't match).200with the updated user.db/store.js— the route never touches the in-memory array directly.400regardless of whether the id exists.Deliberate design choices
PUT /users/:idrequires bothnameandemailon every request and replaces both fields — it does not merge in a partial body. This matchesPUTsemantics and is what the tests require (a missing field is always a400, never a partial update).PUT /users/:idalone. The existingPOST /usersroute keeps its original!name || !emailcheck unchanged — no behavior change to code that already worked.What a reviewer should test
PUT /users/1with{"name":"...","email":"..."}→200with the updated user.GET /users/1after the update shows the change persisted through the store, not just in the response.PUT /users/9999(unknown numeric id) andPUT /users/abc(non-numeric id) with a valid body →404, server stays up.name: 123) →400.Test evidence
npm test— 9/9 passing (the 3update-usertests plus the 2NOTES.mdtests, and the existing suite untouched).npm run lint— clean.tests/update-user.test.jswas not modified.