feat: pagination

This commit is contained in:
trafficlunar 2025-04-13 20:00:33 +01:00
parent 3f4a757bb1
commit cada748f2d
3 changed files with 99 additions and 2 deletions

View file

@ -45,7 +45,7 @@ export async function GET(request: NextRequest) {
const parsed = searchSchema.safeParse(Object.fromEntries(request.nextUrl.searchParams));
if (!parsed.success) return NextResponse.json({ error: parsed.error.errors[0].message }, { status: 400 });
const { q: query, sort, tags, userId, page = 1, limit = 20 } = parsed.data;
const { q: query, sort, tags, userId, page = 1, limit = 24 } = parsed.data;
const where: Prisma.MiiWhereInput = {
// Searching
@ -92,7 +92,7 @@ export async function GET(request: NextRequest) {
const skip = (page - 1) * limit;
const [totalCount, filteredCount, list] = await Promise.all([
prisma.mii.count({ where: userId ? { userId } : {} }),
prisma.mii.count({ where: { ...where, userId } }),
prisma.mii.count({ where, skip, take: limit }),
prisma.mii.findMany({ where, orderBy, select, skip: (page - 1) * limit, take: limit }),
]);
@ -100,6 +100,7 @@ export async function GET(request: NextRequest) {
return NextResponse.json({
total: totalCount,
filtered: filteredCount,
lastPage: Math.ceil(totalCount / limit),
miis: list.map(({ _count, likedBy, ...rest }) => ({
...rest,
likes: _count.likedBy,