|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | +import { db } from "@/lib/db"; |
| 3 | +import { events, type Event } from "@/lib/db/schema"; |
| 4 | +import { processFileForStorage } from "@/lib/storage"; |
| 5 | +import { eq } from "drizzle-orm"; |
| 6 | + |
| 7 | +// Disable body parsing since we're using form data |
| 8 | +export const config = { |
| 9 | + api: { |
| 10 | + bodyParser: false, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +// Helper function to convert binary image to base64 data URL |
| 15 | +function processEventImage(event: Event) { |
| 16 | + if (!event.imageData) return { ...event, imageData: null }; |
| 17 | + |
| 18 | + try { |
| 19 | + const buffer = Buffer.isBuffer(event.imageData) |
| 20 | + ? event.imageData |
| 21 | + : Buffer.from(event.imageData as unknown as ArrayBuffer); |
| 22 | + |
| 23 | + const base64Image = buffer.toString('base64'); |
| 24 | + const mimeType = event.imageMimeType || 'image/jpeg'; |
| 25 | + |
| 26 | + return { |
| 27 | + ...event, |
| 28 | + imageData: `data:${mimeType};base64,${base64Image}`, |
| 29 | + imageMimeType: mimeType |
| 30 | + }; |
| 31 | + } catch (error) { |
| 32 | + console.error('Error processing image:', error); |
| 33 | + return { ...event, imageData: null, imageMimeType: null }; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +type RouteParams = { |
| 38 | + params: { |
| 39 | + id: string; |
| 40 | + }; |
| 41 | +}; |
| 42 | + |
| 43 | +// GET one event |
| 44 | +export async function GET( |
| 45 | + _: Request, |
| 46 | + { params }: RouteParams |
| 47 | +) { |
| 48 | + try { |
| 49 | + const result = await db.select().from(events).where(eq(events.id, params.id)); |
| 50 | + if (!result[0]) { |
| 51 | + return NextResponse.json({ error: "Not found" }, { status: 404 }); |
| 52 | + } |
| 53 | + return NextResponse.json(processEventImage(result[0])); |
| 54 | + } catch (error) { |
| 55 | + console.error("Error fetching event:", error); |
| 56 | + return NextResponse.json( |
| 57 | + { error: "Failed to fetch event" }, |
| 58 | + { status: 500 } |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// PUT (update) event |
| 64 | +export async function PUT( |
| 65 | + request: Request, |
| 66 | + { params }: RouteParams |
| 67 | +) { |
| 68 | + try { |
| 69 | + const formData = await request.formData(); |
| 70 | + const title = formData.get('title') as string; |
| 71 | + const description = formData.get('description') as string; |
| 72 | + const venue = formData.get('venue') as string; |
| 73 | + const date = formData.get('date') as string; |
| 74 | + const time = formData.get('time') as string; |
| 75 | + const imageFile = formData.get('image') as File | null; |
| 76 | + |
| 77 | + if (!title || !description || !venue || !date) { |
| 78 | + return NextResponse.json( |
| 79 | + { error: "Missing required fields" }, |
| 80 | + { status: 400 } |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + const updateData: Partial<Event> = { |
| 85 | + title, |
| 86 | + description, |
| 87 | + venue, |
| 88 | + date: new Date(date), |
| 89 | + time: time || null, |
| 90 | + updatedAt: new Date() |
| 91 | + }; |
| 92 | + |
| 93 | + // Handle image upload if provided |
| 94 | + if (imageFile) { |
| 95 | + try { |
| 96 | + const { buffer, mimeType } = await processFileForStorage(imageFile); |
| 97 | + updateData.imageData = buffer; |
| 98 | + updateData.imageMimeType = mimeType; |
| 99 | + } catch (error) { |
| 100 | + console.error("Error processing image:", error); |
| 101 | + return NextResponse.json( |
| 102 | + { error: "Failed to process image" }, |
| 103 | + { status: 400 } |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + const result = await db |
| 109 | + .update(events) |
| 110 | + .set(updateData) |
| 111 | + .where(eq(events.id, params.id)) |
| 112 | + .returning(); |
| 113 | + |
| 114 | + if (!result[0]) { |
| 115 | + return NextResponse.json({ error: "Event not found" }, { status: 404 }); |
| 116 | + } |
| 117 | + |
| 118 | + return NextResponse.json(processEventImage(result[0])); |
| 119 | + } catch (error) { |
| 120 | + console.error("Error updating event:", error); |
| 121 | + return NextResponse.json( |
| 122 | + { error: "Failed to update event" }, |
| 123 | + { status: 500 } |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// DELETE event |
| 129 | +export async function DELETE( |
| 130 | + _: Request, |
| 131 | + { params }: RouteParams |
| 132 | +) { |
| 133 | + try { |
| 134 | + await db.delete(events).where(eq(events.id, params.id)); |
| 135 | + return new NextResponse(null, { status: 204 }); |
| 136 | + } catch (error) { |
| 137 | + console.error("Error deleting event:", error); |
| 138 | + return NextResponse.json( |
| 139 | + { error: "Failed to delete event" }, |
| 140 | + { status: 500 } |
| 141 | + ); |
| 142 | + } |
| 143 | +} |
0 commit comments