- Created comprehensive collection detail page (/collection/[id]) with: * Hero section with collection metadata (name, creator, format, cost) * Action buttons (copy link, share, print proxies, save deck) * Collection statistics (total cards, value, views, favorites) * Advanced filtering and search functionality * Grid and list view modes for cards * Real-time card filtering by rarity, type, and search terms - Added complete API endpoints for collection management: * GET/PUT/DELETE /api/collections/[id] - collection CRUD operations * POST/PUT/DELETE /api/collections/[id]/cards - card management * Enhanced /api/collections with database integration - Features matching deck builder interface from image: * Beautiful hero section with background image * Metadata display (creator, format, cost, play guide) * Copy/share/save functionality * Comprehensive filtering system * Responsive card grid and list views * Real-time statistics and card counting - Updated collections listing page to link to detailed views - Proper error handling and loading states throughout - Mobile-responsive design with modern UI/UX
131 lines
No EOL
3.8 KiB
JavaScript
131 lines
No EOL
3.8 KiB
JavaScript
import { sql } from '@vercel/postgres';
|
|
|
|
export default async function handler(req, res) {
|
|
// Set CORS headers
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
|
|
|
// Handle preflight requests
|
|
if (req.method === 'OPTIONS') {
|
|
res.status(200).end();
|
|
return;
|
|
}
|
|
|
|
const { id } = req.query; // collection id
|
|
|
|
if (req.method === 'POST') {
|
|
// Add card to collection
|
|
try {
|
|
const { cardId, quantity = 1 } = req.body;
|
|
|
|
if (!cardId) {
|
|
return res.status(400).json({ error: 'Card ID is required' });
|
|
}
|
|
|
|
// Check if card already exists in collection
|
|
const existingResult = await sql`
|
|
SELECT * FROM collection_cards
|
|
WHERE collection_id = ${id} AND card_id = ${cardId}
|
|
`;
|
|
|
|
if (existingResult.rows.length > 0) {
|
|
// Update quantity if card already exists
|
|
const result = await sql`
|
|
UPDATE collection_cards
|
|
SET quantity = quantity + ${quantity}
|
|
WHERE collection_id = ${id} AND card_id = ${cardId}
|
|
RETURNING *
|
|
`;
|
|
|
|
res.status(200).json({
|
|
message: 'Card quantity updated in collection',
|
|
card: result.rows[0]
|
|
});
|
|
} else {
|
|
// Add new card to collection
|
|
const result = await sql`
|
|
INSERT INTO collection_cards (collection_id, card_id, quantity)
|
|
VALUES (${id}, ${cardId}, ${quantity})
|
|
RETURNING *
|
|
`;
|
|
|
|
res.status(201).json({
|
|
message: 'Card added to collection',
|
|
card: result.rows[0]
|
|
});
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Error adding card to collection:', error);
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
}
|
|
} else if (req.method === 'PUT') {
|
|
// Update card quantity in collection
|
|
try {
|
|
const { cardId, quantity } = req.body;
|
|
|
|
if (!cardId || quantity === undefined) {
|
|
return res.status(400).json({ error: 'Card ID and quantity are required' });
|
|
}
|
|
|
|
if (quantity <= 0) {
|
|
// Remove card if quantity is 0 or negative
|
|
await sql`
|
|
DELETE FROM collection_cards
|
|
WHERE collection_id = ${id} AND card_id = ${cardId}
|
|
`;
|
|
|
|
res.status(200).json({ message: 'Card removed from collection' });
|
|
} else {
|
|
// Update quantity
|
|
const result = await sql`
|
|
UPDATE collection_cards
|
|
SET quantity = ${quantity}
|
|
WHERE collection_id = ${id} AND card_id = ${cardId}
|
|
RETURNING *
|
|
`;
|
|
|
|
if (result.rows.length === 0) {
|
|
return res.status(404).json({ error: 'Card not found in collection' });
|
|
}
|
|
|
|
res.status(200).json({
|
|
message: 'Card quantity updated',
|
|
card: result.rows[0]
|
|
});
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Error updating card in collection:', error);
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
}
|
|
} else if (req.method === 'DELETE') {
|
|
// Remove card from collection
|
|
try {
|
|
const { cardId } = req.body;
|
|
|
|
if (!cardId) {
|
|
return res.status(400).json({ error: 'Card ID is required' });
|
|
}
|
|
|
|
const result = await sql`
|
|
DELETE FROM collection_cards
|
|
WHERE collection_id = ${id} AND card_id = ${cardId}
|
|
RETURNING *
|
|
`;
|
|
|
|
if (result.rows.length === 0) {
|
|
return res.status(404).json({ error: 'Card not found in collection' });
|
|
}
|
|
|
|
res.status(200).json({ message: 'Card removed from collection' });
|
|
|
|
} catch (error) {
|
|
console.error('Error removing card from collection:', error);
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
}
|
|
} else {
|
|
res.status(405).json({ error: 'Method not allowed' });
|
|
}
|
|
}
|