Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Implementation Notes

## Plan
Added a PUT /users/:id endpoint to update existing users with the following features:
- Validates that both name and email fields are provided
- Returns 400 with a clear error message if validation fails
- Returns 404 if the user doesn't exist
- Returns the updated user object on success

## Model
Used Claude Haiku 4.5 for implementation.

## Implementation Approach
1. Added `updateUser(id, { name, email })` function to db/store.js that updates a user by ID and returns the updated user or null if not found
2. Added PUT /users/:id route handler that validates input, checks for user existence, and calls the store function
3. Followed existing patterns from the GET /:id and POST / endpoints for consistency

## Changes Made
- **routes/users.js**: Added PUT endpoint with validation and error handling
- **db/store.js**: Added updateUser function that modifies user fields and returns the updated user

All tests now pass.
12 changes: 11 additions & 1 deletion db/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ function createUser({ name, email }) {
return user;
}

module.exports = { getAllUsers, getUserById, createUser };
function updateUser(id, { name, email }) {
const user = users.find((u) => u.id === id);
if (!user) {
return null;
}
if (name !== undefined) user.name = name;
if (email !== undefined) user.email = email;
return user;
}

module.exports = { getAllUsers, getUserById, createUser, updateUser };
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
},
"devDependencies": {
"eslint": "^8.57.0",
"supertest": "^7.0.0"
"supertest": "^7.2.2"
}
}
18 changes: 18 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,22 @@ router.post("/", (req, res) => {
res.status(201).json(user);
});

// PUT /users/:id — update a user; name and email are required
router.put("/:id", (req, res) => {
const id = Number(req.params.id);
const { name, email } = req.body;

if (!name || !email) {
return res.status(400).json({ error: "name and email are required" });
}

const user = store.updateUser(id, { name, email });

if (!user) {
return res.status(404).json({ error: "User not found" });
}

res.json(user);
});

module.exports = router;