diff --git a/nextjs/app/api/party/[id]/join/route.ts b/nextjs/app/api/party/[id]/join/route.ts new file mode 100644 index 0000000..0b9d0ca --- /dev/null +++ b/nextjs/app/api/party/[id]/join/route.ts @@ -0,0 +1,43 @@ +import prisma from "@/lib/prisma"; +import { getServerSession } from "next-auth"; + +export async function GET(_: Request, { params }: { params: { id: string } }) { + const session = await getServerSession(); + if (!session || !session.user || !session.user.email) { + return Response.json(null, { status: 401 }); + } + + const party = await prisma.party.findUnique({ + where: { + code: params.id, + }, + }); + + if (!party) { + return Response.json(party, { status: 400 }); + } + + if (party.started) { + return Response.json(party, { status: 403 }); + } + + await prisma.user.update({ + where: { + email: session.user.email, + }, + data: { + partyId: party.id, + }, + }); + + const res = await prisma.party.findUnique({ + where: { + code: params.id, + }, + include: { + players: true, + }, + }); + + return Response.json(res); +} diff --git a/nextjs/app/api/party/join/[id]/route.ts b/nextjs/app/api/party/join/[id]/route.ts deleted file mode 100644 index 630d89c..0000000 --- a/nextjs/app/api/party/join/[id]/route.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { getServerSession } from "next-auth"; - -export async function GET( - req: Request, - { params }: { params: { id: string } }, -) { - const session = await getServerSession(); - if (!session || !session.user) { - return Response.json(null, { status: 401 }); - } - - return Response.json(null); -} diff --git a/nextjs/app/api/party/route.ts b/nextjs/app/api/party/route.ts index a7c2d74..2d123ea 100644 --- a/nextjs/app/api/party/route.ts +++ b/nextjs/app/api/party/route.ts @@ -30,17 +30,34 @@ export async function GET() { export async function POST() { const session = await getServerSession(); - if (!session || !session.user) { + if (!session || !session.user || !session.user.email) { return Response.json(null, { status: 401 }); } - const code = nanoid(); const party = await prisma.party.create({ data: { - code: code, + code: nanoid(), mode: Mode.CLASSIC, } as Party, }); - return Response.json(party); + await prisma.user.update({ + where: { + email: session.user.email, + }, + data: { + partyId: party.id, + }, + }); + + const res = await prisma.party.findUnique({ + where: { + id: party.id, + }, + include: { + players: true, + }, + }); + + return Response.json(res); }