Skip to content

Commit 9a71ed3

Browse files
Implement update user endpoint
1 parent 19e97af commit 9a71ed3

3 files changed

Lines changed: 52 additions & 5 deletions

File tree

NOTES.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Notes
2+
3+
## Implementation
4+
5+
Added `PUT /users/:id` to update an existing user by ID. The endpoint validates that both `name` and `email` are provided, returns `400` when either field is missing, returns `404` when the requested user does not exist, and returns the updated user with status `200` on success.
6+
7+
The in-memory store now provides an `updateUser` helper that finds the user, updates its name and email, and returns the updated record.
8+
9+
## Testing
10+
11+
The existing grading tests were left unchanged. The implementation is designed to satisfy the success, missing-user, and missing-field cases covered by those tests.
12+
13+
## Design decision
14+
15+
Validation is performed in the route before calling the store helper, while the store helper is responsible only for locating and updating the user. This keeps HTTP concerns in the route and data operations in the store.

db/store.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// A tiny in-memory data store. It stands in for a real database so the
2-
// project stays easy to run. Data is not persisted — it resets every time
2+
// project stays easy to run. Data is not persisted — it resets every time
33
// the server restarts.
44

55
let users = [
@@ -24,4 +24,22 @@ 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+
37+
return user;
38+
}
39+
40+
module.exports = {
41+
getAllUsers,
42+
getUserById,
43+
createUser,
44+
updateUser,
45+
};

routes/users.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ const store = require("../db/store");
33

44
const router = express.Router();
55

6-
// GET /users — list every user
76
router.get("/", (req, res) => {
87
res.json(store.getAllUsers());
98
});
109

11-
// GET /users/:id — fetch a single user, or 404 if it doesn't exist
1210
router.get("/:id", (req, res) => {
1311
const id = Number(req.params.id);
1412
const user = store.getUserById(id);
@@ -20,7 +18,6 @@ router.get("/:id", (req, res) => {
2018
res.json(user);
2119
});
2220

23-
// POST /users — create a user; name and email are required
2421
router.post("/", (req, res) => {
2522
const { name, email } = req.body;
2623

@@ -32,4 +29,21 @@ router.post("/", (req, res) => {
3229
res.status(201).json(user);
3330
});
3431

32+
router.put("/:id", (req, res) => {
33+
const id = Number(req.params.id);
34+
const { name, email } = req.body;
35+
36+
if (!name || !email) {
37+
return res.status(400).json({ error: "name and email are required" });
38+
}
39+
40+
const user = store.updateUser(id, { name, email });
41+
42+
if (!user) {
43+
return res.status(404).json({ error: "User not found" });
44+
}
45+
46+
res.json(user);
47+
});
48+
3549
module.exports = router;

0 commit comments

Comments
 (0)