-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
19 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }) | ||
} | ||
} | ||
} |