add: route to get today's test

This commit is contained in:
Elie Baier 2023-12-05 22:23:34 +01:00
parent 8e78611aab
commit 2fdbb96488
1 changed files with 38 additions and 0 deletions

38
app/api/test/route.ts Normal file
View File

@ -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
}
});
}