CodingNic

Build the Library

Build the Book Data API

Build the Library 15 min read

Build the Book Data API

Build the Book Data API

Task

Create the authenticated Book API that returns only books owned by the current user.

Route

Create:

text
app/api/books/route.ts

GET

ts
const user = await requireUser();

const books = await prisma.book.findMany({
  where: { userId: user.id },
  orderBy: { createdAt: "desc" },
});

return Response.json({ books });

Do not return another user’s records.

Add the POST shape now if the Library needs to create a book record before a later upload step:

ts
const book = await prisma.book.create({
  data: {
    userId: user.id,
    title,
    author,
    // storage fields are added when upload is implemented
  },
});

Keep upload-specific storage handling for Module 5.

Test

Call the endpoint while authenticated and logged out. Confirm the response is user-scoped and unauthenticated requests are rejected.

Checkpoint

The Library has a real authenticated Book data boundary.