diff --git a/app/api/grade/route.ts b/app/api/grade/route.ts index 4f7b41d..9897d38 100644 --- a/app/api/grade/route.ts +++ b/app/api/grade/route.ts @@ -1,25 +1,23 @@ -import type { NextApiRequest, NextApiResponse } from 'next' +import {NextResponse} from "next/server"; import prisma from '@lib/prisma'; - interface IBody { key: string grade: number } -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - const body = req.body as IBody - if (req.method === 'POST') { - // Process a POST request - await prisma.grade.findFirst({where: {key: body.key}}).then(async (grade) => { - if (grade) { - await prisma.grade.create({data: {test: 1, grade: body.grade, key: body.key}}) - res.status(200).json({message: 'Grade updated', cookie: 'voted=true'}) - } else { - res.status(404).json({message: 'Key not found'}) - } - }) - } else { - // Handle any other HTTP method - } +export async function POST(req: Request){ + const body = await req.json() as IBody + await prisma.grade.findFirst({where: {key: body.key}}).then(async (grade) => { + if (grade) { + await prisma.grade.create({data: {test: 1, grade: body.grade, key: body.key}}) + return new NextResponse( + JSON.stringify({message: 'Grade updated', cookie: 'voted=true'}), {status: 200} + ) + } else { + return new NextResponse( + JSON.stringify({message: 'Key not found'}), {status: 404} + ) + } + }) } diff --git a/app/results.tsx b/app/results.tsx new file mode 100644 index 0000000..55a1fde --- /dev/null +++ b/app/results.tsx @@ -0,0 +1,53 @@ +"use server"; +import { + Chart as ChartJS, + CategoryScale, + LinearScale, + Tooltip, + PointElement, + LineElement, +} from "chart.js"; +import { Line } from "react-chartjs-2"; + +// Register ChartJS components using ChartJS.register +ChartJS.register( + CategoryScale, + LinearScale, + PointElement, + LineElement, + Tooltip +); + +import { PrismaClient } from '@prisma/client' + +const prisma = new PrismaClient() + +function getGrades() { + return prisma.grade.findMany() +} + + +const results = async () => { + const grades = await getGrades() + const gradesArray = grades.map(grade => grade.grade.toString()) + const allGrades = ["1", "1,5", "2", "2,5", "3", "3,5", "4", "4,5", "5", "5,5", "6"] + const gradesCount = allGrades.map(grade => gradesArray.filter(g => g === grade).length) + console.log(grades) + + return ( +