38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { deleteConnection, updateConnection, normalizeJsonColumn } from "@/lib/db";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
type Ctx = { params: Promise<{ id: string }> };
|
|
|
|
export async function PUT(request: Request, { params }: Ctx) {
|
|
try {
|
|
const { id } = await params;
|
|
const body = await request.json();
|
|
if (body.params !== undefined) {
|
|
body.params = normalizeJsonColumn(body.params);
|
|
}
|
|
const conn = updateConnection(Number(id), body);
|
|
if (!conn) {
|
|
return Response.json({ error: "connection not found" }, { status: 404 });
|
|
}
|
|
return Response.json(conn);
|
|
} catch (err) {
|
|
const message = (err as Error).message;
|
|
const status = message.includes("UNIQUE") ? 409 : 500;
|
|
return Response.json({ error: message }, { status });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(_request: Request, { params }: Ctx) {
|
|
try {
|
|
const { id } = await params;
|
|
if (!deleteConnection(Number(id))) {
|
|
return Response.json({ error: "connection not found" }, { status: 404 });
|
|
}
|
|
return Response.json({ ok: true });
|
|
} catch (err) {
|
|
return Response.json({ error: (err as Error).message }, { status: 500 });
|
|
}
|
|
}
|