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
9 changes: 9 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The original plan outlined the necessary steps: the updates to routes/users.js and db/store.js. It wanted to write NOTES.ms itself, but I told it not to do it - the task was that I write it myself. I also made sure each step is committed one by one, without pushes in the beginning. I also added a prompt for a small stylistic change to the code it suggested.

I decided to go on with Sonnet. I think this implementation does not need heavy lifting to use Opus, but is critical enough to avoid using Haiku.

I went with one commit/one logical change. I did not push it deliberately until all tests passed.

It found a duplicated condition check, which I told it to refactor since I love clean code.

Added this extra line to reopen pull request, as the platform could not check it first
15 changes: 14 additions & 1 deletion db/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,17 @@ function createUser({ name, email }) {
return user;
}

module.exports = { getAllUsers, getUserById, createUser };
function updateUser(id, { name, email }) {
const user = getUserById(id);

if (!user) {
return undefined;
}

user.name = name;
user.email = email;

return user;
}

module.exports = { getAllUsers, getUserById, createUser, updateUser };
Comment on lines +27 to +40

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updateUser function is correctly implemented: it finds the user, returns undefined for not-found cases, updates both fields, and exports properly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The route you're missing is there, you muppet

24 changes: 23 additions & 1 deletion routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const store = require("../db/store");

const router = express.Router();

function hasRequiredFields({ name, email }) {
return !!name && !!email;
}

// GET /users — list every user
router.get("/", (req, res) => {
res.json(store.getAllUsers());
Expand All @@ -24,12 +28,30 @@ router.get("/:id", (req, res) => {
router.post("/", (req, res) => {
const { name, email } = req.body;

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

const user = store.createUser({ name, email });
res.status(201).json(user);
});

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

if (!hasRequiredFields({ 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;