mirror of
https://github.com/catdevnull/preciazo.git
synced 2024-11-22 14:16:19 +00:00
sitio: adaptar homepage para usar api
This commit is contained in:
parent
f01213aaf8
commit
a1fa2796ef
3 changed files with 24 additions and 68 deletions
|
@ -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)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<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 lang="ts">
|
||||
|
@ -7,9 +7,9 @@
|
|||
</script>
|
||||
|
||||
<a href={`/ean/${product.ean}`} class="flex gap-2">
|
||||
{#if product.imageUrl}
|
||||
{#if product.image_url}
|
||||
<img
|
||||
src={product.imageUrl}
|
||||
src={product.image_url}
|
||||
alt={product.name}
|
||||
class="max-h-48"
|
||||
loading="lazy"
|
||||
|
|
|
@ -1,68 +1,27 @@
|
|||
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 type { Product } from "$lib/ProductPreview.svelte";
|
||||
|
||||
type Data = {
|
||||
category: string;
|
||||
products: Product[];
|
||||
}[];
|
||||
|
||||
let cache: Promise<{ key: Date; data: Data }> = 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(),
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue