created price-history.ts to get history data and added to modal via chart.js
This commit is contained in:
39
src/pages/partials/price-history.ts
Normal file
39
src/pages/partials/price-history.ts
Normal 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' }
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user