Adding more game util methods and add game pages.

This commit is contained in:
Bradley Shellnut 2024-02-20 23:31:05 -08:00
parent c472f43b9e
commit 39ecce591a
8 changed files with 92 additions and 1 deletions

View file

@ -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');

View file

@ -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()
});

View file

@ -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');
}
}

View file

@ -0,0 +1,10 @@
<script lang="ts">
import GameSearchForm from "$components/search/GameSearchForm.svelte";
export let data;
</script>
<h1>Add a game to your collection</h1>
<h2>Board Game Geek URL</h2>
<GameSearchForm form={data.form} />

View file

@ -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 };
}

View file

@ -0,0 +1,14 @@
<script lang="ts">
import GameSearchForm from "$components/search/GameSearchForm.svelte";
import Input from "$components/ui/input/input.svelte";
import Label from "$components/ui/label/label.svelte";
export let data;
</script>
<h1>Add a game to your collection</h1>
<form method="POST" action="/collection/add/bgg">
<Label for="url">Board Game Geek URL</Label>
<Input type="url" id="url" name="url" placeholder="https://boardgamegeek.com/boardgame/123"/>
<button type="submit">Add Game</button>
</form>

View file

@ -20,7 +20,7 @@ import {
async function searchForGames(
locals: App.Locals,
eventFetch: Function,
eventFetch,
urlQueryParams: SearchQuery
) {
try {

View file

@ -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
});
}
}