- Backend: new POST /api/persons endpoint in PersonController - Frontend: new /persons/new route with Vorname/Nachname/Alias form, redirects to the new person's detail page on success - Persons list: subtle '+ Neue Person' link below the page title Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
27 lines
870 B
TypeScript
27 lines
870 B
TypeScript
import { fail, redirect } from '@sveltejs/kit';
|
|
import { createApiClient } from '$lib/api.server';
|
|
|
|
export const actions = {
|
|
default: async ({ request, fetch }) => {
|
|
const formData = await request.formData();
|
|
const firstName = formData.get('firstName')?.toString().trim();
|
|
const lastName = formData.get('lastName')?.toString().trim();
|
|
const alias = formData.get('alias')?.toString().trim() || undefined;
|
|
|
|
if (!firstName || !lastName) {
|
|
return fail(400, { error: 'Vor- und Nachname sind Pflichtfelder.' });
|
|
}
|
|
|
|
const api = createApiClient(fetch);
|
|
const result = await api.POST('/api/persons', {
|
|
body: { firstName, lastName, ...(alias ? { alias } : {}) }
|
|
});
|
|
|
|
if (!result.response.ok) {
|
|
return fail(result.response.status, { error: 'Person konnte nicht gespeichert werden.' });
|
|
}
|
|
|
|
throw redirect(303, `/persons/${result.data!.id}`);
|
|
}
|
|
};
|