pagination

This commit is contained in:
briannelson95
2025-07-02 20:33:23 -04:00
parent 95f8dfe2ab
commit e6e24f12d4
9 changed files with 211 additions and 30 deletions

View File

@@ -0,0 +1,24 @@
import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url)
const cursor = searchParams.get('cursor') ?? undefined
const take = parseInt(searchParams.get('take') || '10', 10)
try {
const entries = await prisma.guestBookEntry.findMany({
take,
skip: cursor ? 1 : 0,
...(cursor && { cursor: { id: cursor } }),
orderBy: [{ lName: 'asc' }, { fName: 'asc' }],
})
const nextCursor = entries.length === take ? entries[entries.length - 1].id : null
return NextResponse.json({ entries, nextCursor })
} catch (error) {
console.error('[GET GUESTBOOK PAGINATE]', error)
return NextResponse.json({ message: 'Failed to fetch paginated entries' }, { status: 500 })
}
}