created price-history.ts to get history data and added to modal via chart.js

This commit is contained in:
zach
2026-03-16 08:39:06 -04:00
parent 3a6dbf2ed9
commit 9c81a13c69
5 changed files with 684 additions and 246 deletions

View File

@@ -0,0 +1,39 @@
import type { APIRoute } from 'astro';
import { db } from '../../db/index';
import { priceHistory, skus } from '../../db/schema';
import { eq, inArray } from 'drizzle-orm';
export const prerender = false;
export const GET: APIRoute = async ({ url }) => {
const cardId = Number(url.searchParams.get('cardId')) || 0;
// Get all skus for this card
const cardSkus = await db
.select()
.from(skus)
.where(eq(skus.cardId, cardId));
if (!cardSkus.length) {
return new Response(JSON.stringify([]), { headers: { 'Content-Type': 'application/json' } });
}
const skuIds = cardSkus.map(s => s.skuId);
// Fetch price history for all skus
const history = await db
.select({
skuId: priceHistory.skuId,
calculatedAt: priceHistory.calculatedAt,
marketPrice: priceHistory.marketPrice,
condition: skus.condition,
})
.from(priceHistory)
.innerJoin(skus, eq(priceHistory.skuId, skus.skuId))
.where(inArray(priceHistory.skuId, skuIds))
.orderBy(priceHistory.calculatedAt);
return new Response(JSON.stringify(history), {
headers: { 'Content-Type': 'application/json' }
});
};