From e9fe5bb22ac92de50d0dd2f868d77b0e68f3bade Mon Sep 17 00:00:00 2001 From: headlesdev Date: Sat, 17 May 2025 14:51:16 +0200 Subject: [PATCH] Create User API Route --- app/api/user/create/route.ts | 34 ++++++++++++++++++++++++++++++++++ app/prisma.ts | 5 +++++ 2 files changed, 39 insertions(+) create mode 100644 app/api/user/create/route.ts create mode 100644 app/prisma.ts diff --git a/app/api/user/create/route.ts b/app/api/user/create/route.ts new file mode 100644 index 0000000..93e3f1e --- /dev/null +++ b/app/api/user/create/route.ts @@ -0,0 +1,34 @@ +import { NextRequest, NextResponse } from "next/server"; +import prisma from "@/app/prisma"; + +interface Body { + username: string; + name: string; + email: string; + password: string; +} + +export async function POST(request: NextRequest) { + try { + const body: Body = await request.json(); + + if (!body.username || !body.name || !body.email || !body.password) { + return NextResponse.json({ error: "Missing required fields" }, { status: 400 }); + } + + const user = await prisma.user.create({ + data: { + username: body.username, + name: body.name, + email: body.email, + password: body.password, + }, + }); + + return NextResponse.json({ user }, { status: 201 }); + + } catch (error: any) { + return NextResponse.json({ error: "Internal Server Error" }, { status: 500 }); + } +} + \ No newline at end of file diff --git a/app/prisma.ts b/app/prisma.ts new file mode 100644 index 0000000..7871941 --- /dev/null +++ b/app/prisma.ts @@ -0,0 +1,5 @@ +import { PrismaClient } from "@/prisma/generated/prisma"; + +let prisma = new PrismaClient(); + +export default prisma; \ No newline at end of file