init
This commit is contained in:
21
app/api/drops/active/route.ts
Normal file
21
app/api/drops/active/route.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import pool from '@/lib/db'
|
||||
|
||||
// GET /api/drops/active - Get the currently active drop (not sold out)
|
||||
export async function GET() {
|
||||
try {
|
||||
const [rows] = await pool.execute(
|
||||
'SELECT * FROM drops WHERE fill < size ORDER BY created_at DESC LIMIT 1'
|
||||
)
|
||||
|
||||
const drops = rows as any[]
|
||||
return NextResponse.json(drops[0] || null)
|
||||
} catch (error) {
|
||||
console.error('Error fetching active drop:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch active drop' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
58
app/api/drops/route.ts
Normal file
58
app/api/drops/route.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
66
app/api/upload/route.ts
Normal file
66
app/api/upload/route.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { writeFile, mkdir } from 'fs/promises'
|
||||
import { join } from 'path'
|
||||
import { existsSync } from 'fs'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const formData = await request.formData()
|
||||
const file = formData.get('file') as File
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No file uploaded' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Validate file type
|
||||
const validTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp']
|
||||
if (!validTypes.includes(file.type)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid file type. Only JPEG, PNG, and WebP are allowed.' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Validate file size (max 5MB)
|
||||
const maxSize = 5 * 1024 * 1024 // 5MB
|
||||
if (file.size > maxSize) {
|
||||
return NextResponse.json(
|
||||
{ error: 'File size too large. Maximum size is 5MB.' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const bytes = await file.arrayBuffer()
|
||||
const buffer = Buffer.from(bytes)
|
||||
|
||||
// Create uploads directory if it doesn't exist
|
||||
const uploadsDir = join(process.cwd(), 'public', 'uploads')
|
||||
if (!existsSync(uploadsDir)) {
|
||||
await mkdir(uploadsDir, { recursive: true })
|
||||
}
|
||||
|
||||
// Generate unique filename
|
||||
const timestamp = Date.now()
|
||||
const originalName = file.name.replace(/[^a-zA-Z0-9.-]/g, '_')
|
||||
const filename = `${timestamp}-${originalName}`
|
||||
const filepath = join(uploadsDir, filename)
|
||||
|
||||
// Write file
|
||||
await writeFile(filepath, buffer)
|
||||
|
||||
// Return the public URL
|
||||
const fileUrl = `/uploads/${filename}`
|
||||
|
||||
return NextResponse.json({ url: fileUrl }, { status: 200 })
|
||||
} catch (error) {
|
||||
console.error('Error uploading file:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to upload file' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user