File size: 2,109 Bytes
333c51a
 
 
 
 
5cff373
 
 
 
333c51a
 
 
 
 
 
 
 
 
5cff373
333c51a
 
 
5cff373
333c51a
 
5cff373
 
 
 
 
333c51a
5cff373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333c51a
 
5cff373
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 });
  }
}