Skip to content

Feat/update user endpoint - #100

Closed
EvheniiChuhai wants to merge 5 commits into
mate-academy:mainfrom
EvheniiChuhai:feat/update-user-endpoint
Closed

Feat/update user endpoint#100
EvheniiChuhai wants to merge 5 commits into
mate-academy:mainfrom
EvheniiChuhai:feat/update-user-endpoint

Conversation

@EvheniiChuhai

Copy link
Copy Markdown

No description provided.

EvheniiChuhai and others added 5 commits August 24, 2026 12:28
Routes go through db/store.js for all data access, so the upcoming
PUT /users/:id route needs a store-level way to write to a user.

updateUser looks the record up by id and overwrites name and email in
place, which keeps the id stable and leaves the array ordering alone. It
returns undefined for an unknown id, mirroring getUserById so callers
can use the same not-found check they already use for reads.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The upcoming PUT /users/:id needs the same "is this field present and
usable" test that POST /users does, and the conventions for this repo
say not to grow a second way of doing the same thing. Pull the check
into an isUsable helper both routes can share.

This tightens POST slightly: the old `!name` accepted a whitespace-only
string and a number, so POST {"name": "   "} stored a blank-looking
name and returned 201. A field that is present but empty is bad input,
so isUsable requires a non-empty string once trimmed. No existing test
depended on the looser behaviour.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The users resource could list, read, and create but not update. Add the
update route, which replaces a user's name and email and returns the
updated record with 200.

PUT is a full replacement here, so both fields are required rather than
optional: sending only a name is a 400, not a partial update. A partial
update would be a PATCH, which nothing asks for yet.

Two failure paths, following the repo's convention that 400 is about the
body and 404 is about the id:

- the body is validated before the store is touched, so a request that
  is bad on both counts reports the body error the caller must fix first
- an id matching no record returns 404 instead of crashing. A
  non-numeric id also lands here, because Number("abc") is NaN and NaN
  matches no record, which makes "no such user" the honest answer.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Self-review turned up an inconsistency introduced with isUsable: it
judges value.trim(), but the routes then handed the raw string to the
store. PUT /users/1 with {"name": "   Ada   "} passed validation and
persisted the padding, so the value that was checked was not the value
that was written.

Trim at both call sites so the store holds what validation approved.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The course project asks for a short write-up alongside the code, and two
of the provided tests check it exists with real content.

Covers the plan and what I changed in it before approving, the model
choice, why the commits are split the way they are, and what the review
pass caught (the trim bug in the previous commit) versus what it
confirmed was already correct.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.

1 participant