feat: add pagination

This commit is contained in:
gosangam
2026-04-14 23:15:26 +05:30
parent beffb9fae1
commit 213eea8e94
7 changed files with 428 additions and 406 deletions
+16 -7
View File
@@ -1,8 +1,6 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/db";
const DEFAULT_PAGE_SIZE = 20;
export async function GET(
req: Request,
{ params }: { params: Promise<{ book: string }> },
@@ -11,6 +9,7 @@ export async function GET(
const url = new URL(req.url);
const division = url.searchParams.get("division");
const chapterStr = url.searchParams.get("chapter");
const verseStr = url.searchParams.get("verse");
const indexOnly = url.searchParams.get("indexOnly") === "true";
const pageStr = url.searchParams.get("page");
const limitStr = url.searchParams.get("limit");
@@ -31,8 +30,21 @@ export async function GET(
where.division = division;
}
// --- Single verse mode: ?verse=N ---
if (verseStr) {
const verseNum = parseInt(verseStr);
const verse = await prisma.verse.findFirst({
where: { ...where, verse: verseNum },
});
if (!verse) {
return NextResponse.json({ error: "Verse not found" }, { status: 404 });
}
return NextResponse.json(verse);
}
// --- Index-only mode: return lightweight verse list for the sidebar ---
// Returns all verses but only the fields needed for navigation
if (indexOnly) {
const verses = await prisma.verse.findMany({
where,
@@ -49,10 +61,7 @@ export async function GET(
// --- Paginated mode: return full verse data in pages ---
const page = Math.max(1, parseInt(pageStr || "1"));
const limit = Math.min(
100,
Math.max(1, parseInt(limitStr || String(DEFAULT_PAGE_SIZE))),
);
const limit = Math.min(100, Math.max(1, parseInt(limitStr || "20")));
const skip = (page - 1) * limit;
const [verses, total] = await Promise.all([