2025-07-23 22:26:54 -04:00
|
|
|
import { sql } from '@vercel/postgres';
|
|
|
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
2025-07-24 16:03:09 -04:00
|
|
|
if (req.method !== 'GET') {
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
}
|
2025-07-23 22:26:54 -04:00
|
|
|
|
2025-07-24 16:03:09 -04:00
|
|
|
const { id } = req.query;
|
2025-07-23 22:26:54 -04:00
|
|
|
|
2025-07-24 16:03:09 -04:00
|
|
|
try {
|
|
|
|
|
const result = await sql`
|
|
|
|
|
SELECT
|
|
|
|
|
id, name, set_name, set_code, card_number, rarity, game,
|
2025-07-23 22:26:54 -04:00
|
|
|
mana_cost, cmc, card_type, colors, oracle_text,
|
|
|
|
|
power, toughness, image_url, stock_image_url,
|
2025-07-24 16:03:09 -04:00
|
|
|
current_price, market_price, scryfall_id, verified,
|
|
|
|
|
quantity
|
|
|
|
|
FROM cards
|
|
|
|
|
WHERE id = ${id}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
if (result.rows.length === 0) {
|
|
|
|
|
return res.status(404).json({ error: 'Card not found' });
|
|
|
|
|
}
|
2025-07-23 22:26:54 -04:00
|
|
|
|
2025-07-24 16:03:09 -04:00
|
|
|
const card = result.rows[0];
|
|
|
|
|
|
|
|
|
|
// Parse colors if it's a JSON string
|
|
|
|
|
if (card.colors && typeof card.colors === 'string') {
|
|
|
|
|
try {
|
|
|
|
|
card.colors = JSON.parse(card.colors);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
card.colors = [];
|
2025-07-23 22:26:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 16:03:09 -04:00
|
|
|
res.status(200).json(card);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching card:', error);
|
|
|
|
|
res.status(500).json({ error: 'Failed to fetch card' });
|
2025-07-23 22:26:54 -04:00
|
|
|
}
|
|
|
|
|
}
|