corretto logica di eliminazione di articoli da macchina/parte/gruppo
All checks were successful
Deploy to VPS / build (push) Successful in 38s
Deploy to VPS / deploy (push) Successful in 28s

This commit is contained in:
AV77web 2026-05-31 12:02:07 +02:00
parent 15a08b006d
commit b23f7d9da5
2 changed files with 24 additions and 3 deletions

View file

@ -1,7 +1,7 @@
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import { auth } from "@/src/auth"; import { auth } from "@/src/auth";
import { deleteArticle, updateArticle } from "@/src/lib/articles"; import { deleteArticle, updateArticle, getArticleById } from "@/src/lib/articles";
interface RouteContext { interface RouteContext {
params: Promise<{ id: string }>; params: Promise<{ id: string }>;
@ -18,6 +18,24 @@ async function readId(params: Promise<{ id: string }>): Promise<number> {
return articleId; return articleId;
} }
export async function GET(_request: Request, context: RouteContext) {
const session = await auth();
if (!session?.user) {
return NextResponse.json({ error: "Non autenticato" }, { status: 401 });
}
try {
const article = await getArticleById(await readId(context.params));
return NextResponse.json({ article });
} catch (error) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Articolo non trovato" },
{ status: 404 },
);
}
}
export async function PUT(request: Request, context: RouteContext) { export async function PUT(request: Request, context: RouteContext) {
const session = await auth(); const session = await auth();

View file

@ -62,9 +62,12 @@ export default function ArticoliMacchinaModal({
let articoloEsiste = true; let articoloEsiste = true;
try { try {
const res = await fetch(`/api/articoli/${articolo.id_articolo}`); const res = await fetch(`/api/articoli/${articolo.id_articolo}`);
articoloEsiste = res.ok; // Solo 404 indica che l'articolo non esiste
// Altri errori (401, 500, etc.) non significano che l'articolo non esiste
articoloEsiste = res.status !== 404;
} catch { } catch {
articoloEsiste = false; // In caso di errore di rete, assumiamo che l'articolo esista
articoloEsiste = true;
} }
setRimuoviModal({ setRimuoviModal({