39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getAufgaben } from '@/lib/db';
|
|
|
|
// CSV-Header definieren
|
|
const CSV_HEADER = 'ID,Beschreibung,Erledigt,Erstellt\n';
|
|
|
|
// Aufgabe in CSV-Zeile konvertieren
|
|
function aufgabeToCsvRow(aufgabe: any): string {
|
|
const erledigt = aufgabe.erledigt ? 'Ja' : 'Nein';
|
|
const erstellt = new Date(aufgabe.erstellt).toLocaleString('de-DE');
|
|
// Kommas in der Beschreibung durch Semikolon ersetzen und Anführungszeichen escapen
|
|
const beschreibung = `"${aufgabe.beschreibung.replace(/"/g, '""')}"`;
|
|
return `${aufgabe.id},${beschreibung},${erledigt},"${erstellt}"`;
|
|
}
|
|
|
|
// GET /api/tasks/export - CSV-Export
|
|
export async function GET() {
|
|
try {
|
|
const aufgaben = await getAufgaben();
|
|
|
|
// CSV generieren
|
|
const csvRows = aufgaben.map(aufgabeToCsvRow);
|
|
const csvContent = CSV_HEADER + csvRows.join('\n');
|
|
|
|
// CSV als Datei-Download zurückgeben
|
|
return new NextResponse(csvContent, {
|
|
headers: {
|
|
'Content-Type': 'text/csv; charset=utf-8',
|
|
'Content-Disposition': 'attachment; filename="aufgaben.csv"',
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error('Fehler beim CSV-Export:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Fehler beim Erstellen des CSV-Exports' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|