From a1fa2796ef037b5351e0e63907b8e62209ba8054 Mon Sep 17 00:00:00 2001 From: Nulo Date: Sun, 4 Aug 2024 12:55:20 -0300 Subject: [PATCH] sitio: adaptar homepage para usar api --- pnpm-lock.yaml | 3 -- sitio/src/lib/ProductPreview.svelte | 6 +-- sitio/src/routes/+page.server.ts | 83 ++++++++--------------------- 3 files changed, 24 insertions(+), 68 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4700fbd..686162e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -39,9 +39,6 @@ importers: dayjs: specifier: ^1.11.10 version: 1.11.10 - drizzle-kit: - specifier: ^0.23.0 - version: 0.23.0 drizzle-orm: specifier: ^0.32.0 version: 0.32.0(@types/better-sqlite3@7.6.9)(better-sqlite3@11.1.2) diff --git a/sitio/src/lib/ProductPreview.svelte b/sitio/src/lib/ProductPreview.svelte index f2b3bae..6e05d7f 100644 --- a/sitio/src/lib/ProductPreview.svelte +++ b/sitio/src/lib/ProductPreview.svelte @@ -1,5 +1,5 @@ - {#if product.imageUrl} + {#if product.image_url} {product.name} = doQuery(); - -async function doQuery() { - const db = await getDb(); - - const categories = await db - .select({ - fetchedAt: bestSelling.fetchedAt, - category: bestSelling.category, - eansJson: bestSelling.eansJson, - }) - .from(bestSelling) - .groupBy(bestSelling.category) - .having(max(bestSelling.fetchedAt)); - - const categoriesWithProducts = await Promise.all( - categories.map(async (category) => { - const eans = z.array(z.string()).parse(JSON.parse(category.eansJson)); - - const products = await db - .select({ - ean: precios.ean, - name: precios.name, - imageUrl: precios.imageUrl, - }) - .from(precios) - .where(sql`${precios.ean} in ${eans}`) - .groupBy(precios.ean) - .having(max(precios.fetchedAt)); - - return { - category: category.category, - products: eans - .map((ean) => products.find((p) => p.ean === ean)) - .filter((x): x is Product => !!x && !!x.name), - }; - }), - ); - - return { key: new Date(), data: categoriesWithProducts }; +async function getBestSelling() { + const res = await fetch("http://localhost:8000/api/0/best-selling-products"); + const json = await res.json(); + return z + .array( + z.object({ + category: z.string(), + products: z.array( + z.object({ + ean: z.string(), + name: z.string().nullable(), + image_url: z.string().nullable(), + }), + ), + }), + ) + .parse(json); } -console.log("setting up interval"); -setInterval( - async () => { - const c = await doQuery(); - cache = Promise.resolve(c); - }, - 4 * 60 * 60 * 1000, -); - -export const load: PageServerLoad = async ({ - params, -}): Promise<{ data: Data }> => { - return { data: (await cache).data }; +export const load: PageServerLoad = async ({ params }) => { + return { + data: await getBestSelling(), + }; };