Skip to content

Commit 1b57b33

Browse files
habinapclaude
andcommitted
Add PUT /users/:id endpoint to update a user
Validates required fields, returns 404 for unknown users instead of crashing, and goes through a new db/store.js updateUser helper to stay consistent with the existing GET/POST handlers. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0145ufxzBfh7NnfMRL4yYMvp
1 parent 19e97af commit 1b57b33

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

NOTES.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Notes — Update a User endpoint
2+
3+
**Plan:** Add `PUT /users/:id` by following the two patterns already in
4+
`routes/users.js` — the id lookup + 404 handling from `GET /users/:id`, and
5+
the required-field validation from `POST /users` — and route the actual
6+
mutation through a new `updateUser(id, { name, email })` helper in
7+
`db/store.js`, so the route file never touches the `users` array directly,
8+
same as the existing handlers. I didn't edit anything before approving.
9+
10+
**Model:** I chose the Sonnet model, for its solid balance between heavy thinking and speed.
11+
12+
**Commits:** One commit for the whole change (store helper + route +
13+
NOTES.md). The store helper and the route aren't independently useful —
14+
`updateUser` has no other caller, and the route can't do anything without
15+
it — so splitting them into separate commits would just create a
16+
mid-history state where the code doesn't do anything new yet. NOTES.md rides
17+
along in the same commit since it documents that same change rather than a
18+
separate concern.
19+
20+
**Review:** Confirmed as already fine: the 404 path doesn't crash on a
21+
non-numeric id — `Number("abc")` is `NaN`, `getUserById` doesn't match, and
22+
`updateUser` correctly returns `null` — because `updateUser` reuses
23+
`getUserById` instead of re-implementing the search. Lint (`npx eslint`) was
24+
clean and the full suite (`npm test`, 9 tests including the grading tests in
25+
`tests/update-user.test.js`) passes with no regressions to the existing
26+
`GET`/`POST` tests. Nothing needed fixing.

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 null;
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)