2025-07-23 22:26:54 -04:00
|
|
|
import { sql } from '@vercel/postgres';
|
|
|
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
2025-07-25 11:42:00 -04:00
|
|
|
// Set CORS headers
|
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
|
|
|
|
|
|
|
|
|
// Handle preflight requests
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
|
res.status(200).end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 22:26:54 -04:00
|
|
|
if (req.method !== 'GET') {
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2025-07-25 11:42:00 -04:00
|
|
|
const { q = '', limit = 20 } = req.query;
|
2025-07-23 22:26:54 -04:00
|
|
|
|
|
|
|
|
let result;
|
2025-07-25 11:42:00 -04:00
|
|
|
if (q.trim()) {
|
|
|
|
|
// Search by name
|
2025-07-23 22:26:54 -04:00
|
|
|
result = await sql`
|
2025-07-25 11:42:00 -04:00
|
|
|
SELECT id, name, set_name, rarity, card_type, image_url, market_price, game
|
2025-07-23 22:26:54 -04:00
|
|
|
FROM cards
|
2025-07-25 11:42:00 -04:00
|
|
|
WHERE name ILIKE ${`%${q}%`}
|
|
|
|
|
ORDER BY name
|
|
|
|
|
LIMIT ${parseInt(limit)}
|
2025-07-23 22:26:54 -04:00
|
|
|
`;
|
|
|
|
|
} else {
|
2025-07-25 11:42:00 -04:00
|
|
|
// Return all cards if no search query
|
2025-07-23 22:26:54 -04:00
|
|
|
result = await sql`
|
2025-07-25 11:42:00 -04:00
|
|
|
SELECT id, name, set_name, rarity, card_type, image_url, market_price, game
|
2025-07-23 22:26:54 -04:00
|
|
|
FROM cards
|
2025-07-25 11:42:00 -04:00
|
|
|
ORDER BY name
|
|
|
|
|
LIMIT ${parseInt(limit)}
|
2025-07-23 22:26:54 -04:00
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 11:42:00 -04:00
|
|
|
res.status(200).json(result.rows);
|
2025-07-23 22:26:54 -04:00
|
|
|
|
|
|
|
|
} catch (error) {
|
2025-07-25 11:42:00 -04:00
|
|
|
console.error('Error searching cards:', error);
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
2025-07-23 22:26:54 -04:00
|
|
|
}
|
2025-07-25 11:42:00 -04:00
|
|
|
}
|