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
10 changes: 10 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Model:
I use Sonnet model

# Changes: Add PUT /users/:id endpoint

Added an update-user endpoint to the users resource following the existing patterns in the codebase.

**`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.

**`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.
9 changes: 8 additions & 1 deletion db/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ function createUser({ name, email }) {
return user;
}

module.exports = { getAllUsers, getUserById, createUser };
function updateUser(id, { name, email }) {
const index = users.findIndex((u) => u.id === id);
if (index === -1) return undefined;
users[index] = { ...users[index], name, email };
return users[index];
}

module.exports = { getAllUsers, getUserById, createUser, updateUser };
17 changes: 17 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ router.get("/:id", (req, res) => {
res.json(user);
});

// PUT /users/:id — update an existing user, or 404 if it doesn't exist
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);
});

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