60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import { writeFile } from 'fs/promises';
|
|
import { randomUUID } from 'crypto';
|
|
import { json } from '@sveltejs/kit';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
export async function POST({ request }) {
|
|
const formData = await request.formData();
|
|
|
|
const get = (key: string) => formData.get(key)?.toString() ?? '';
|
|
|
|
const pdfs = formData.getAll('pdfs') as File[];
|
|
|
|
const gespeichertePfade: string[] = [];
|
|
|
|
for (const pdf of pdfs) {
|
|
if (pdf.size > 0 && pdf.type === 'application/pdf') {
|
|
const buffer = Buffer.from(await pdf.arrayBuffer());
|
|
const dateiname = `${randomUUID()}.pdf`;
|
|
const pfad = `/uploads/${dateiname}`;
|
|
await writeFile(`static${pfad}`, buffer);
|
|
gespeichertePfade.push(pfad);
|
|
}
|
|
}
|
|
|
|
try {
|
|
await prisma.anmeldung.create({
|
|
data: {
|
|
anrede: get('anrede'),
|
|
vorname: get('vorname'),
|
|
nachname: get('nachname'),
|
|
geburtsdatum: get('geburtsdatum'),
|
|
strasse: get('strasse'),
|
|
hausnummer: get('hausnummer'),
|
|
ort: get('ort'),
|
|
plz: get('plz'),
|
|
telefon: get('telefon'),
|
|
email: get('email'),
|
|
schulart: get('schulart'),
|
|
motivation: get('motivation'),
|
|
wunsch1Id: parseInt(get('wunsch1Id')),
|
|
wunsch2Id: parseInt(get('wunsch2Id')),
|
|
wunsch3Id: parseInt(get('wunsch3Id')),
|
|
pdfs: {
|
|
create: gespeichertePfade.map((pfad) => ({ pfad }))
|
|
}
|
|
}
|
|
});
|
|
|
|
return json({ success: true });
|
|
} catch (err: unknown) {
|
|
if (err instanceof Error && (err as { code?: string }).code === 'P2002') {
|
|
return json({ error: 'Diese E-Mail wurde bereits verwendet.' }, { status: 400 });
|
|
}
|
|
|
|
console.error(err);
|
|
return json({ error: 'Fehler beim Speichern.' }, { status: 500 });
|
|
}
|
|
} |