Skip to content

Add update-user endpoint - #117

Open
kl1874 wants to merge 4 commits into
mate-academy:mainfrom
kl1874:add-update-user-endpoint
Open

Add update-user endpoint#117
kl1874 wants to merge 4 commits into
mate-academy:mainfrom
kl1874:add-update-user-endpoint

Conversation

@kl1874

@kl1874 kl1874 commented Aug 31, 2026

Copy link
Copy Markdown

What

Adds PUT /users/:id to the users resource: updates an existing user, validates name/email (400 on missing/invalid), and returns 404 for an unknown id instead of crashing. Adds a matching updateUser helper in db/store.js so the route goes through the store like the rest of the resource.

Why

Closes out the users resource with update support, following the same validation/error shape already used by POST /users and the same not-found handling used by GET /users/:id.

What to test

  • PUT /users/:id with a valid body updates the user and returns 200 with the updated fields.
  • PUT /users/9999 (unknown id) returns 404, not a crash.
  • PUT /users/:id with a missing name or email returns 400 with a clear error message.
  • npm test is green (9/9), including the pre-existing update-user.test.js suite.

See NOTES.md for the plan, model choice, commit split, and self-review notes.

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review Summary

The updateUser helper in db/store.js is correctly implemented and follows the existing store pattern — it returns undefined for unknown ids, updates the user in place, and is properly exported. However, routes/users.js is not present in the submitted files, which means the PUT /users/:id route handler itself cannot be reviewed. This is a critical blocker: the store helper is only half the deliverable. Without the route file, there is no way to verify that the endpoint exists, that it validates name/email presence with a 400 error, or that it correctly translates the store's undefined return into a 404 response. Please upload routes/users.js so the full implementation can be reviewed against all task requirements.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

@kl1874

kl1874 commented Aug 31, 2026

Copy link
Copy Markdown
Author

@mateacademy-ai-mentor routes/users.js is present in this PR — it's listed as a modified file in the diff (18 additions, adding the PUT /users/:id handler) alongside db/store.js, CLAUDE.md, and NOTES.md. Confirmed via gh pr view 117 --json files and gh pr diff 117, both of which show the full route diff including the validation (400) and not-found (404) logic. Could you re-check? Re-requesting your review now.

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review Summary

I am requesting changes because the core implementation files needed to verify the task are not available in this review payload, which blocks confirming required functionality. While CLAUDE.md and NOTES.md look good, I cannot inspect routes/users.js and db/store.js here to ensure the PUT /users/:id route validates inputs (400 for missing name/email), returns 404 for unknown ids, and uses the updateUser helper as required. Please upload or paste the full contents (with line numbers) of routes/users.js and db/store.js so I can verify: 1) the route exists and matches the existing error shape from POST /users, 2) it returns 400 when either field is missing, and 3) it returns 404 when updateUser returns undefined. If these files are already committed, re-submit them in this review or include the diffs. Minor: your documentation is clear and aligns with the workflow—no changes needed there. Once I can confirm the route and store behavior, I’ll approve promptly.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread CLAUDE.md
Comment on lines +1 to +58
# CLAUDE.md

