From 2fdf120280a5db48cefaea6e78ed335afd51d7fa Mon Sep 17 00:00:00 2001 From: trafficlunar Date: Mon, 31 Mar 2025 18:09:51 +0100 Subject: [PATCH] feat: random mii button --- src/app/random/route.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/app/random/route.ts diff --git a/src/app/random/route.ts b/src/app/random/route.ts new file mode 100644 index 0000000..888764f --- /dev/null +++ b/src/app/random/route.ts @@ -0,0 +1,18 @@ +import { redirect } from "next/navigation"; +import { prisma } from "@/lib/prisma"; + +export async function GET() { + const count = await prisma.mii.count(); + if (count === 0) redirect("/"); + + const randomIndex = Math.floor(Math.random() * count); + const randomMii = await prisma.mii.findFirst({ + skip: randomIndex, + take: 1, + select: { id: true }, + }); + + if (!randomMii) redirect("/"); + + redirect(`/mii/${randomMii.id}`); +}