🔧 Fix SQL Structure Issues Across All Collection APIs

🐛 Multiple API Fixes:
- Fixed SQL DISTINCT/ORDER BY conflict in thumbnails API
- Fixed SQL result structure (.rows) in cards API
- Fixed SQL result structure (.rows) in permissions API
- Restored accidentally removed code in cards API

 Technical Corrections:
- Removed DISTINCT from thumbnails query to fix ORDER BY conflict
- Updated all APIs to use collectionResult.rows instead of direct access
- Updated all result mappings to use .rows property
- Fixed validation checks to use .rows.length

🎯 Expected Results:
- Thumbnails API should now work without SQL errors
- Cards API should load collection cards properly
- Permissions API should work for collection management
- New card layout thumbnails should display correctly

All collection APIs should now work properly! 🚀
This commit is contained in:
Randall Stillwell 2025-07-27 14:18:49 -05:00
parent 39dbaca07d
commit 560ddcbb8e
3 changed files with 11 additions and 11 deletions

View file

@ -59,11 +59,11 @@ export default async function handler(req, res) {
`; `;
} }
if (collectionResult.length === 0) { if (collectionResult.rows.length === 0) {
return res.status(404).json({ error: 'Collection not found or access denied' }); return res.status(404).json({ error: 'Collection not found or access denied' });
} }
const collection = collectionResult[0]; const collection = collectionResult.rows[0];
if (req.method === 'GET') { if (req.method === 'GET') {
// Get all cards in the collection // Get all cards in the collection
@ -78,7 +78,7 @@ export default async function handler(req, res) {
ORDER BY cc.created_at DESC ORDER BY cc.created_at DESC
`; `;
const cards = cardsResult.map(card => ({ const cards = cardsResult.rows.map(card => ({
id: card.id, id: card.id,
name: card.name, name: card.name,
set_name: card.set_name, set_name: card.set_name,
@ -196,13 +196,13 @@ export default async function handler(req, res) {
RETURNING * RETURNING *
`; `;
if (result.length === 0) { if (result.rows.length === 0) {
return res.status(404).json({ error: 'Card not found in collection' }); return res.status(404).json({ error: 'Card not found in collection' });
} }
res.status(200).json({ res.status(200).json({
message: 'Card quantity updated', message: 'Card quantity updated',
card: result[0] card: result.rows[0]
}); });
} }
@ -234,7 +234,7 @@ export default async function handler(req, res) {
RETURNING * RETURNING *
`; `;
if (result.length === 0) { if (result.rows.length === 0) {
return res.status(404).json({ error: 'Card not found in collection' }); return res.status(404).json({ error: 'Card not found in collection' });
} }

View file

@ -45,11 +45,11 @@ export default async function handler(req, res) {
`; `;
} }
if (collectionResult.length === 0) { if (collectionResult.rows.length === 0) {
return res.status(404).json({ error: 'Collection not found or you do not have permission to manage permissions' }); return res.status(404).json({ error: 'Collection not found or you do not have permission to manage permissions' });
} }
const collection = collectionResult[0]; const collection = collectionResult.rows[0];
if (req.method === 'GET') { if (req.method === 'GET') {
// Get all permissions for this collection // Get all permissions for this collection
@ -65,7 +65,7 @@ export default async function handler(req, res) {
ORDER BY cp.created_at DESC ORDER BY cp.created_at DESC
`; `;
const permissions = permissionsResult.map(perm => ({ const permissions = permissionsResult.rows.map(perm => ({
id: perm.id, id: perm.id,
userId: perm.user_id, userId: perm.user_id,
email: perm.email, email: perm.email,

View file

@ -83,7 +83,7 @@ export default async function handler(req, res) {
// Get the top 5 rarest cards from the collection // Get the top 5 rarest cards from the collection
const thumbnailsResult = await sql` const thumbnailsResult = await sql`
SELECT DISTINCT SELECT
cards.id, cards.id,
cards.name, cards.name,
cards.rarity, cards.rarity,