123 lines
3.2 KiB
TypeScript
123 lines
3.2 KiB
TypeScript
|
|
import { NextResponse } from "next/server";
|
||
|
|
|
||
|
|
import { auth } from "@/src/auth";
|
||
|
|
import {
|
||
|
|
getMacchinaById,
|
||
|
|
updateMacchina,
|
||
|
|
deleteMacchina,
|
||
|
|
} from "@/src/lib/macchine";
|
||
|
|
import type { MacchinaUpdateInput } from "@/src/types/macchina";
|
||
|
|
|
||
|
|
interface RouteParams {
|
||
|
|
params: Promise<{ id: string }>;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function GET(request: Request, { params }: RouteParams) {
|
||
|
|
const session = await auth();
|
||
|
|
|
||
|
|
if (!session?.user) {
|
||
|
|
return NextResponse.json({ error: "Non autorizzato" }, { status: 401 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const { id } = await params;
|
||
|
|
const macchinaId = parseInt(id, 10);
|
||
|
|
|
||
|
|
if (isNaN(macchinaId)) {
|
||
|
|
return NextResponse.json({ error: "ID non valido" }, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const macchina = await getMacchinaById(macchinaId);
|
||
|
|
|
||
|
|
if (!macchina) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Macchina non trovata" },
|
||
|
|
{ status: 404 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return NextResponse.json({ macchina });
|
||
|
|
} catch (error) {
|
||
|
|
console.error("[API macchine/:id] Errore GET:", error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Errore nel recupero della macchina" },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function PUT(request: Request, { params }: RouteParams) {
|
||
|
|
const session = await auth();
|
||
|
|
|
||
|
|
if (!session?.user) {
|
||
|
|
return NextResponse.json({ error: "Non autorizzato" }, { status: 401 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const { id } = await params;
|
||
|
|
const macchinaId = parseInt(id, 10);
|
||
|
|
|
||
|
|
if (isNaN(macchinaId)) {
|
||
|
|
return NextResponse.json({ error: "ID non valido" }, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const body = await request.json();
|
||
|
|
|
||
|
|
const input: MacchinaUpdateInput = {};
|
||
|
|
if (body.nome !== undefined) input.nome = body.nome;
|
||
|
|
if (body.tipo !== undefined) input.tipo = body.tipo;
|
||
|
|
if (body.id_padre !== undefined) input.id_padre = body.id_padre;
|
||
|
|
if (body.id_stabilimento !== undefined) input.id_stabilimento = body.id_stabilimento;
|
||
|
|
if (body.colore_testo !== undefined) input.colore_testo = body.colore_testo;
|
||
|
|
|
||
|
|
const macchina = await updateMacchina(macchinaId, input);
|
||
|
|
|
||
|
|
if (!macchina) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Macchina non trovata" },
|
||
|
|
{ status: 404 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return NextResponse.json({ macchina });
|
||
|
|
} catch (error) {
|
||
|
|
console.error("[API macchine/:id] Errore PUT:", error);
|
||
|
|
const message = error instanceof Error ? error.message : "Errore nell'aggiornamento";
|
||
|
|
return NextResponse.json({ error: message }, { status: 500 });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function DELETE(request: Request, { params }: RouteParams) {
|
||
|
|
const session = await auth();
|
||
|
|
|
||
|
|
if (!session?.user) {
|
||
|
|
return NextResponse.json({ error: "Non autorizzato" }, { status: 401 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const { id } = await params;
|
||
|
|
const macchinaId = parseInt(id, 10);
|
||
|
|
|
||
|
|
if (isNaN(macchinaId)) {
|
||
|
|
return NextResponse.json({ error: "ID non valido" }, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const deleted = await deleteMacchina(macchinaId);
|
||
|
|
|
||
|
|
if (!deleted) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Macchina non trovata" },
|
||
|
|
{ status: 404 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return NextResponse.json({ success: true });
|
||
|
|
} catch (error) {
|
||
|
|
console.error("[API macchine/:id] Errore DELETE:", error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Errore nell'eliminazione" },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|