97 lines
2.9 KiB
JavaScript
97 lines
2.9 KiB
JavaScript
|
|
import { sql } from '@vercel/postgres';
|
||
|
|
|
||
|
|
export default async function handler(req, res) {
|
||
|
|
if (req.method !== 'POST') {
|
||
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const { setCode } = req.body;
|
||
|
|
|
||
|
|
if (!setCode) {
|
||
|
|
return res.status(400).json({ error: 'Set code is required' });
|
||
|
|
}
|
||
|
|
|
||
|
|
// Fetch cards from Pokemon TCG API
|
||
|
|
const response = await fetch(`https://api.pokemontcg.io/v2/cards?q=set.id:${setCode}&pageSize=250`);
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
throw new Error(`Pokemon TCG API error: ${response.status}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
const data = await response.json();
|
||
|
|
const cards = data.data || [];
|
||
|
|
|
||
|
|
let importedCount = 0;
|
||
|
|
let skippedCount = 0;
|
||
|
|
|
||
|
|
for (const card of cards) {
|
||
|
|
try {
|
||
|
|
// Check if card already exists
|
||
|
|
const existingCard = await sql`
|
||
|
|
SELECT id FROM cards WHERE scryfall_id = ${card.id}
|
||
|
|
`;
|
||
|
|
|
||
|
|
if (existingCard.rows.length > 0) {
|
||
|
|
skippedCount++;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Extract price data from TCGPlayer
|
||
|
|
let currentPrice = null;
|
||
|
|
if (card.tcgplayer?.prices?.normal?.market) {
|
||
|
|
currentPrice = parseFloat(card.tcgplayer.prices.normal.market);
|
||
|
|
} else if (card.tcgplayer?.prices?.holofoil?.market) {
|
||
|
|
currentPrice = parseFloat(card.tcgplayer.prices.holofoil.market);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Determine rarity
|
||
|
|
let rarity = card.rarity || 'Unknown';
|
||
|
|
if (rarity.includes('Holo')) {
|
||
|
|
rarity = 'Holographic';
|
||
|
|
} else if (rarity.includes('Secret')) {
|
||
|
|
rarity = 'Secret Rare';
|
||
|
|
} else if (rarity.includes('Ultra')) {
|
||
|
|
rarity = 'Ultra Rare';
|
||
|
|
}
|
||
|
|
|
||
|
|
// Insert card into database
|
||
|
|
await sql`
|
||
|
|
INSERT INTO cards (
|
||
|
|
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
|
||
|
|
) VALUES (
|
||
|
|
${card.name}, ${card.set.name}, ${card.set.id}, ${card.number},
|
||
|
|
${rarity}, 'Pokemon', null, null, ${card.supertype || 'Pokemon'},
|
||
|
|
${JSON.stringify(card.types || [])}, ${card.flavorText || null},
|
||
|
|
${card.attacks?.[0]?.damage || null}, null,
|
||
|
|
${card.images?.small || null}, ${card.images?.large || null},
|
||
|
|
${currentPrice}, null, ${card.id}, true
|
||
|
|
)
|
||
|
|
`;
|
||
|
|
|
||
|
|
importedCount++;
|
||
|
|
} catch (error) {
|
||
|
|
console.error(`Error importing card ${card.name}:`, error);
|
||
|
|
skippedCount++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
res.status(200).json({
|
||
|
|
success: true,
|
||
|
|
message: `Import completed for set ${setCode}`,
|
||
|
|
imported: importedCount,
|
||
|
|
skipped: skippedCount,
|
||
|
|
total: cards.length
|
||
|
|
});
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Card import error:', error);
|
||
|
|
res.status(500).json({
|
||
|
|
error: 'Import failed',
|
||
|
|
details: error.message
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|