import { NextRequest, NextResponse } from "next/server"; import { db } from "@/lib/db"; export async function GET() { try { const projects = await db.project.findMany({ orderBy: { createdAt: "desc" } }); return NextResponse.json({ success: true, projects }); } catch { return NextResponse.json({ success: false, error: "Error" }, { status: 500 }); } } export async function POST(request: NextRequest) { try { const body = await request.json(); const { name, description, style } = body; if (!name) { return NextResponse.json({ success: false, error: "Nombre requerido" }, { status: 400 }); } const project = await db.project.create({ data: { name, description: description || null, style: style || "default" } }); return NextResponse.json({ success: true, project }); } catch { return NextResponse.json({ success: false, error: "Error" }, { status: 500 }); } } export async function PUT(request: NextRequest) { try { const body = await request.json(); const { id, name, description, status } = body; if (!id) { return NextResponse.json({ success: false, error: "ID requerido" }, { status: 400 }); } const data: { name?: string; description?: string | null; status?: string } = {}; if (name) data.name = name; if (description !== undefined) data.description = description || null; if (status) data.status = status; const project = await db.project.update({ where: { id }, data }); return NextResponse.json({ success: true, project }); } catch { return NextResponse.json({ success: false, error: "Error" }, { status: 500 }); } } export async function DELETE(request: NextRequest) { try { const { searchParams } = new URL(request.url); const id = searchParams.get("id"); if (!id) return NextResponse.json({ success: false, error: "ID requerido" }, { status: 400 }); await db.project.delete({ where: { id } }); return NextResponse.json({ success: true }); } catch { return NextResponse.json({ success: false, error: "Error" }, { status: 500 }); } }