sitio: adaptar homepage para usar api

This commit is contained in:
Cat /dev/Nulo 2024-08-04 12:55:20 -03:00
parent f01213aaf8
commit a1fa2796ef
3 changed files with 24 additions and 68 deletions

View file

@ -39,9 +39,6 @@ importers:
dayjs: dayjs:
specifier: ^1.11.10 specifier: ^1.11.10
version: 1.11.10 version: 1.11.10
drizzle-kit:
specifier: ^0.23.0
version: 0.23.0
drizzle-orm: drizzle-orm:
specifier: ^0.32.0 specifier: ^0.32.0
version: 0.32.0(@types/better-sqlite3@7.6.9)(better-sqlite3@11.1.2) version: 0.32.0(@types/better-sqlite3@7.6.9)(better-sqlite3@11.1.2)

View file

@ -1,5 +1,5 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
export type Product = { ean: string; name: string; imageUrl: string | null }; export type Product = { ean: string; name: string; image_url: string | null };
</script> </script>
<script lang="ts"> <script lang="ts">
@ -7,9 +7,9 @@
</script> </script>
<a href={`/ean/${product.ean}`} class="flex gap-2"> <a href={`/ean/${product.ean}`} class="flex gap-2">
{#if product.imageUrl} {#if product.image_url}
<img <img
src={product.imageUrl} src={product.image_url}
alt={product.name} alt={product.name}
class="max-h-48" class="max-h-48"
loading="lazy" loading="lazy"

View file

@ -1,68 +1,27 @@
import type { PageServerLoad } from "./$types"; import type { PageServerLoad } from "./$types";
import { getDb, schema } from "$lib/server/db";
const { precios, bestSelling } = schema;
import { max, sql } from "drizzle-orm";
import z from "zod"; import z from "zod";
import type { Product } from "$lib/ProductPreview.svelte";
type Data = { async function getBestSelling() {
category: string; const res = await fetch("http://localhost:8000/api/0/best-selling-products");
products: Product[]; const json = await res.json();
}[]; return z
.array(
let cache: Promise<{ key: Date; data: Data }> = doQuery(); z.object({
category: z.string(),
async function doQuery() { products: z.array(
const db = await getDb(); z.object({
ean: z.string(),
const categories = await db name: z.string().nullable(),
.select({ image_url: z.string().nullable(),
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 }; )
.parse(json);
} }
console.log("setting up interval"); export const load: PageServerLoad = async ({ params }) => {
setInterval( return {
async () => { data: await getBestSelling(),
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 };
}; };