From d4e5e12b5f3fd785e7d1f3dcc174fc6d44768e6b Mon Sep 17 00:00:00 2001 From: minhd-vu Date: Sat, 10 Feb 2024 03:49:49 -0500 Subject: [PATCH] update routing structure --- nextjs/app/api/party/[id]/join/route.ts | 43 +++++++++++++++++++++++++ nextjs/app/api/party/join/[id]/route.ts | 13 -------- nextjs/app/api/party/route.ts | 25 +++++++++++--- 3 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 nextjs/app/api/party/[id]/join/route.ts delete mode 100644 nextjs/app/api/party/join/[id]/route.ts 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); }