Updating
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { handleApiError } from "@/app/api/_utils";
|
||||
import { connectToDatabase } from "@/lib/db";
|
||||
import { serialize } from "@/lib/serialize";
|
||||
import { saveGameSchema } from "@/lib/validators";
|
||||
import { GameModel } from "@/models/Game";
|
||||
import { LineupModel } from "@/models/Lineup";
|
||||
import { PlayerModel } from "@/models/Player";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const statFields = [
|
||||
"atBats",
|
||||
"runs",
|
||||
"hits",
|
||||
"doubles",
|
||||
"triples",
|
||||
"homeRuns",
|
||||
"runsBattedIn",
|
||||
"walks",
|
||||
"strikeouts",
|
||||
"stolenBases"
|
||||
] as const;
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
await connectToDatabase();
|
||||
const { searchParams } = new URL(request.url);
|
||||
const gameDate = searchParams.get("gameDate");
|
||||
const filter = gameDate ? { gameDate: new Date(gameDate) } : {};
|
||||
const games = await GameModel.find(filter).sort({ gameDate: -1, gameNumber: -1 }).lean();
|
||||
return NextResponse.json(serialize(games));
|
||||
} catch (error) {
|
||||
return handleApiError(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
await connectToDatabase();
|
||||
const payload = saveGameSchema.parse(await request.json());
|
||||
const lineup = await LineupModel.findById(payload.lineupId).lean();
|
||||
|
||||
if (!lineup) {
|
||||
return NextResponse.json({ error: "Selected lineup not found." }, { status: 404 });
|
||||
}
|
||||
|
||||
const gameDate = new Date(payload.gameDate);
|
||||
const latestForDate = await GameModel.findOne({ gameDate }).sort({ gameNumber: -1 }).select("gameNumber").lean();
|
||||
const gameNumber = (latestForDate?.gameNumber ?? 0) + 1;
|
||||
|
||||
const game = await GameModel.create({
|
||||
gameDate,
|
||||
gameNumber,
|
||||
activePlayerIds: lineup.playerIds,
|
||||
startingPitcherId: lineup.startingPitcherId ?? null,
|
||||
lineupId: lineup._id,
|
||||
lineupNumber: lineup.lineupNumber ?? null,
|
||||
opponent: payload.opponent ?? "",
|
||||
teamScore: payload.teamScore ?? null,
|
||||
opponentScore: payload.opponentScore ?? null,
|
||||
notes: payload.notes ?? "",
|
||||
playerStats: payload.playerStats
|
||||
});
|
||||
|
||||
await PlayerModel.bulkWrite(
|
||||
payload.playerStats.map((playerStat) => {
|
||||
const increment: Record<string, number> = { "stats.gamesPlayed": 1 };
|
||||
|
||||
for (const field of statFields) {
|
||||
increment[`stats.${field}`] = playerStat[field] ?? 0;
|
||||
}
|
||||
|
||||
return {
|
||||
updateOne: {
|
||||
filter: { _id: playerStat.playerId },
|
||||
update: { $inc: increment }
|
||||
}
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return NextResponse.json(serialize(game.toObject()), { status: 201 });
|
||||
} catch (error) {
|
||||
return handleApiError(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user