Skip to content

Commit e74e3f5

Browse files
committed
Add PUT /users/:id route
Validates name/email are present (400 if not), 404s when the id doesn't match a user, otherwise updates and returns the record. Mirrors the existing GET /:id and POST / handlers.
1 parent 3e23e95 commit e74e3f5

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

routes/users.js

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

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

0 commit comments

Comments
 (0)