first commit
This commit is contained in:
34
app/api/team/route.ts
Normal file
34
app/api/team/route.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db } from "@/lib/db";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "@/lib/auth";
|
||||
|
||||
export async function GET() {
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const team = await db.team.findUnique({
|
||||
where: { userId: (session.user as any).id },
|
||||
include: {
|
||||
players: {
|
||||
include: { player: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(team);
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
|
||||
const { name } = await req.json();
|
||||
const userId = (session.user as any).id;
|
||||
|
||||
const existing = await db.team.findUnique({ where: { userId } });
|
||||
if (existing) return NextResponse.json({ error: "Team already exists" }, { status: 400 });
|
||||
|
||||
const team = await db.team.create({ data: { name, userId } });
|
||||
return NextResponse.json(team, { status: 201 });
|
||||
}
|
||||
Reference in New Issue
Block a user