2025-07-29 15:19:48 -04:00
|
|
|
import { sql } from '@vercel/postgres';
|
|
|
|
|
import { getUserFromRequest } from '../../lib/permission-middleware';
|
|
|
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
|
|
|
|
try {
|
|
|
|
|
const user = await getUserFromRequest(req);
|
|
|
|
|
if (!user) {
|
|
|
|
|
return res.status(401).json({ error: 'Authentication required' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.method === 'POST') {
|
2026-05-27 14:55:31 -04:00
|
|
|
const {
|
|
|
|
|
cardId,
|
|
|
|
|
quantity = 1,
|
|
|
|
|
condition = 'NM',
|
|
|
|
|
is_foil = false,
|
|
|
|
|
scan_image_url: scanImageUrlRaw,
|
|
|
|
|
} = req.body;
|
2025-07-29 15:19:48 -04:00
|
|
|
|
|
|
|
|
if (!cardId) {
|
|
|
|
|
return res.status(400).json({ error: 'Card ID is required' });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 14:55:31 -04:00
|
|
|
const scanImageUrl =
|
|
|
|
|
typeof scanImageUrlRaw === 'string' && scanImageUrlRaw.trim().length > 0
|
|
|
|
|
? scanImageUrlRaw.trim()
|
|
|
|
|
: null;
|
|
|
|
|
|
2025-07-29 15:19:48 -04:00
|
|
|
// Check if user already owns this card
|
|
|
|
|
const existingResult = await sql`
|
|
|
|
|
SELECT * FROM user_cards
|
|
|
|
|
WHERE user_id = ${user.userId} AND card_id = ${cardId} AND is_foil = ${is_foil}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
if (existingResult.rows.length > 0) {
|
2026-05-27 14:55:31 -04:00
|
|
|
// Update quantity; preserve existing scan image unless a new URL is supplied
|
2025-07-29 15:19:48 -04:00
|
|
|
const newQuantity = existingResult.rows[0].quantity + quantity;
|
|
|
|
|
await sql`
|
|
|
|
|
UPDATE user_cards
|
2026-05-27 14:55:31 -04:00
|
|
|
SET quantity = ${newQuantity},
|
|
|
|
|
updated_at = CURRENT_TIMESTAMP,
|
|
|
|
|
scan_image_url = COALESCE(${scanImageUrl}, scan_image_url)
|
2025-07-29 15:19:48 -04:00
|
|
|
WHERE user_id = ${user.userId} AND card_id = ${cardId} AND is_foil = ${is_foil}
|
|
|
|
|
`;
|
|
|
|
|
} else {
|
|
|
|
|
// Insert new record
|
|
|
|
|
await sql`
|
2026-05-27 14:55:31 -04:00
|
|
|
INSERT INTO user_cards (user_id, card_id, quantity, condition, is_foil, scan_image_url)
|
|
|
|
|
VALUES (${user.userId}, ${cardId}, ${quantity}, ${condition}, ${is_foil}, ${scanImageUrl})
|
2025-07-29 15:19:48 -04:00
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({ message: 'Card added to owned cards' });
|
|
|
|
|
|
|
|
|
|
} else if (req.method === 'GET') {
|
|
|
|
|
// Get user's owned cards
|
|
|
|
|
const result = await sql`
|
|
|
|
|
SELECT uc.*, c.name, c.set_name, c.rarity, c.game, c.image_url
|
|
|
|
|
FROM user_cards uc
|
|
|
|
|
JOIN cards c ON uc.card_id = c.id
|
|
|
|
|
WHERE uc.user_id = ${user.userId}
|
|
|
|
|
ORDER BY uc.created_at DESC
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
return res.status(200).json(result.rows);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error in user-cards API:', error);
|
|
|
|
|
return res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
}
|
|
|
|
|
}
|