Files
cbd420/app/api/drops/route.ts
2025-12-20 10:32:36 +05:30

59 lines
1.6 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import pool from '@/lib/db'
// GET /api/drops - Get all drops
export async function GET(request: NextRequest) {
try {
const [rows] = await pool.execute(
'SELECT * FROM drops ORDER BY created_at DESC'
)
return NextResponse.json(rows)
} catch (error) {
console.error('Error fetching drops:', error)
return NextResponse.json(
{ error: 'Failed to fetch drops' },
{ status: 500 }
)
}
}
// POST /api/drops - Create a new drop
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const { item, size, unit = 'g', ppu, imageUrl } = body
// Validate required fields
if (!item || !size || !ppu) {
return NextResponse.json(
{ error: 'Missing required fields: item, size, ppu' },
{ status: 400 }
)
}
// Insert new drop
// Note: If imageUrl column doesn't exist in database, add it with:
// ALTER TABLE drops ADD COLUMN image_url VARCHAR(255) DEFAULT NULL AFTER unit;
const [result] = await pool.execute(
'INSERT INTO drops (item, size, unit, ppu, fill, image_url) VALUES (?, ?, ?, ?, 0, ?)',
[item, size, unit, ppu, imageUrl || null]
)
const insertId = (result as any).insertId
// Fetch the created drop
const [rows] = await pool.execute('SELECT * FROM drops WHERE id = ?', [
insertId,
])
return NextResponse.json(rows[0], { status: 201 })
} catch (error) {
console.error('Error creating drop:', error)
return NextResponse.json(
{ error: 'Failed to create drop' },
{ status: 500 }
)
}
}