diff --git a/src/routes/racepicks/+page.server.ts b/src/routes/racepicks/+page.server.ts index 016bcd1..84647dd 100644 --- a/src/routes/racepicks/+page.server.ts +++ b/src/routes/racepicks/+page.server.ts @@ -1,18 +1,49 @@ -import type { RacePick } from "$lib/schema"; +import type { Driver, Race, RacePick, RaceResult } from "$lib/schema"; import type { PageServerLoad } from "./$types"; export const load: PageServerLoad = async ({ fetch, locals }) => { const fetch_racepicks = async (): Promise => { - // TODO: What is faster, expanding everything or filling in using individual requests? - // Probably expanding everything directly... + // PXX/DNF is not expanded, as we have the drivers anyways (for guessing) const racepicks: RacePick[] = await locals.pb .collection("racepicks") - .getFullList({ fetch: fetch, expand: "user,race,pxx,dnf" }); + .getFullList({ fetch: fetch, expand: "user,race" }); return racepicks; }; + const fetch_currentrace = async (): Promise => { + const currentrace: Race[] = await locals.pb.collection("currentrace").getFullList(); + + // The currentrace collection either has a single or no entries + return currentrace[0] ?? null; + }; + + const fetch_raceresults = async (): Promise => { + const raceresults: RaceResult[] = await locals.pb + .collection("raceresults") + .getFullList({ expand: "race,pxxs,dnfs" }); + + return raceresults; + }; + + // TODO: Duplicated code from data/season/+layout.server.ts + const fetch_drivers = async (): Promise => { + const drivers: Driver[] = await locals.pb.collection("drivers").getFullList({ + sort: "+code", + fetch: fetch, + }); + + drivers.map((driver: Driver) => { + driver.headshot_url = locals.pb.files.getURL(driver, driver.headshot); + }); + + return drivers; + }; + return { racepicks: await fetch_racepicks(), + currentrace: await fetch_currentrace(), + raceresults: await fetch_raceresults(), + drivers: await fetch_drivers(), }; };