import { NextRequest, NextResponse } from 'next/server'; import { Pool } from 'pg'; const pool = new Pool({ connectionString: process.env.DATABASE_URL || 'postgresql://vixtix:vixtix_secret@localhost:5433/vixtix', }); function serializeRow(row: Record): Record { const toCamel = (str: string) => str.replace(/_([a-z])/g, (_, c) => c.toUpperCase()); const result: Record = {}; for (const [key, value] of Object.entries(row)) { const camelKey = toCamel(key); if (value instanceof Date) { result[camelKey] = value.toISOString(); } else { result[camelKey] = value; } } return result; } export async function GET( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { const { id } = await params; try { const result = await pool.query('SELECT * FROM projects WHERE id = $1', [id]); if (result.rows.length === 0) { return NextResponse.json({ error: 'Project not found' }, { status: 404 }); } return NextResponse.json(serializeRow(result.rows[0])); } catch (error) { console.error('Error fetching project:', error); return NextResponse.json({ error: 'Failed to fetch project' }, { status: 500 }); } } export async function PUT( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { const { id } = await params; let body; try { body = await request.json(); } catch { return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 }); } const { name, description, color, sortOrder } = body; if (!name?.trim()) { return NextResponse.json({ error: 'Name is required' }, { status: 400 }); } try { const result = await pool.query( `UPDATE projects SET name = $1, description = $2, color = $3, sort_order = $4 WHERE id = $5 RETURNING *`, [name.trim(), description?.trim() || '', color, sortOrder, id] ); if (result.rows.length === 0) { return NextResponse.json({ error: 'Project not found' }, { status: 404 }); } return NextResponse.json(serializeRow(result.rows[0])); } catch (error) { console.error('Error updating project:', error); return NextResponse.json({ error: 'Failed to update project' }, { status: 500 }); } } export async function DELETE( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { const { id } = await params; try { // Delete all tasks in the project first await pool.query('DELETE FROM tasks WHERE project_id = $1', [id]); // Then delete the project await pool.query('DELETE FROM projects WHERE id = $1', [id]); return NextResponse.json({ success: true }); } catch (error) { console.error('Error deleting project:', error); return NextResponse.json({ error: 'Failed to delete project' }, { status: 500 }); } }