This commit is contained in:
Zeeshaun
2026-05-06 16:10:07 +00:00
parent 2ed854389c
commit 02186dcdfa
15 changed files with 557 additions and 189 deletions
+8 -8
View File
@@ -15,7 +15,7 @@ export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const gameDate = searchParams.get("gameDate");
const filter = gameDate ? { gameDate: new Date(gameDate) } : {};
const lineups = await LineupModel.find(filter).sort({ gameDate: -1 }).lean();
const lineups = await LineupModel.find(filter).sort({ gameDate: -1, lineupNumber: -1 }).lean();
return NextResponse.json(serialize(lineups));
} catch (error) {
return handleApiError(error);
@@ -26,8 +26,12 @@ export async function POST(request: Request) {
try {
await connectToDatabase();
const payload = saveLineupSchema.parse(await request.json());
const gameDate = new Date(payload.gameDate);
const latestForDate = await LineupModel.findOne({ gameDate }).sort({ lineupNumber: -1 }).select("lineupNumber").lean();
const lineupNumber = (latestForDate?.lineupNumber ?? 0) + 1;
const lineupPayload = {
gameDate: new Date(payload.gameDate),
gameDate,
lineupNumber,
playerIds: payload.playerIds,
startingPitcherId: payload.startingPitcherId ?? null,
battingOrder: payload.battingOrder,
@@ -36,11 +40,7 @@ export async function POST(request: Request) {
notes: payload.notes
};
const lineup = await LineupModel.findOneAndUpdate({ gameDate: lineupPayload.gameDate }, lineupPayload, {
upsert: true,
new: true,
runValidators: true
}).lean();
const lineup = await LineupModel.create(lineupPayload);
await GameModel.findOneAndUpdate(
{ gameDate: lineupPayload.gameDate },
@@ -52,7 +52,7 @@ export async function POST(request: Request) {
{ upsert: true, new: true }
);
return NextResponse.json(serialize(lineup), { status: 201 });
return NextResponse.json(serialize(lineup.toObject()), { status: 201 });
} catch (error) {
return handleApiError(error);
}