Skip to content

Commit e184bf4

Browse files
authored
Merge pull request #1 from rolex17/add-update-user-endpoint
Add PUT endpoint to update existing users
2 parents 19e97af + 0c6fcd1 commit e184bf4

3 files changed

Lines changed: 55 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+
# Ship a Change - Implementation Notes
2+
3+
## Plan
4+
The plan was to add a PUT endpoint for updating users. The implementation required two changes:
5+
1. Add an `updateUser` function to `db/store.js` that finds a user by ID and updates their name and email
6+
2. Add a PUT route to `routes/users.js` that validates input (both name and email required), handles the not-found case (404), and returns the updated user (200)
7+
8+
I did not need to edit the plan — it was clear and complete.
9+
10+
## Model Choice
11+
I used Claude Haiku (haiku-4.5-20251001) because this is a small, straightforward feature with clear requirements from the tests. The task involves following an existing pattern (similar to POST endpoint) and the logic is simple enough that a faster model is efficient here.
12+
13+
## Commit Split
14+
I made one commit containing both file changes:
15+
- `db/store.js`: Added the `updateUser` function
16+
- `routes/users.js`: Added the PUT endpoint with validation
17+
18+
This is one logical change (adding the update user feature) and fits together naturally — the route needs the store function, so splitting them would create a temporarily broken state.
19+
20+
## Review Findings
21+
The implementation correctly handles all three test cases:
22+
- ✅ Updates an existing user and returns 200 with the updated data
23+
- ✅ Returns 404 when the user doesn't exist
24+
- ✅ Returns 400 when required fields are missing
25+
26+
The validation logic mirrors the POST endpoint, ensuring consistency. No issues found.

db/store.js

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

27-
module.exports = { getAllUsers, getUserById, createUser };
27+
function updateUser(id, { name, email }) {
28+
const user = users.find((u) => u.id === id);
29+
if (!user) {
30+
return null;
31+
}
32+
user.name = name;
33+
user.email = email;
34+
return user;
35+
}
36+
37+
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 a user; name and email are required
36+
router.put("/:id", (req, res) => {
37+
const { name, email } = req.body;
38+
39+
if (!name || !email) {
40+
return res.status(400).json({ error: "name and email are required" });
41+
}
42+
43+
const id = Number(req.params.id);
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)