Fixing search form.

This commit is contained in:
Bradley Shellnut 2024-03-03 11:32:36 -08:00
parent b25be86d32
commit 8069db626f
3 changed files with 109 additions and 96 deletions

View file

@ -1,38 +1,54 @@
<script lang="ts"> <script lang="ts">
import type { SuperValidated } from 'sveltekit-superforms'; import { superForm, type Infer, type SuperValidated } from 'sveltekit-superforms';
import { search_schema, type SearchSchema } from '$lib/zodValidation'; import { search_schema, type SearchSchema } from '$lib/zodValidation';
import * as Form from "$lib/components/ui/form"; import * as Form from "$lib/components/ui/form";
import { zodClient } from 'sveltekit-superforms/adapters';
import Input from '$components/ui/input/input.svelte';
import Checkbox from '$components/ui/checkbox/checkbox.svelte';
export let form: SuperValidated<SearchSchema>; export let data: SuperValidated<Infer<SearchSchema>>;
const form = superForm(data, {
validators: zodClient(search_schema),
});
const { form: formData } = form;
</script> </script>
<search> <search>
<Form.Root id="search-form" action="/search" method="GET" data-sveltekit-reload {form} schema={search_schema} let:config> <form id="search-form" action="/search" method="GET" data-sveltekit-reload>
<fieldset> <fieldset>
<Form.Item> <Form.Field {form} name="q">
<Form.Field {config} name="q"> <Form.Control let:attrs>
<Form.Label for="label">Search</Form.Label> <Form.Label>Search</Form.Label>
<Form.Input /> <Input {...attrs} bind:value={$formData.q} />
<Form.Validation /> </Form.Control>
</Form.Field> <Form.FieldErrors />
<Form.Field {config} name="skip"> </Form.Field>
<Form.Input type="hidden" /> <Form.Field {form} name="skip">
</Form.Field> <Form.Control let:attrs>
<Form.Field {config} name="limit"> <Input type="hidden" />
<Form.Input type="hidden" /> </Form.Control>
</Form.Field> </Form.Field>
</Form.Item> <Form.Field {form} name="limit">
<Form.Control let:attrs>
<Input type="hidden" />
</Form.Control>
</Form.Field>
</fieldset> </fieldset>
<fieldset> <fieldset>
<div class="flex items-center space-x-2"> <div class="flex items-center space-x-2">
<Form.Field {config} name="exact"> <Form.Field {form} name="exact">
<Form.Label>Exact Search</Form.Label> <Form.Control let:attrs>
<Form.Checkbox class="mt-0" /> <Form.Label>Exact Search</Form.Label>
<Checkbox {...attrs} class="mt-0" bind:checked={$formData.exact} />
<input name={attrs.name} value={$formData.exact} hidden />
</Form.Control>
</Form.Field> </Form.Field>
</div> </div>
</fieldset> </fieldset>
<Form.Button>Submit</Form.Button> <Form.Button>Submit</Form.Button>
</Form.Root> </form>
</search> </search>
<style lang="postcss"> <style lang="postcss">

View file

