39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { handleApiError } from "@/app/api/_utils";
|
|
import { connectToDatabase } from "@/lib/db";
|
|
import { serialize } from "@/lib/serialize";
|
|
import { pitchingRulesSchema } from "@/lib/validators";
|
|
import { PitchingRuleModel } from "@/models/PitchingRule";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function GET() {
|
|
try {
|
|
await connectToDatabase();
|
|
const rules = await PitchingRuleModel.find().sort({ sortOrder: 1 }).lean();
|
|
return NextResponse.json(serialize(rules));
|
|
} catch (error) {
|
|
return handleApiError(error);
|
|
}
|
|
}
|
|
|
|
export async function PUT(request: Request) {
|
|
try {
|
|
await connectToDatabase();
|
|
const payload = pitchingRulesSchema.parse(await request.json());
|
|
|
|
await PitchingRuleModel.deleteMany({});
|
|
const rules = await PitchingRuleModel.insertMany(
|
|
payload.map((rule, index) => ({
|
|
...rule,
|
|
sortOrder: index
|
|
}))
|
|
);
|
|
|
|
return NextResponse.json(serialize(rules));
|
|
} catch (error) {
|
|
return handleApiError(error);
|
|
}
|
|
}
|