73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import pool from '@/lib/db'
|
|
|
|
// GET /api/drops/images?drop_id=X - Get all images for a drop
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const searchParams = request.nextUrl.searchParams
|
|
const dropId = searchParams.get('drop_id')
|
|
|
|
if (!dropId) {
|
|
return NextResponse.json(
|
|
{ error: 'drop_id parameter is required' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const [rows] = await pool.execute(
|
|
'SELECT id, image_url, display_order FROM drop_images WHERE drop_id = ? ORDER BY display_order ASC LIMIT 4',
|
|
[dropId]
|
|
)
|
|
|
|
return NextResponse.json(rows)
|
|
} catch (error) {
|
|
console.error('Error fetching drop images:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch drop images' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// POST /api/drops/images - Add images to a drop
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
const { drop_id, image_urls } = body
|
|
|
|
if (!drop_id || !image_urls || !Array.isArray(image_urls)) {
|
|
return NextResponse.json(
|
|
{ error: 'drop_id and image_urls array are required' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
if (image_urls.length > 4) {
|
|
return NextResponse.json(
|
|
{ error: 'Maximum 4 images allowed per drop' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Delete existing images for this drop
|
|
await pool.execute('DELETE FROM drop_images WHERE drop_id = ?', [drop_id])
|
|
|
|
// Insert new images
|
|
for (let i = 0; i < image_urls.length; i++) {
|
|
await pool.execute(
|
|
'INSERT INTO drop_images (drop_id, image_url, display_order) VALUES (?, ?, ?)',
|
|
[drop_id, image_urls[i], i]
|
|
)
|
|
}
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
console.error('Error saving drop images:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to save drop images' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|