First commit - es tut schon mal ganz gut
This commit is contained in:
82
app/api/ausgaben/[id]/route.ts
Normal file
82
app/api/ausgaben/[id]/route.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
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, WochTag, Wo, Was, Wieviel, Wie, OK } = body;
|
||||
|
||||
const pool = getDbPool();
|
||||
|
||||
const query = `
|
||||
UPDATE Ausgaben_Tag
|
||||
SET Datum = ?, WochTag = ?, Wo = ?, Was = ?, Wieviel = ?, Wie = ?, OK = ?
|
||||
WHERE ID = ?
|
||||
`;
|
||||
|
||||
const [result] = await pool.query<ResultSetHeader>(query, [
|
||||
Datum,
|
||||
WochTag,
|
||||
Wo,
|
||||
Was,
|
||||
parseFloat(Wieviel),
|
||||
Wie,
|
||||
OK || 0,
|
||||
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_Tag 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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user