This commit is contained in:
Elie Baier 2023-12-04 11:37:10 +01:00
parent cc2dc0aa74
commit 873ff3d5ae
5 changed files with 7 additions and 72 deletions

View File

@ -8,15 +8,11 @@ interface IBody {
export async function POST(req: Request){ export async function POST(req: Request){
const body = await req.json() as IBody const body = await req.json() as IBody
await prisma.grade.findFirst({where: {key: body.key}}).then(async (grade) => { const grade = await prisma.grade.findFirst({ select: { id: true}, where: { oral: 1, key: { key: body.key } } });
if (grade) { if(grade) return NextResponse.json({message: 'Deja vote'}, {status: 403});
await prisma.grade.create({data: {test: 1, grade: body.grade, key: body.key}})
return NextResponse.json({message: 'Grade updated', cookie: 'voted=true'}, {status: 200}) const gradeCreated = await prisma.grade.create({ data: { key: { connect: { key: body.key } }, grade: body.grade, oral: 1 } });
} else { if(gradeCreated) return NextResponse.json({message: 'Vote enregistré'}, {status: 200});
return NextResponse.json(
JSON.stringify({message: 'Key not found'}), {status: 404}
)
}
});
return NextResponse.json({message: 'Server error'}, {status: 500}) return NextResponse.json({message: 'Server error'}, {status: 500})
} }

View File

@ -3,11 +3,6 @@ import { Inter } from 'next/font/google';
const inter = Inter({ subsets: ['latin'] }); const inter = Inter({ subsets: ['latin'] });
export const metadata: Metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
};
export default function RootLayout({ children }: { children: React.ReactNode }) { export default function RootLayout({ children }: { children: React.ReactNode }) {
return ( return (
<html lang="en"> <html lang="en">

View File

@ -22,9 +22,6 @@ export default function play() {
const response = await fetch('/api/grade', { const response = await fetch('/api/grade', {
method: 'POST', method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ grade, key: password }), body: JSON.stringify({ grade, key: password }),
}); });
const data = await response.json(); const data = await response.json();

View File

@ -1,53 +0,0 @@
"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 (
<div>
<Line
data={{
labels: allGrades,
datasets: [
{
data: gradesCount,
backgroundColor: "purple",
},
],
}}
/>
</div>
);
};
export default results;

View File

@ -16,7 +16,7 @@ model Keys {
model Grade { model Grade {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
test Int oral Int
grade Float grade Float
key Keys @relation(fields: [keyId], references: [id]) key Keys @relation(fields: [keyId], references: [id])
keyId Int keyId Int