Racepicks: Implement create/update/delete routes
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import type { Driver, Graphic, Race, RacePick, RaceResult } from "$lib/schema";
|
||||
import type { PageServerLoad } from "./$types";
|
||||
import { form_data_clean, form_data_ensure_keys, form_data_get_and_remove_id } from "$lib/form";
|
||||
import type { CurrentPickedUser, Driver, Graphic, Race, RacePick, RaceResult } from "$lib/schema";
|
||||
import type { Actions, PageServerLoad } from "./$types";
|
||||
|
||||
export const load: PageServerLoad = async ({ fetch, locals }) => {
|
||||
const fetch_racepicks = async (): Promise<RacePick[]> => {
|
||||
@ -27,6 +28,23 @@ export const load: PageServerLoad = async ({ fetch, locals }) => {
|
||||
return currentrace[0];
|
||||
};
|
||||
|
||||
// const fetch_currentpickedusers = async (): Promise<CurrentPickedUser[]> => {
|
||||
// const currentpickedusers: CurrentPickedUser[] = await locals.pb
|
||||
// .collection("currentpickedusers")
|
||||
// .getFullList();
|
||||
//
|
||||
// currentpickedusers.map((currentpickeduser: CurrentPickedUser) => {
|
||||
// if (currentpickeduser.avatar) {
|
||||
// currentpickeduser.avatar_url = locals.pb.files.getURL(
|
||||
// currentpickeduser,
|
||||
// currentpickeduser.avatar,
|
||||
// );
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// return currentpickedusers;
|
||||
// };
|
||||
|
||||
const fetch_raceresults = async (): Promise<RaceResult[]> => {
|
||||
// Don't expand races/pxxs/dnfs since we already fetched those
|
||||
const raceresults: RaceResult[] = await locals.pb.collection("raceresults").getFullList();
|
||||
@ -78,9 +96,41 @@ export const load: PageServerLoad = async ({ fetch, locals }) => {
|
||||
return {
|
||||
racepicks: await fetch_racepicks(),
|
||||
currentrace: await fetch_currentrace(),
|
||||
// currentpickedusers: await fetch_currentpickedusers(),
|
||||
raceresults: await fetch_raceresults(),
|
||||
drivers: await fetch_drivers(),
|
||||
races: await fetch_races(),
|
||||
graphics: await fetch_graphics(),
|
||||
};
|
||||
};
|
||||
|
||||
export const actions = {
|
||||
create_racepick: async ({ request, locals }) => {
|
||||
const data: FormData = form_data_clean(await request.formData());
|
||||
form_data_ensure_keys(data, ["user", "race", "pxx", "dnf"]);
|
||||
|
||||
if (locals.user?.id !== data.get("user")) return { unauthorized: true };
|
||||
|
||||
await locals.pb.collection("racepicks").create(data);
|
||||
},
|
||||
|
||||
update_racepick: async ({ request, locals }) => {
|
||||
const data: FormData = form_data_clean(await request.formData());
|
||||
form_data_ensure_keys(data, ["user", "race"]);
|
||||
const id: string = form_data_get_and_remove_id(data);
|
||||
|
||||
if (locals.user?.id !== data.get("user")) return { unauthorized: true };
|
||||
|
||||
await locals.pb.collection("racepicks").update(id, data);
|
||||
},
|
||||
|
||||
delete_racepick: async ({ request, locals }) => {
|
||||
const data: FormData = form_data_clean(await request.formData());
|
||||
form_data_ensure_keys(data, ["user", "race"]);
|
||||
const id: string = form_data_get_and_remove_id(data);
|
||||
|
||||
if (locals.user?.id !== data.get("user")) return { unauthorized: true };
|
||||
|
||||
await locals.pb.collection("racepicks").delete(id);
|
||||
},
|
||||
} satisfies Actions;
|
||||
|
Reference in New Issue
Block a user