39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
|
|
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' }
|
||
|
|
});
|
||
|
|
};
|