CodingNic

Build the Library

Create and Edit Book Records

Build the Library 18 min read

Create and Edit Book Records

Create and Edit Book Records

Task

Add the create and edit flows needed by the prepared Library UI.

Routes

Use:

text
POST /api/books
PATCH /api/books/:id

Every mutation must use requireUser() and verify ownership.

For updates:

ts
const result = await prisma.book.updateMany({
  where: {
    id: bookId,
    userId: user.id,
  },
  data: {
    title,
    author,
  },
});

Using an ownership predicate directly in the update prevents a client from changing another user’s record.

Validate incoming fields before writing them.

UI

Connect the existing Add/Edit Book dialog to the routes. After a successful mutation, refresh the Library data and show the existing success toast.

Clickable controls should retain the pointer cursor.

Test

Create a book, edit its title, refresh the page, and confirm the change persists.

Attempt to edit a book that belongs to another account.

Checkpoint

Users can create and edit their own Library records, and ownership is enforced on the server.