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
11 changes: 11 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Ship a change end to end

Plan: Add PUT /users/:id, validate required name and email fields, use db/store.js for updates, and return 404 when the user does not exist. No plan edits were needed.

Model: GPT-5.6 Luna, used because Claude Code was unavailable.

Commits: One logical commit containing the route, store helper, and notes because this is one small cohesive feature.

Review: Confirmed 400 validation, 404 for unknown users, updates through the store, and unchanged provided tests.

Developer: Hammad Hussain
17 changes: 13 additions & 4 deletions db/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// A tiny in-memory data store. It stands in for a real database so the
// project stays easy to run. Data is not persisted — it resets every time
// the server restarts.
const fs = require("fs");

Check warning on line 1 in db/store.js

View workflow job for this annotation

GitHub Actions / check

'fs' is assigned a value but never used
const path = require("path");

const FILE = path.join(__dirname, "..", "users.json");

Check warning on line 4 in db/store.js

View workflow job for this annotation

GitHub Actions / check

'FILE' is assigned a value but never used

let users = [
{ id: 1, name: "Ada Lovelace", email: "ada@example.com" },
Expand All @@ -24,4 +25,12 @@
return user;
}

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

module.exports = { getAllUsers, getUserById, createUser, updateUser };
24 changes: 11 additions & 13 deletions routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,31 @@ const store = require("../db/store");

const router = express.Router();

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

// GET /users/:id — fetch a single user, or 404 if it doesn't exist
router.get("/:id", (req, res) => {
const id = Number(req.params.id);
const user = store.getUserById(id);

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

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;

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

if (!name || !email) return res.status(400).json({ error: "name and email are required" });
const user = store.createUser({ name, email });
res.status(201).json(user);
});

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;