Updating
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user