45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
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, formation } = await req.json();
|
|
const userId = (session.user as any).id;
|
|
|
|
// بررسی وجود کاربر
|
|
const user = await db.user.findUnique({ where: { id: userId } });
|
|
if (!user) return NextResponse.json({ error: "User not found" }, { status: 404 });
|
|
|
|
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,
|
|
formation: formation || "4-3-3"
|
|
}
|
|
});
|
|
return NextResponse.json(team, { status: 201 });
|
|
}
|