mirror of
https://github.com/BradNut/boredgame
synced 2025-09-08 17:40:22 +00:00
Adding more game util methods and add game pages.
This commit is contained in:
parent
c472f43b9e
commit
39ecce591a
8 changed files with 92 additions and 1 deletions
|
|
@ -5,6 +5,23 @@ import { eq } from 'drizzle-orm';
|
||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
import { PUBLIC_SITE_URL } from '$env/static/public';
|
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) {
|
export async function createGame(locals: App.Locals, game: Games, externalId: string) {
|
||||||
if (!game || !externalId || externalId === '') {
|
if (!game || !externalId || externalId === '') {
|
||||||
error(400, 'Invalid Request');
|
error(400, 'Invalid Request');
|
||||||
|
|
|
||||||
|
|
@ -117,6 +117,10 @@ export const search_schema = z
|
||||||
|
|
||||||
export type SearchSchema = typeof search_schema;
|
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({
|
export const collection_search_schema = Search.extend({
|
||||||
collection_id: z.string()
|
collection_id: z.string()
|
||||||
});
|
});
|
||||||
|
|
|
||||||
11
src/routes/(app)/(protected)/collection/add/+page.server.ts
Normal file
11
src/routes/(app)/(protected)/collection/add/+page.server.ts
Normal 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');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
10
src/routes/(app)/(protected)/collection/add/+page.svelte
Normal file
10
src/routes/(app)/(protected)/collection/add/+page.svelte
Normal 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} />
|
||||||
|
|
@ -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 };
|
||||||
|
}
|
||||||
14
src/routes/(app)/(protected)/collection/add/bgg/+page.svelte
Normal file
14
src/routes/(app)/(protected)/collection/add/bgg/+page.svelte
Normal 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>
|
||||||
|
|
@ -20,7 +20,7 @@ import {
|
||||||
|
|
||||||
async function searchForGames(
|
async function searchForGames(
|
||||||
locals: App.Locals,
|
locals: App.Locals,
|
||||||
eventFetch: Function,
|
eventFetch,
|
||||||
urlQueryParams: SearchQuery
|
urlQueryParams: SearchQuery
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue