2025-07-24 16:03:09 -04:00
|
|
|
import { sql } from '@vercel/postgres';
|
2025-07-26 01:29:51 -04:00
|
|
|
import { getUserFromRequest, logCollectionActivity } from '../../lib/permission-middleware';
|
2025-07-26 23:06:52 -04:00
|
|
|
import { generateUniqueSlug } from '../../lib/slug-utils';
|
2025-07-24 16:03:09 -04:00
|
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
if (req.method === 'GET') {
|
|
|
|
|
try {
|
2025-07-26 01:29:51 -04:00
|
|
|
// Get authenticated user
|
|
|
|
|
const user = await getUserFromRequest(req);
|
|
|
|
|
if (!user) {
|
|
|
|
|
return res.status(401).json({ error: 'Authentication required' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const currentUserId = user.userId;
|
2025-07-27 20:17:55 -04:00
|
|
|
const { excludeSystem } = req.query; // New parameter to exclude system collections
|
2025-07-25 09:34:28 -04:00
|
|
|
|
2025-07-27 14:17:13 -04:00
|
|
|
// Get collections based on ownership, collaboration, or shared access (no public discovery)
|
2025-07-27 21:00:20 -04:00
|
|
|
let query = `
|
2025-07-25 09:34:28 -04:00
|
|
|
SELECT DISTINCT
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
c.*,
|
|
|
|
|
u.email as creator_email,
|
|
|
|
|
COUNT(cc.card_id) as card_count,
|
2025-07-25 09:34:28 -04:00
|
|
|
COALESCE(SUM(cards.market_price * cc.quantity), 0) as total_value,
|
2025-07-25 11:29:45 -04:00
|
|
|
cp.role as user_role,
|
|
|
|
|
CASE
|
2025-07-27 21:00:20 -04:00
|
|
|
WHEN c.user_id = $1 THEN 'owner'
|
2025-07-25 11:29:45 -04:00
|
|
|
WHEN cp.role IS NOT NULL THEN cp.role
|
|
|
|
|
ELSE NULL
|
|
|
|
|
END as effective_role
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
FROM collections c
|
|
|
|
|
LEFT JOIN users u ON c.user_id = u.id
|
|
|
|
|
LEFT JOIN collection_cards cc ON c.id = cc.collection_id
|
|
|
|
|
LEFT JOIN cards ON cc.card_id = cards.id
|
2025-07-27 21:00:20 -04:00
|
|
|
LEFT JOIN collection_permissions cp ON c.id = cp.collection_id AND cp.user_id = $2 AND cp.status = 'active'
|
2025-07-25 09:34:28 -04:00
|
|
|
WHERE
|
2025-07-27 21:00:20 -04:00
|
|
|
(c.user_id = $3 OR cp.id IS NOT NULL)
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const params = [currentUserId, currentUserId, currentUserId];
|
|
|
|
|
|
|
|
|
|
if (excludeSystem === 'true') {
|
|
|
|
|
query += ` AND (c.is_system_collection IS NULL OR c.is_system_collection = false)`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query += `
|
2025-07-25 09:34:28 -04:00
|
|
|
GROUP BY c.id, u.email, cp.role
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
ORDER BY c.updated_at DESC
|
|
|
|
|
`;
|
|
|
|
|
|
2025-07-27 21:00:20 -04:00
|
|
|
const result = await sql.query(query, params);
|
|
|
|
|
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
const collections = result.rows.map(collection => ({
|
|
|
|
|
id: collection.id,
|
2025-07-26 23:06:52 -04:00
|
|
|
slug: collection.slug,
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
name: collection.name,
|
|
|
|
|
description: collection.description,
|
|
|
|
|
tcg: collection.tcg || 'MTG',
|
|
|
|
|
cardCount: parseInt(collection.card_count) || 0,
|
|
|
|
|
value: parseFloat(collection.total_value) || 0,
|
|
|
|
|
lastViewed: collection.updated_at,
|
|
|
|
|
createdAt: collection.created_at,
|
2025-07-25 11:29:45 -04:00
|
|
|
isPublic: collection.is_public || false,
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
tags: collection.tags ? collection.tags.split(',') : [],
|
2025-07-25 09:34:28 -04:00
|
|
|
creator: collection.creator_email,
|
2025-07-25 11:29:45 -04:00
|
|
|
userRole: collection.effective_role
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
res.status(200).json(collections);
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching collections:', error);
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
}
|
|
|
|
|
} else if (req.method === 'POST') {
|
|
|
|
|
try {
|
2025-07-26 01:29:51 -04:00
|
|
|
// Get authenticated user
|
|
|
|
|
const user = await getUserFromRequest(req);
|
|
|
|
|
if (!user) {
|
|
|
|
|
return res.status(401).json({ error: 'Authentication required' });
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 11:29:45 -04:00
|
|
|
const { name, description, tcg = 'MTG', isPublic = false, image = '', tags = [] } = req.body;
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
|
|
|
|
|
if (!name || !description) {
|
|
|
|
|
return res.status(400).json({ error: 'Name and description are required' });
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 01:29:51 -04:00
|
|
|
const userId = user.userId;
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
|
2025-07-26 23:06:52 -04:00
|
|
|
// Generate unique slug for the collection
|
|
|
|
|
const existingSlugsResult = await sql`SELECT slug FROM collections WHERE slug IS NOT NULL`;
|
|
|
|
|
const existingSlugs = existingSlugsResult.rows.map(row => row.slug);
|
|
|
|
|
const uniqueSlug = await generateUniqueSlug(name, existingSlugs);
|
|
|
|
|
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
const result = await sql`
|
2025-07-26 23:06:52 -04:00
|
|
|
INSERT INTO collections (name, description, tcg, is_public, image, tags, user_id, slug)
|
|
|
|
|
VALUES (${name}, ${description}, ${tcg}, ${isPublic}, ${image}, ${tags.join(',')}, ${userId}, ${uniqueSlug})
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
RETURNING *
|
|
|
|
|
`;
|
|
|
|
|
|
2025-07-25 09:34:28 -04:00
|
|
|
// Create owner permission record
|
|
|
|
|
await sql`
|
|
|
|
|
INSERT INTO collection_permissions (collection_id, user_id, role, status)
|
|
|
|
|
VALUES (${result.rows[0].id}, ${userId}, 'owner', 'active')
|
|
|
|
|
`;
|
|
|
|
|
|
Enhanced collections with detailed view and card management
- 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
2025-07-25 08:44:23 -04:00
|
|
|
res.status(201).json(result.rows[0]);
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error creating collection:', error);
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
res.status(405).json({ error: 'Method not allowed' });
|
2025-07-24 16:03:09 -04:00
|
|
|
}
|
|
|
|
|
}
|