From 39ecce591aa15525aee7326c4a03d75d90c4faf7 Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Tue, 20 Feb 2024 23:31:05 -0800 Subject: [PATCH] Adding more game util methods and add game pages. --- src/lib/utils/db/gameUtils.ts | 17 ++++++++++++++++ src/lib/zodValidation.ts | 4 ++++ .../collection/add/+page.server.ts | 11 ++++++++++ .../(protected)/collection/add/+page.svelte | 10 ++++++++++ .../collection/add/bgg/+page.server.ts | 15 ++++++++++++++ .../collection/add/bgg/+page.svelte | 14 +++++++++++++ src/routes/(app)/search/+page.server.ts | 2 +- src/routes/api/game/[id]/+server.ts | 20 +++++++++++++++++++ 8 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/routes/(app)/(protected)/collection/add/+page.server.ts create mode 100644 src/routes/(app)/(protected)/collection/add/+page.svelte create mode 100644 src/routes/(app)/(protected)/collection/add/bgg/+page.server.ts create mode 100644 src/routes/(app)/(protected)/collection/add/bgg/+page.svelte diff --git a/src/lib/utils/db/gameUtils.ts b/src/lib/utils/db/gameUtils.ts index 7ae301a..cef1edc 100644 --- a/src/lib/utils/db/gameUtils.ts +++ b/src/lib/utils/db/gameUtils.ts @@ -5,6 +5,23 @@ import { eq } from 'drizzle-orm'; import { error } from '@sveltejs/kit'; import { PUBLIC_SITE_URL } from '$env/static/public'; +export async function getGame(locals: App.Locals, id: string) { + if (!id || id === '') { + error(400, 'Invalid Request'); + } + + try { + return await db.query.games.findFirst({ + where: eq(games.id, id) + }); + } catch (e) { + console.error(e); + return new Response('Could not get games', { + status: 500 + }); + } +} + export async function createGame(locals: App.Locals, game: Games, externalId: string) { if (!game || !externalId || externalId === '') { error(400, 'Invalid Request'); diff --git a/src/lib/zodValidation.ts b/src/lib/zodValidation.ts index 8fbac7d..e66ce7d 100644 --- a/src/lib/zodValidation.ts +++ b/src/lib/zodValidation.ts @@ -117,6 +117,10 @@ export const search_schema = z export type SearchSchema = typeof search_schema; +export const BggForm = z.object({ + link: z.string().trim().startsWith('https://boardgamegeek.com/boardgame/') +}) + export const collection_search_schema = Search.extend({ collection_id: z.string() }); diff --git a/src/routes/(app)/(protected)/collection/add/+page.server.ts b/src/routes/(app)/(protected)/collection/add/+page.server.ts new file mode 100644 index 0000000..9388402 --- /dev/null +++ b/src/routes/(app)/(protected)/collection/add/+page.server.ts @@ -0,0 +1,11 @@ +import { redirect } from "@sveltejs/kit"; +import type { PageServerLoad } from "../$types"; + +export const load: PageServerLoad = async ({ locals, fetch }) => { + const user = locals.user; + if (!user) { + redirect(302, '/login'); + } + + +} \ No newline at end of file diff --git a/src/routes/(app)/(protected)/collection/add/+page.svelte b/src/routes/(app)/(protected)/collection/add/+page.svelte new file mode 100644 index 0000000..201b843 --- /dev/null +++ b/src/routes/(app)/(protected)/collection/add/+page.svelte @@ -0,0 +1,10 @@ + +

Add a game to your collection

+ +

Board Game Geek URL

+ + diff --git a/src/routes/(app)/(protected)/collection/add/bgg/+page.server.ts b/src/routes/(app)/(protected)/collection/add/bgg/+page.server.ts new file mode 100644 index 0000000..edd4dca --- /dev/null +++ b/src/routes/(app)/(protected)/collection/add/bgg/+page.server.ts @@ -0,0 +1,15 @@ +import { redirect } from "@sveltejs/kit"; +import type { PageServerLoad } from "../$types"; +import { BggForm } from "$lib/zodValidation"; +import { superValidate } from "sveltekit-superforms/server"; + +export const load: PageServerLoad = async ({ locals, fetch }) => { + const user = locals.user; + if (!user) { + redirect(302, '/login'); + } + + const form = await superValidate({}, BggForm); + + return { form }; +} \ No newline at end of file diff --git a/src/routes/(app)/(protected)/collection/add/bgg/+page.svelte b/src/routes/(app)/(protected)/collection/add/bgg/+page.svelte new file mode 100644 index 0000000..80b0154 --- /dev/null +++ b/src/routes/(app)/(protected)/collection/add/bgg/+page.svelte @@ -0,0 +1,14 @@ + +

Add a game to your collection

+ +
+ + + +
diff --git a/src/routes/(app)/search/+page.server.ts b/src/routes/(app)/search/+page.server.ts index ab1e9eb..910b641 100644 --- a/src/routes/(app)/search/+page.server.ts +++ b/src/routes/(app)/search/+page.server.ts @@ -20,7 +20,7 @@ import { async function searchForGames( locals: App.Locals, - eventFetch: Function, + eventFetch, urlQueryParams: SearchQuery ) { try { diff --git a/src/routes/api/game/[id]/+server.ts b/src/routes/api/game/[id]/+server.ts index e69de29..afe3581 100644 --- a/src/routes/api/game/[id]/+server.ts +++ b/src/routes/api/game/[id]/+server.ts @@ -0,0 +1,20 @@ +import { error, json } from '@sveltejs/kit'; +import { getGame } from '$lib/utils/db/gameUtils.js'; + +export const GET = async ({ locals, params }) => { + const game_id = Number(params.id).valueOf(); + + // TODO: Debounce excessive calls and possibly throttle + if (isNaN(game_id) || !isFinite(game_id)) { + error(400, { message: 'Invalid game id' }); + } + + try { + return json(await getGame(locals, params.id)); + } catch (e) { + console.error(e); + return new Response('Could not get games', { + status: 500 + }); + } +} \ No newline at end of file