Skip to content

Commit 82c5c57

Browse files
authored
Merge pull request #1 from robertomumo/add-update-user-endpoint
Add PUT /users/:id endpoint to update a user
2 parents 19e97af + 87e65bd commit 82c5c57

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

NOTES.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# NOTES
2+
3+
## The plan
4+
5+
I planned this in Claude Code's plan mode before writing any code. The plan was to add `store.updateUser(id, { name, email })` to `db/store.js` following the existing `getUserById`/`createUser` style, then add a `PUT /:id` handler in `routes/users.js` that validates `name`/`email` are present (400, matching `POST /users`'s validation), looks up the user via the store (404 if missing, matching `GET /users/:id`), and returns the updated user (200) otherwise. I approved the plan as written — no edits were needed, since the two existing routes already gave clear patterns to follow for validation and not-found handling.
6+
7+
## Model choice
8+
9+
I used Claude Sonnet 5 for the whole task — planning, implementation, and review. The change was small and the codebase tiny (a two-route Express resource with no ambiguity in requirements), so there was no need for a heavier model; Sonnet 5 handled reading the existing patterns and applying them consistently without issue.
10+
11+
## Commit split
12+
13+
Two commits: the first adds the `updateUser` store function and the `PUT /:id` route together, since they're one indivisible logical change — the endpoint doesn't work without the store function, and the store function has no other caller. The second commit is a small follow-up from self-review (see below), kept separate so the "what review caught" fix is visible on its own rather than folded into the original diff.
14+
15+
## What review caught
16+
17+
I ran a self-review of the diff before opening the PR. It flagged one real issue: `updateUser` reimplemented the same `users.find((user) => user.id === id)` lookup that `getUserById` already does, instead of calling it. Not a correctness bug, but a duplication that would let the two lookups silently drift if the storage strategy ever changed. I fixed it by having `updateUser` call `getUserById(id)` directly. The review otherwise confirmed the validation order, the not-found path, and the in-place mutation of the found user were all consistent with the rest of the resource and had no regressions.

db/store.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@ function createUser({ name, email }) {
2424
return user;
2525
}
2626

27-
module.exports = { getAllUsers, getUserById, createUser };
27+
function updateUser(id, { name, email }) {
28+
const user = getUserById(id);
29+
30+
if (!user) {
31+
return undefined;
32+
}
33+
34+
user.name = name;
35+
user.email = email;
36+
return user;
37+
}
38+
39+
module.exports = { getAllUsers, getUserById, createUser, updateUser };

routes/users.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,22 @@ router.post("/", (req, res) => {
3232
res.status(201).json(user);
3333
});
3434

35+
// PUT /users/:id — update an existing user; name and email are required
36+
router.put("/:id", (req, res) => {
37+
const id = Number(req.params.id);
38+
const { name, email } = req.body;
39+
40+
if (!name || !email) {
41+
return res.status(400).json({ error: "name and email are required" });
42+
}
43+
44+
const user = store.updateUser(id, { name, email });
45+
46+
if (!user) {
47+
return res.status(404).json({ error: "User not found" });
48+
}
49+
50+
res.json(user);
51+
});
52+
3553
module.exports = router;

0 commit comments

Comments
 (0)