diff --git a/src/app/api/users/[id]/route.js b/src/app/api/users/[id]/route.js index 6559b5842..ffcaa816e 100644 --- a/src/app/api/users/[id]/route.js +++ b/src/app/api/users/[id]/route.js @@ -3,7 +3,7 @@ import prisma from "@/prisma" export async function GET(request, { params }) { const user = await prisma.user.findUnique({ where: { - id: params.id + id: Number(params.id) } }) return Response.json(user) diff --git a/src/app/api/users/route.js b/src/app/api/users/route.js index 6135b10d6..f1626c41d 100644 --- a/src/app/api/users/route.js +++ b/src/app/api/users/route.js @@ -1,26 +1,39 @@ -import { NextResponse } from "next/server" +import { Prisma } from '@prisma/client' import prisma from "@/prisma" export async function GET() { const users = await prisma.user.findMany() - return NextResponse.json(users) + return Response.json(users) } export async function POST(req) { const body = await req.json() - const { email } = body; + const { email, firstname, lastname } = body; + if (!email) { - return NextResponse.json({}, { status: 400 }) + return Response.json({}, { status: 400 }) } + try { const user = await prisma.user.create({ data: { - email: email + email: email, + firstname: firstname, + lastname: lastname } }) return Response.json(user) + } catch (error) { + // synes dette er en veldig stygg måte å håndtere feil på + if ( + error instanceof Prisma.PrismaClientKnownRequestError && + error.code === 'P2002' + ) { + return Response.json({}, { status: 409 }) + } + } } \ No newline at end of file