Files
ausgaben-next/app/api/ausgaben/[id]/route.ts
2026-03-01 11:26:44 +00:00

90 lines
2.1 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getDbPool } from '@/lib/db';
import { ResultSetHeader } from 'mysql2';
// PUT /api/ausgaben/[id] - Update entry
export async function PUT(
request: Request,
context: { params: Promise<{ id: string }> }
) {
try {
const { id } = await context.params;
const body = await request.json();
const { Datum, Wo, Was, Kat, Wieviel, Wie, TYP } = body;
if (!Datum || !Wo || !Was || !Wieviel || !Wie || TYP === undefined) {
return NextResponse.json(
{ success: false, error: 'Missing required fields' },
{ status: 400 }
);
}
const pool = getDbPool();
const query = `
UPDATE Ausgaben
SET Datum = ?, Wo = ?, Was = ?, Kat = ?, Wieviel = ?, Wie = ?, TYP = ?
WHERE ID = ?
`;
const [result] = await pool.query<ResultSetHeader>(query, [
Datum,
Wo,
Was,
Kat || 'L',
parseFloat(Wieviel),
Wie,
TYP,
parseInt(id),
]);
if (result.affectedRows === 0) {
return NextResponse.json(
{ success: false, error: 'Entry not found' },
{ status: 404 }
);
}
return NextResponse.json({
success: true,
});
} catch (error) {
console.error('Database error:', error);
return NextResponse.json(
{ success: false, error: 'Database error' },
{ status: 500 }
);
}
}
// DELETE /api/ausgaben/[id] - Delete entry
export async function DELETE(
request: Request,
context: { params: Promise<{ id: string }> }
) {
try {
const { id } = await context.params;
const pool = getDbPool();
const query = 'DELETE FROM Ausgaben WHERE ID = ?';
const [result] = await pool.query<ResultSetHeader>(query, [parseInt(id)]);
if (result.affectedRows === 0) {
return NextResponse.json(
{ success: false, error: 'Entry not found' },
{ status: 404 }
);
}
return NextResponse.json({
success: true,
});
} catch (error) {
console.error('Database error:', error);
return NextResponse.json(
{ success: false, error: 'Database error' },
{ status: 500 }
);
}
}