47 lines
1.5 KiB
TypeScript
47 lines
1.5 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 PATCH(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
try {
|
|
await connectToDatabase();
|
|
const { id } = await params;
|
|
const payload = pitchingAppearanceSchema.parse(await request.json());
|
|
const appearance = await PitchingAppearanceModel.findByIdAndUpdate(
|
|
id,
|
|
{ ...payload, gameDate: new Date(payload.gameDate) },
|
|
{ new: true, runValidators: true }
|
|
).lean();
|
|
|
|
if (!appearance) {
|
|
return NextResponse.json({ error: "Pitching appearance not found." }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json(serialize(appearance));
|
|
} catch (error) {
|
|
return handleApiError(error);
|
|
}
|
|
}
|
|
|
|
export async function DELETE(_request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
try {
|
|
await connectToDatabase();
|
|
const { id } = await params;
|
|
const appearance = await PitchingAppearanceModel.findByIdAndDelete(id).lean();
|
|
|
|
if (!appearance) {
|
|
return NextResponse.json({ error: "Pitching appearance not found." }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
return handleApiError(error);
|
|
}
|
|
}
|