From 842e9c72bfb0e5f6396439d7a1e852ed8e68b401 Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Wed, 17 Apr 2024 12:02:51 -0700 Subject: [PATCH] Fixing wishlist, collection, and individual of those pages. --- src/lib/components/Game.svelte | 1 - .../(protected)/collections/+page.server.ts | 111 +++------- .../(protected)/collections/+page.svelte | 68 ++---- .../collections/[id]/+page.server.ts | 201 +++++++++++------- .../(protected)/wishlists/+page.server.ts | 72 +++---- .../(app)/(protected)/wishlists/+page.svelte | 40 ++-- .../wishlists/[id]/+page.server.ts | 5 +- 7 files changed, 225 insertions(+), 273 deletions(-) diff --git a/src/lib/components/Game.svelte b/src/lib/components/Game.svelte index 542f6fa..06bfdfc 100644 --- a/src/lib/components/Game.svelte +++ b/src/lib/components/Game.svelte @@ -4,7 +4,6 @@ import type { CollectionItems } from '../../schema'; export let game: GameType | CollectionItems; - export let detailed: boolean = false; export let variant: 'default' | 'compact' = 'default'; // Naive and assumes description is only on our GameType at the moment diff --git a/src/routes/(app)/(protected)/collections/+page.server.ts b/src/routes/(app)/(protected)/collections/+page.server.ts index e743aca..645fa50 100644 --- a/src/routes/(app)/(protected)/collections/+page.server.ts +++ b/src/routes/(app)/(protected)/collections/+page.server.ts @@ -2,7 +2,7 @@ import { type Actions, error, fail } from '@sveltejs/kit'; import { and, eq } from 'drizzle-orm'; import { superValidate } from 'sveltekit-superforms/server'; import { zod } from 'sveltekit-superforms/adapters'; -import { redirect } from 'sveltekit-flash-message/server' +import { redirect } from 'sveltekit-flash-message/server'; import { modifyListGameSchema, type ListGame } from '$lib/validations/zod-schemas'; import { search_schema } from '$lib/zodValidation.js'; import db from '$lib/drizzle'; @@ -10,93 +10,38 @@ import { collection_items, collections, games } from '../../../../schema'; import { notSignedInMessage } from '$lib/flashMessages'; export async function load(event) { - const { url, locals } = event; - const user = locals.user; + const user = event.locals.user; if (!user) { redirect(302, '/login', notSignedInMessage, event); } - // console.log('locals load', locals); - const searchParams = Object.fromEntries(url?.searchParams); - console.log('searchParams', searchParams); - const q = searchParams?.q; - const limit = parseInt(searchParams?.limit) || 10; - const skip = parseInt(searchParams?.skip) || 0; - - const searchData = { - q, - limit, - skip - }; - - const searchForm = await superValidate(searchData, zod(search_schema)); - const listManageForm = await superValidate(zod(modifyListGameSchema)); - try { - const collection = await db.query.collections.findFirst({ - where: eq(collections.user_id, user.id) + const userCollections = await db.query.collections.findMany({ + columns: { + cuid: true, + name: true, + created_at: true, + }, + where: eq(collections.user_id, user.id), }); - console.log('collection', collection); + console.log('collections', userCollections); - if (!collection) { + if (userCollections?.length === 0) { console.log('Collection was not found'); return fail(404, {}); - // collection = await prisma.collection.create({ - // data: { - // user_id: session.userId - // } - // }); - } - - const collectionItems = await db.query.collection_items.findMany({ - where: eq(collection_items.collection_id, collection.id), - with: { - game: { - columns: { - id: true, - name: true, - thumb_url: true - } - } - }, - offset: skip, - limit - }); - - console.log('collection_items', collectionItems); - - const items: ListGame[] = []; - for (const item of collectionItems) { - console.log('item', item); - const game = item.game; - if (game) { - let collectionItem: ListGame = { - id: game.id, - collection_id: item.collection_id, - name: game.name, - thumb_url: game.thumb_url, - times_played: item.times_played, - in_collection: true - }; - items.push(collectionItem); - } } return { - searchForm, - listManageForm, - collection: items + collections: userCollections, }; } catch (e) { console.error(e); } return { - searchForm, - listManageForm, - collection: [] + collections: [], }; -}; +} export const actions: Actions = { // Add game to a wishlist @@ -109,7 +54,7 @@ export const actions: Actions = { const user = event.locals.user; const game = await db.query.games.findFirst({ - where: eq(games.id, form.data.id) + where: eq(games.id, form.data.id), }); if (!game) { @@ -124,7 +69,7 @@ export const actions: Actions = { try { const collection = await db.query.collections.findFirst({ - where: eq(collections.user_id, user.id) + where: eq(collections.user_id, user.id), }); if (!collection) { @@ -135,11 +80,11 @@ export const actions: Actions = { await db.insert(collection_items).values({ game_id: game.id, collection_id: collection.id, - times_played: 0 + times_played: 0, }); return { - form + form, }; } catch (e) { console.error(e); @@ -170,7 +115,7 @@ export const actions: Actions = { } const game = await db.query.games.findFirst({ - where: eq(games.id, form.data.id) + where: eq(games.id, form.data.id), }); if (!game) { @@ -180,7 +125,7 @@ export const actions: Actions = { try { const collection = await db.query.collections.findFirst({ - where: eq(collections.user_id, locals.user.id) + where: eq(collections.user_id, locals.user.id), }); if (!collection) { @@ -188,17 +133,21 @@ export const actions: Actions = { return error(404, 'Collection not found'); } - await db.delete(collection_items).where(and( - eq(collection_items.collection_id, collection.id), - eq(collection_items.game_id, game.id) - )); + await db + .delete(collection_items) + .where( + and( + eq(collection_items.collection_id, collection.id), + eq(collection_items.game_id, game.id), + ), + ); return { - form + form, }; } catch (e) { console.error(e); return error(500, 'Something went wrong'); } - } + }, }; diff --git a/src/routes/(app)/(protected)/collections/+page.svelte b/src/routes/(app)/(protected)/collections/+page.svelte index 063f830..42f0e66 100644 --- a/src/routes/(app)/(protected)/collections/+page.svelte +++ b/src/routes/(app)/(protected)/collections/+page.svelte @@ -1,76 +1,40 @@ - Your Collection | Bored Game + Your Collections | Bored Game -

