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
19 changes: 19 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Commands

- `npm install` — install dependencies
- `npm run dev` — run the server with auto-restart
- `npm test` — run tests (Node's built-in test runner)
- `node --test tests/update-user.test.js` — run a single test file
- `npm run lint` — run ESLint

## Architecture

Small Express API: `server.js` mounts routers from `routes/` (`/users`, `/health`), which call into `db/store.js` for data. `db/store.js` is an in-memory array, not a real database — data resets on restart.

`server.js` exports `app` without calling `.listen()` when required (only on direct run), so tests use `supertest` against it in-process.

New endpoints follow the same pattern: store functions in `db/store.js`, route handler in `routes/` doing validation (400) and not-found (404) checks, mounted in `server.js`.
9 changes: 9 additions & 0 deletions NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Notes

**Plan:** Task was fully spelled out, so I implemented directly: `updateUser` helper in `db/store.js`, plus a `PUT /:id` route mirroring the existing `POST /` (400 validation) and `GET /:id` (404) patterns. No changes from the initial approach.

**Model:** Claude Sonnet 5 — small CRUD endpoint following existing patterns, no need for anything heavier.

**Commits:** Store helper + route kept as one commit (neither works without the other). `CLAUDE.md` is a separate commit.

**Review:** Checked the non-numeric id case (`PUT /users/abc`) — `Number("abc")` is `NaN`, and strict `===` in `getUserById` correctly falls through to 404. Nothing needed fixing.
14 changes: 13 additions & 1 deletion db/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ function createUser({ name, email }) {
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 };
32 changes: 25 additions & 7 deletions routes/users.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
const express = require("express");
const store = require("../db/store");
const express = require('express');
const store = require('../db/store');

const router = express.Router();

// GET /users — list every user
router.get("/", (req, res) => {
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) => {
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" });
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) => {
router.post('/', (req, res) => {
const { name, email } = req.body;

if (!name || !email) {
return res.status(400).json({ error: "name and email are required" });
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 (!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;