mirror of
https://github.com/Fayorg/calendrier-avant.git
synced 2026-05-28 01:28:37 +02:00
Update grade route.ts to Next 13
This commit is contained in:
@@ -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}
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
53
app/results.tsx
Normal file
53
app/results.tsx
Normal file
@@ -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 (
|
||||
<div>
|
||||
<Line
|
||||
data={{
|
||||
labels: allGrades,
|
||||
datasets: [
|
||||
{
|
||||
data: gradesCount,
|
||||
backgroundColor: "purple",
|
||||
},
|
||||
],
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
export default results;
|
||||
Reference in New Issue
Block a user