1
0
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:
timhaller
2023-12-04 10:41:48 +01:00
parent ecbd14a06c
commit 4e7efb327f
4 changed files with 97 additions and 17 deletions

View File

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