From 2fdbb9648890d6081587293cef3a2b871b1bde42 Mon Sep 17 00:00:00 2001 From: Fayorg Date: Tue, 5 Dec 2023 22:23:34 +0100 Subject: [PATCH] add: route to get today's test --- app/api/test/route.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 app/api/test/route.ts diff --git a/app/api/test/route.ts b/app/api/test/route.ts new file mode 100644 index 0000000..ebff42d --- /dev/null +++ b/app/api/test/route.ts @@ -0,0 +1,38 @@ +import {NextRequest, NextResponse} from "next/server"; +import prisma from "@/lib/prisma"; + +export async function GET(req: NextRequest){ + const date = req.nextUrl.searchParams.get("date"); + + const test = await prisma.test.findFirst({ + select: { + id: true, + testOn: true, + testOf: { + select: { + id: true, + firstName: true, + lastName: true, + isTeacher: true + } + } + }, + where: { + testOn: (date ? new Date(date) : new Date()) + } + }); + + if(!test){ + return NextResponse.json({error: "Test not found"}, {status: 404}); + } + + return NextResponse.json({ + id: test.id, + testOn: test.testOn, + testOf: { + firstName: test.testOf.firstName, + lastName: test.testOf.lastName, + isTeacher: test.testOf.isTeacher + } + }); +}