@ -1,52 +1,52 @@
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
import type { Game } from '@prisma/client';
import {
createArtist,
createCategory,
createDesigner,
createExpansion,
createMechanic,
createOrUpdateGame,
createPublisher
} from '$lib/utils/db/dbUtils.js';
import { mapAPIGameToBoredGame } from '$lib/utils/gameMapper.js'; import { mapAPIGameToBoredGame } from '$lib/utils/gameMapper.js';
import prisma from '$lib/prisma';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import { createCategory } from '$lib/utils/db/categoryUtils';
import { createMechanic } from '$lib/utils/db/mechanicUtils';
import { createPublisher } from '$lib/utils/db/publisherUtils';
import { createExpansion } from '$lib/utils/db/expansionUtils';
import { createOrUpdateGame } from '$lib/utils/db/gameUtils';
import db from '$lib/drizzle';
import { and, eq } from 'drizzle-orm';
import { collection_items, collections, expansions, games, wishlist_items, wishlists } from '../../../../schema';
export const load: PageServerLoad = async ({ params, locals, fetch }) => { export const load: PageServerLoad = async ({ params, locals, fetch }) => {
try { try {
const { user } = locals; const { user } = locals;
const { id } = params; const { id } = params;
const game = await prisma.game.findUnique({ const game = await db.query.games.findFirst({
where: { where: eq(games.id, id),
id with: {
}, publishers_to_games: {
include: { with: {
artists: true, publisher: {
designers: true, columns: {
publishers: true,
mechanics: true,
categories: true,
expansions: {
include: {
game: {
select: {
id: true, id: true,
name: true name: true
} }
} }
} }
}, },
expansion_of: { mechanics_to_games: {
include: { with: {
base_game: { mechanic: {
select: { columns: {
id: true, id: true,
name: true name: true
} }
} }
} }
} },
categories_to_games: {
with: {
category: {
columns: {
id: true,
name: true
}
}
}
},
} }
}); });
console.log('found game', game); console.log('found game', game);
@ -64,45 +64,54 @@ export const load: PageServerLoad = async ({ params, locals, fetch }) => {
await syncGameAndConnectedData(locals, game, fetch); await syncGameAndConnectedData(locals, game, fetch);
} }
let wishlist; const gameExpansions = await db.query.expansions.findMany({
let collection; where: eq(expansions.base_game_id, game.id),
if (user) { with: {
wishlist = await prisma.wishlist.findUnique({ game: {
where: { columns: {
user_id: user.id id: true,
}, name: true,
include: { thumb_url: true
items: {
where: {
game_id: game.id
}
} }
} }
}
});
let collectionItem;
let wishlistItem;
if (user) {
const wishlist = await db.query.wishlists.findFirst({
where: eq(wishlists.user_id, user.id),
}); });
collection = await prisma.collection.findUnique({ // TODO: Select wishlist items based on wishlist
where: { if (wishlist) {
user_id: user.id wishlistItem = await db.query.wishlist_items.findFirst({
}, where: and(eq(wishlist_items.wishlist_id, wishlist.id), eq(wishlist_items.game_id, game.id)),
include: { })
items: { }
where: {
game_id: game.id const collection = await db.query.collections.findFirst({
} where: eq(collections.user_id, user.id),
}
}
}); });
// TODO: Select collection items based on collection
if (collection) {
collectionItem = await db.query.collection_items.findFirst({
where: and(eq(collection_items.collection_id, collection.id), eq(collection_items.game_id, game.id)),
})
}
} }
console.log('Returning game', game); console.log('Returning game', game);
return { return {
game, game,
expansions: gameExpansions,
user, user,
in_wishlist: wishlist?.items?.length !== 0 || false, in_wishlist: wishlistItem !== undefined || false,
in_collection: collection?.items?.length !== 0 || false, in_collection: collectionItem !== undefined || false,
wishlist,
collection
}; };
} catch (error) { } catch (error) {
console.log(error); console.log(error);
@ -121,38 +130,28 @@ async function syncGameAndConnectedData(locals: App.Locals, game: Game, eventFet
console.log('externalGame', externalGame); console.log('externalGame', externalGame);
const categories = []; const categories = [];
const mechanics = []; const mechanics = [];
const artists = [];
const designers = [];
const publishers = []; const publishers = [];
for (const externalCategory of externalGame.categories) { for (const externalCategory of externalGame.categories) {
const category = await createCategory(locals, externalCategory); const category = await createCategory(locals, externalCategory, externalGame.external_id);
categories.push({ categories.push({
id: category.id id: category.id
}); });
} }
for (const externalMechanic of externalGame.mechanics) { for (const externalMechanic of externalGame.mechanics) {
const mechanic = await createMechanic(locals, externalMechanic); const mechanic = await createMechanic(locals, externalMechanic, externalGame.external_id);
mechanics.push({ id: mechanic.id }); mechanics.push({ id: mechanic.id });
} }
for (const externalArtist of externalGame.artists) {
const artist = await createArtist(locals, externalArtist);
artists.push({ id: artist.id });
}
for (const externalDesigner of externalGame.designers) {
const designer = await createDesigner(locals, externalDesigner);
designers.push({ id: designer.id });
}
for (const externalPublisher of externalGame.publishers) { for (const externalPublisher of externalGame.publishers) {
const publisher = await createPublisher(locals, externalPublisher); const publisher = await createPublisher(locals, externalPublisher, externalGame.external_id);
publishers.push({ id: publisher.id }); publishers.push({ id: publisher.id });
} }
for (const externalExpansion of externalGame.expansions) { for (const externalExpansion of externalGame.expansions) {
console.log('Inbound?', externalExpansion.inbound); console.log('Inbound?', externalExpansion.inbound);
if (externalExpansion?.inbound === true) { if (externalExpansion?.inbound === true) {
createExpansion(locals, game, externalExpansion, false, eventFetch); createExpansion(locals, externalExpansion);
} else { } else {
createExpansion(locals, game, externalExpansion, true, eventFetch); createExpansion(locals,externalExpansion);
} }
} }
@ -160,10 +159,8 @@ async function syncGameAndConnectedData(locals: App.Locals, game: Game, eventFet
boredGame.categories = categories; boredGame.categories = categories;
boredGame.mechanics = mechanics; boredGame.mechanics = mechanics;
boredGame.designers = designers;
boredGame.artists = artists;
boredGame.publishers = publishers; boredGame.publishers = publishers;
// boredGame.expansions = expansions; // boredGame.expansions = expansions;
return createOrUpdateGame(locals, boredGame); return createOrUpdateGame(locals, boredGame, externalGame.external_id);
} }
} }

View file

@ -36,7 +36,7 @@
<SuperDebug collapsible data={data.form} /> <SuperDebug collapsible data={data.form} />
{/if} {/if}
<GameSearchForm form={data.form} /> <GameSearchForm data={data.form} />
<section class="games"> <section class="games">
<div> <div>