deckhearth/pages/api/cards/[id]/price-history.js

33 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

import { sql } from '../../../../lib/sql.js';
import { getPriceHistoryPoints } from '../../../../lib/pricing/price-history.js';
export default async function handler(req, res) {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
const cardId = parseInt(req.query.id, 10);
if (!req.query.id || Number.isNaN(cardId) || cardId <= 0) {
return res.status(400).json({ error: 'Valid card ID is required' });
}
const daysRaw = req.query.days ?? '90';
const days = parseInt(daysRaw, 10);
if (Number.isNaN(days) || days < 1 || days > 365) {
return res.status(400).json({ error: 'days must be between 1 and 365' });
}
const cardCheck = await sql`SELECT id FROM cards WHERE id = ${cardId}`;
if (cardCheck.rows.length === 0) {
return res.status(404).json({ error: 'Card not found' });
}
const points = await getPriceHistoryPoints(cardId, { days });
return res.status(200).json({ points });
} catch (err) {
console.error('[GET /api/cards/[id]/price-history]', err);
return res.status(500).json({ error: 'Internal server error' });
}
}