deckhearth/pages/api/cards/[id].js
Randall Stillwell bb60b4f6b0 Enhanced card detail page with real data and functionality
- Updated card detail page to fetch real data from API
- Added ownership tracking with quantity management
- Added favorite system for cards
- Added collection and deck management functionality
- Created API endpoints for ownership, favorites, collections, and decks
- Added database columns for quantity and favorited status
- Shows current collections and decks the card belongs to
- Added proper error handling and loading states
- Integrated with real card data from database
- Added purchase links to TCGPlayer and eBay
2025-07-24 15:03:09 -05:00

42 lines
No EOL
1.1 KiB
JavaScript

import { sql } from '@vercel/postgres';
export default async function handler(req, res) {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method not allowed' });
}
const { id } = req.query;
try {
const result = await sql`
SELECT
id, name, set_name, set_code, card_number, rarity, game,
mana_cost, cmc, card_type, colors, oracle_text,
power, toughness, image_url, stock_image_url,
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' });
}
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 = [];
}
}
res.status(200).json(card);
} catch (error) {
console.error('Error fetching card:', error);
res.status(500).json({ error: 'Failed to fetch card' });
}
}