77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
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 }> },
|
|
) {
|
|
const { book } = await params;
|
|
const url = new URL(req.url);
|
|
const division = url.searchParams.get("division");
|
|
const chapterStr = url.searchParams.get("chapter");
|
|
const indexOnly = url.searchParams.get("indexOnly") === "true";
|
|
const pageStr = url.searchParams.get("page");
|
|
const limitStr = url.searchParams.get("limit");
|
|
|
|
if (!chapterStr) {
|
|
return NextResponse.json(
|
|
{ error: "chapter query param is required" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const chapter = parseInt(chapterStr);
|
|
const where: Record<string, unknown> = { book, chapter };
|
|
|
|
if (book === "bg") {
|
|
where.division = null;
|
|
} else if (division) {
|
|
where.division = division;
|
|
}
|
|
|
|
// --- 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,
|
|
orderBy: { verse: "asc" },
|
|
select: {
|
|
verse: true,
|
|
verseEnd: true,
|
|
reference: true,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ verses, total: verses.length });
|
|
}
|
|
|
|
// --- 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 skip = (page - 1) * limit;
|
|
|
|
const [verses, total] = await Promise.all([
|
|
prisma.verse.findMany({
|
|
where,
|
|
orderBy: { verse: "asc" },
|
|
skip,
|
|
take: limit,
|
|
}),
|
|
prisma.verse.count({ where }),
|
|
]);
|
|
|
|
return NextResponse.json({
|
|
verses,
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages: Math.ceil(total / limit),
|
|
hasMore: skip + verses.length < total,
|
|
});
|
|
}
|