CodingNic

Build the Library

Delete Books Safely

Build the Library 15 min read

Delete Books Safely

Delete Books Safely

Task

Add safe Book deletion before storage files exist.

Route

Create:

text
DELETE /api/books/:id

First verify ownership:

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

if (result.count === 0) {
  return Response.json({ error: "Book not found" }, { status: 404 });
}

Do not use a browser confirm().

Use the project’s custom delete dialog and require an explicit confirmation.

UI

After deletion:

  1. close the dialog
  2. remove or refresh the book
  3. show a bottom-right success toast

Storage cleanup is intentionally deferred until Module 5, when each Book can have an associated object-storage key.

Test

Delete a book and reload the Library. Confirm it is gone.

Cancel the custom dialog and confirm nothing is deleted.

Checkpoint

Book deletion works through a deliberate UI confirmation and an authenticated server mutation.