File size: 1,410 Bytes
ef4c36f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextRequest, NextResponse } from "next/server";
import { loadProject, saveProject } from "@/lib/storage";
import type { Project } from "@/types";

export const runtime = "nodejs";

export async function GET(req: NextRequest) {
  const id = req.nextUrl.searchParams.get("id");
  if (!id) return NextResponse.json({ error: "Missing id" }, { status: 400 });

  const project = await loadProject(id);
  if (!project) return NextResponse.json({ error: "Project not found" }, { status: 404 });

  return NextResponse.json(project);
}

export async function PUT(req: NextRequest) {
  try {
    const body = (await req.json()) as Project;
    if (!body?.id) return NextResponse.json({ error: "Missing project id" }, { status: 400 });

    const existing = await loadProject(body.id);
    if (!existing) return NextResponse.json({ error: "Project not found" }, { status: 404 });

    const updated: Project = {
      ...existing,
      captions: body.captions ?? existing.captions,
      style: body.style ?? existing.style,
      layout: body.layout ?? existing.layout,
      animation: body.animation ?? existing.animation,
      status: body.status ?? existing.status,
    };

    await saveProject(updated);
    return NextResponse.json(updated);
  } catch (error) {
    return NextResponse.json(
      { error: error instanceof Error ? error.message : "Save failed" },
      { status: 500 },
    );
  }
}