26 lines
962 B
TypeScript
26 lines
962 B
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 rounds = await db.round.findMany({ orderBy: { number: "asc" } });
|
|
return NextResponse.json(rounds);
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session || (session.user as any).role !== "ADMIN")
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
|
|
const { number, name, deadline } = await req.json();
|
|
|
|
const existing = await db.round.findUnique({ where: { number } });
|
|
if (existing) return NextResponse.json({ error: "این شماره دور قبلاً ثبت شده" }, { status: 400 });
|
|
|
|
const round = await db.round.create({
|
|
data: { number, name, deadline: new Date(deadline) },
|
|
});
|
|
return NextResponse.json(round, { status: 201 });
|
|
}
|