78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getDb } from "@/lib/db";
|
|
|
|
export async function GET(
|
|
_request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const sql = getDb();
|
|
const { id } = await params;
|
|
const rows = await sql(`SELECT * FROM suppliers WHERE id = $1`, [Number(id)]);
|
|
|
|
if (rows.length === 0) {
|
|
return NextResponse.json({ error: "Supplier not found" }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json(rows[0]);
|
|
} catch (error) {
|
|
console.error("[v0] Supplier GET error:", error);
|
|
return NextResponse.json(
|
|
{ error: error instanceof Error ? error.message : "Failed to fetch supplier" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const sql = getDb();
|
|
const { id } = await params;
|
|
const body = await request.json();
|
|
const { name, code, email, phone, address, contact_person, country, currency, notes } = body;
|
|
|
|
const rows = await sql(
|
|
`UPDATE suppliers SET
|
|
name = $1, code = $2, email = $3, phone = $4,
|
|
address = $5, contact_person = $6, country = $7, currency = $8, notes = $9, updated_at = NOW()
|
|
WHERE id = $10
|
|
RETURNING *`,
|
|
[name, code, email || null, phone || null, address || null, contact_person || null, country || "", currency || "EUR", notes || null, Number(id)]
|
|
);
|
|
|
|
if (rows.length === 0) {
|
|
return NextResponse.json({ error: "Supplier not found" }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json(rows[0]);
|
|
} catch (error) {
|
|
console.error("[v0] Supplier PUT error:", error);
|
|
return NextResponse.json(
|
|
{ error: error instanceof Error ? error.message : "Failed to update supplier" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function DELETE(
|
|
_request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const sql = getDb();
|
|
const { id } = await params;
|
|
|
|
await sql(`DELETE FROM suppliers WHERE id = $1`, [Number(id)]);
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error("[v0] Supplier DELETE error:", error);
|
|
return NextResponse.json(
|
|
{ error: error instanceof Error ? error.message : "Failed to delete supplier" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|