From cc0b628048390de3d5ac4749fa5c8637e4ca057e Mon Sep 17 00:00:00 2001 From: trafficlunar Date: Sat, 12 Apr 2025 22:37:52 +0100 Subject: [PATCH] feat: page and limit params in mii list route --- src/app/api/mii/list/route.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/app/api/mii/list/route.ts b/src/app/api/mii/list/route.ts index 7ed91df..bd1e91a 100644 --- a/src/app/api/mii/list/route.ts +++ b/src/app/api/mii/list/route.ts @@ -4,7 +4,7 @@ import { z } from "zod"; import { auth } from "@/lib/auth"; import { prisma } from "@/lib/prisma"; -import { nameSchema, tagsSchema } from "@/lib/schemas"; +import { nameSchema } from "@/lib/schemas"; const searchSchema = z.object({ query: nameSchema.optional(), @@ -18,11 +18,25 @@ const searchSchema = z.object({ .map((tag) => tag.trim()) .filter((tag) => tag.length > 0) ), + // todo: incorporate tagsSchema + // Profiles userId: z.coerce - .number({ message: "User ID must be valid" }) + .number({ message: "User ID must be a number" }) .int({ message: "User ID must be an integer" }) .positive({ message: "User ID must be valid" }) .optional(), + // Pages + limit: z.coerce + .number({ message: "Limit must be a number" }) + .int({ message: "Limit must be an integer" }) + .min(1, { message: "Limit must be at least 1" }) + .max(100, { message: "Limit cannot be more than 100" }) + .optional(), + page: z.coerce + .number({ message: "Page must be a number" }) + .int({ message: "Page must be an integer" }) + .min(1, { message: "Page must be at least 1" }) + .optional(), }); export async function GET(request: NextRequest) { @@ -31,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 { query, sort, tags, userId } = parsed.data; + const { query, sort, tags, userId, page = 0, limit = 20 } = parsed.data; const where: Prisma.MiiWhereInput = { // Searching @@ -78,7 +92,7 @@ export async function GET(request: NextRequest) { const [totalCount, filteredCount, list] = await Promise.all([ prisma.mii.count({ where: userId ? { userId } : {} }), prisma.mii.count({ where }), - prisma.mii.findMany({ where, orderBy, select }), + prisma.mii.findMany({ where, orderBy, select, skip: (page - 1) * limit, take: limit }), ]); return NextResponse.json({