diff --git a/nextjs/app/api/party/route.ts b/nextjs/app/api/party/route.ts index 767c3bd..a7c2d74 100644 --- a/nextjs/app/api/party/route.ts +++ b/nextjs/app/api/party/route.ts @@ -1,3 +1,5 @@ +import prisma from "@/lib/prisma"; +import { Mode, Party } from "@prisma/client"; import { customAlphabet } from "nanoid"; import { getServerSession } from "next-auth"; @@ -9,14 +11,36 @@ export async function GET() { return Response.json(null, { status: 401 }); } - return Response.json(null); + const user = await prisma.user.findUnique({ + where: { + email: session.user.email, + }, + include: { + party: { + include: { + winner: true, + players: true, + }, + }, + }, + }); + + return Response.json(user); } -export async function POST(req: Request) { +export async function POST() { const session = await getServerSession(); if (!session || !session.user) { return Response.json(null, { status: 401 }); } - return Response.json(null); + const code = nanoid(); + const party = await prisma.party.create({ + data: { + code: code, + mode: Mode.CLASSIC, + } as Party, + }); + + return Response.json(party); } diff --git a/nextjs/prisma/schema.prisma b/nextjs/prisma/schema.prisma index edc2473..7cdea2a 100644 --- a/nextjs/prisma/schema.prisma +++ b/nextjs/prisma/schema.prisma @@ -16,9 +16,14 @@ model User { pending Boolean @default(false) admin Boolean @default(false) - party Party? @relation("PartyPlayers", fields: [partyId], references: [id]) - partyId String? @map("party_id") @db.ObjectId - targetId String? @map("target_id") @db.ObjectId + party Party? @relation("PartyPlayers", fields: [partyId], references: [id]) + partyId String? @map("party_id") @db.ObjectId + + target User? @relation("UserTarget", fields: [targetId], references: [id], onDelete: NoAction, onUpdate: NoAction) + targetId String? @map("target_id") @db.ObjectId + targetedBy User[] @relation("UserTarget") + + parties Party[] @@map("users") } @@ -29,7 +34,9 @@ model Party { started Boolean @default(false) mode Mode @default(CLASSIC) - players User[] @relation("PartyPlayers") + players User[] @relation("PartyPlayers") + + winner User? @relation(fields: [winnerId], references: [id], onDelete: NoAction, onUpdate: NoAction) winnerId String? @map("winner_id") @db.ObjectId @@map("parties")