Skip to content

Commit 258d29c

Browse files
Add PUT /users/:id update endpoint
Adds updateUser to db/store.js and a PUT /:id route to routes/users.js. Validates required fields (400), handles missing users (404), and returns the updated user on success (200). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 19e97af commit 258d29c

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

NOTES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Model:
2+
I use Sonnet model
3+
4+
# Changes: Add PUT /users/:id endpoint
5+
6+
Added an update-user endpoint to the users resource following the existing patterns in the codebase.
7+
8+
**`db/store.js`** — added `updateUser(id, { name, email })`: locates the user by id using `findIndex`, returns `undefined` if not found, otherwise replaces the record in-place and returns the updated user. Exported alongside the existing store methods.
9+
10+
**`routes/users.js`** — added `PUT /users/:id` handler: converts the `:id` param to a number, rejects requests missing `name` or `email` with a 400 error, calls `store.updateUser` and returns 404 if the user doesn't exist, otherwise responds with the updated user object and a 200 status.

db/store.js

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

27-
module.exports = { getAllUsers, getUserById, createUser };
27+
function updateUser(id, { name, email }) {
28+
const index = users.findIndex((u) => u.id === id);
29+
if (index === -1) return undefined;
30+
users[index] = { ...users[index], name, email };
31+
return users[index];
32+
}
33+
34+
module.exports = { getAllUsers, getUserById, createUser, updateUser };

routes/users.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ router.get("/:id", (req, res) => {
2020
res.json(user);
2121
});
2222

23+
// PUT /users/:id — update an existing user, or 404 if it doesn't exist
24+
router.put("/:id", (req, res) => {
25+
const id = Number(req.params.id);
26+
const { name, email } = req.body;
27+
28+
if (!name || !email) {
29+
return res.status(400).json({ error: "name and email are required" });
30+
}
31+
32+
const user = store.updateUser(id, { name, email });
33+
if (!user) {
34+
return res.status(404).json({ error: "User not found" });
35+
}
36+
37+
res.json(user);
38+
});
39+
2340
// POST /users — create a user; name and email are required
2441
router.post("/", (req, res) => {
2542
const { name, email } = req.body;

0 commit comments

Comments
 (0)