Small Express API (CommonJS, Node's built-in test runner). Routes live in `routes/`, all data
access goes through `db/store.js`, and `server.js` exports `app` so tests can import it without
opening a port.

- `npm test` — runs `node --test`
- `npm run dev` — `node --watch server.js`
- `npm run lint` — eslint

Current task: add `PUT /users/:id` to `routes/users.js`, with the store helper it needs in
`db/store.js`. `tests/update-user.test.js` is the spec — do not edit it.

## Models

Plan with **Opus**, execute with **Sonnet**.

Use Opus in plan mode to decide the approach — which files change, how validation and the
not-found path work, how the work splits into commits. Switch to Sonnet (`/model sonnet`) to
write the code, run tests, and commit. If a plan turns out to be wrong mid-execution, stop and
re-plan with Opus rather than improvising in Sonnet.

## Commits

One logical change per commit. Write the message from the actual diff — read what changed, then
describe it; do not paraphrase the task description.

Run `npm test` as you go, not just at the end. The `update-user` tests turn green once the
endpoint is right; that is the signal the feature is done.

Each message must be understandable without opening the diff. A reader scanning `git log`
should know what changed and why.

Good: Add updateUser helper to store, returning undefined for unknown ids
Bad: Update store.js

Do not bundle the store helper, the route, and `NOTES.md` into one commit — they are separate
logical changes.

## Review before the PR

Before opening the pull request, review the changes yourself — `git diff main...HEAD` — and
report what you find. Look specifically for:

- bugs and edge cases (non-numeric `:id`, empty-string vs missing field, extra body fields)
- the not-found path — 404, not a crash or a 500
- the validation path — 400 with a clear error, matching the shape `POST /users` already uses

Present what you flag as findings for me to judge. I decide what is real; fix those, and say
plainly which ones I chose to skip.

A green test run and a clean review are signals, not a guarantee. Do not describe the change as
verified on the strength of passing tests alone — say what was actually checked and what was not.

## PR

The description must say what changed, why, and what a reviewer should test — including the
not-found and invalid-input cases.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

CLAUDE.md correctly documents the workflow and expectations. No action needed here. (No functional code to validate.)

Comment thread CLAUDE.md
Comment on lines +1 to +26
# CLAUDE.md

Small Express API (CommonJS, Node's built-in test runner). Routes live in `routes/`, all data
access goes through `db/store.js`, and `server.js` exports `app` so tests can import it without
opening a port.

- `npm test` — runs `node --test`
- `npm run dev` — `node --watch server.js`
- `npm run lint` — eslint

Current task: add `PUT /users/:id` to `routes/users.js`, with the store helper it needs in
`db/store.js`. `tests/update-user.test.js` is the spec — do not edit it.

## Models

Plan with **Opus**, execute with **Sonnet**.

Use Opus in plan mode to decide the approach — which files change, how validation and the
not-found path work, how the work splits into commits. Switch to Sonnet (`/model sonnet`) to
write the code, run tests, and commit. If a plan turns out to be wrong mid-execution, stop and
re-plan with Opus rather than improvising in Sonnet.

## Commits

One logical change per commit. Write the message from the actual diff — read what changed, then
describe it; do not paraphrase the task description.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

NOTES.md contains a clear plan, model choice, commit split and review summary as required by the checklist. However, the NOTES states the implementation exists; I cannot verify that because the route and store implementation files are not included in this review. Please include routes/users.js and db/store.js (or the diffs) so I can confirm the endpoint validates input, returns 400 on missing fields, and returns 404 for unknown ids as required by the tests.

Comment thread CLAUDE.md
Comment on lines +1 to +26
# CLAUDE.md

Small Express API (CommonJS, Node's built-in test runner). Routes live in `routes/`, all data
access goes through `db/store.js`, and `server.js` exports `app` so tests can import it without
opening a port.

- `npm test` — runs `node --test`
- `npm run dev` — `node --watch server.js`
- `npm run lint` — eslint

Current task: add `PUT /users/:id` to `routes/users.js`, with the store helper it needs in
`db/store.js`. `tests/update-user.test.js` is the spec — do not edit it.

## Models

Plan with **Opus**, execute with **Sonnet**.

Use Opus in plan mode to decide the approach — which files change, how validation and the
not-found path work, how the work splits into commits. Switch to Sonnet (`/model sonnet`) to
write the code, run tests, and commit. If a plan turns out to be wrong mid-execution, stop and
re-plan with Opus rather than improvising in Sonnet.

## Commits

One logical change per commit. Write the message from the actual diff — read what changed, then
describe it; do not paraphrase the task description.

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 previous review summary (from the review context) flagged that routes/users.js was missing from the PR. The user message claims the file is present in the PR. If you believe it is present, please re-upload its contents here (the file text with line markers) so I can inspect it line-by-line and confirm compliance with the checklist.

@kl1874

kl1874 commented Aug 31, 2026

Copy link
Copy Markdown
Author

@mateacademy-ai-mentor Here are the full contents of both files, with line numbers, as requested.

routes/users.js

 1  const express = require("express");
 2  const store = require("../db/store");
 3
 4  const router = express.Router();
 5
 6  // GET /users — list every user
 7  router.get("/", (req, res) => {
 8    res.json(store.getAllUsers());
 9  });
10
11  // GET /users/:id — fetch a single user, or 404 if it doesn't exist
12  router.get("/:id", (req, res) => {
13    const id = Number(req.params.id);
14    const user = store.getUserById(id);
15
16    if (!user) {
17      return res.status(404).json({ error: "User not found" });
18    }
19
20    res.json(user);
21  });
22
23  // POST /users — create a user; name and email are required
24  router.post("/", (req, res) => {
25    const { name, email } = req.body;
26
27    if (!name || !email) {
28      return res.status(400).json({ error: "name and email are required" });
29    }
30
31    const user = store.createUser({ name, email });
32    res.status(201).json(user);
33  });
34
35  // PUT /users/:id — update an existing user; name and email are required
36  router.put("/:id", (req, res) => {
37    const id = Number(req.params.id);
38    const { name, email } = req.body;
39
40    if (!name || !email) {
41      return res.status(400).json({ error: "name and email are required" });
42    }
43
44    const user = store.updateUser(id, { name, email });
45
46    if (!user) {
47      return res.status(404).json({ error: "User not found" });
48    }
49
50    res.json(user);
51  });
52
53  module.exports = router;

db/store.js

 1  // A tiny in-memory data store. It stands in for a real database so the
 2  // project stays easy to run. Data is not persisted — it resets every time
 3  // the server restarts.
 4
 5  let users = [
 6    { id: 1, name: "Ada Lovelace", email: "ada@example.com" },
 7    { id: 2, name: "Alan Turing", email: "alan@example.com" },
 8  ];
 9
10  let nextId = 3;
11
12  function getAllUsers() {
13    return users;
14  }
15
16  function getUserById(id) {
17    return users.find((user) => user.id === id);
18  }
19
20  function createUser({ name, email }) {
21    const user = { id: nextId, name, email };
22    nextId += 1;
23    users.push(user);
24    return user;
25  }
26
27  function updateUser(id, { name, email }) {
28    const user = getUserById(id);
29
30    if (!user) {
31      return undefined;
32    }
33
34    user.name = name;
35    user.email = email;
36    return user;
37  }
38
39  module.exports = { getAllUsers, getUserById, createUser, updateUser };

Confirming against your three checks:

  1. PUT /users/:id exists (line 36) and its 400 error uses the same shape as POST /users ({ error: "name and email are required" }, line 41 vs line 28).
  2. Returns 400 when either name or email is missing (line 40-42).
  3. Returns 404 when store.updateUser returns undefined for an unknown id (line 46-48), backed by updateUser in db/store.js returning undefined when getUserById finds nothing (line 30-32).

Re-requesting your review now.

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review Summary

Your implementation is approved and complete. The PUT /users/:id endpoint correctly validates name and email, returns 400 with the exact error shape matching POST /users, handles unknown ids with 404 via store.updateUser, and all data access goes through db/store.js as required. Your documentation in CLAUDE.md and NOTES.md is thorough and well-organized. Great work following the existing patterns precisely — this makes the codebase consistent and maintainable. The only minor step before final submission is confirming npm test is green on the pushed branch to fully close the loop.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants