38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { handleApiError } from "@/app/api/_utils";
|
|
import { connectToDatabase } from "@/lib/db";
|
|
import { serialize } from "@/lib/serialize";
|
|
import { pitchingAppearanceSchema } from "@/lib/validators";
|
|
import { PitchingAppearanceModel } from "@/models/PitchingAppearance";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
await connectToDatabase();
|
|
const { searchParams } = new URL(request.url);
|
|
const playerId = searchParams.get("playerId");
|
|
const filter = playerId ? { playerId } : {};
|
|
const appearances = await PitchingAppearanceModel.find(filter).sort({ gameDate: -1 }).lean();
|
|
return NextResponse.json(serialize(appearances));
|
|
} catch (error) {
|
|
return handleApiError(error);
|
|
}
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
await connectToDatabase();
|
|
const payload = pitchingAppearanceSchema.parse(await request.json());
|
|
const appearance = await PitchingAppearanceModel.create({
|
|
...payload,
|
|
gameDate: new Date(payload.gameDate)
|
|
});
|
|
|
|
return NextResponse.json(serialize(appearance), { status: 201 });
|
|
} catch (error) {
|
|
return handleApiError(error);
|
|
}
|
|
}
|