Your Collection

- +

Your Collections

-
-
- {#if collectionItems.length === 0} -

No games in your collection

+
+
+ {#if collections.length === 0} +

You have no collections

{:else} - {#each collectionItems as game (game.game_id)} - + {#each collections as collection} +
+

{collection.name}

+

Created at: {new Date(collection.created_at).toLocaleString()}

+
{/each} {/if}
-
\ No newline at end of file + diff --git a/src/routes/(app)/(protected)/wishlists/[id]/+page.server.ts b/src/routes/(app)/(protected)/wishlists/[id]/+page.server.ts index 579c1e2..a9730e4 100644 --- a/src/routes/(app)/(protected)/wishlists/[id]/+page.server.ts +++ b/src/routes/(app)/(protected)/wishlists/[id]/+page.server.ts @@ -10,6 +10,7 @@ import { games, wishlist_items, wishlists } from '../../../../../schema.js'; export async function load(event) { const { params, locals } = event; + const { id } = params; if (!locals.user) { redirect(302, '/login', notSignedInMessage, event); } @@ -17,8 +18,8 @@ export async function load(event) { console.log('Wishlist load User id', locals.user.id); try { - const wishlist = await db.query.wishlists.findFirst({ - where: eq(wishlists.user_id, locals.user.id), + const wishlist = await db.query.wishlists.findMany({ + where: and(eq(wishlists.user_id, locals.user.id), eq(wishlists.cuid, id)), }); if (!wishlist) {