import { apiClient } from '$lib/server/api'; const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; function isValidUuid(value: string | null): value is string { return typeof value === 'string' && UUID_RE.test(value); } export async function addSlotAction({ fetch, request }: { fetch: typeof globalThis.fetch; request: Request }) { const formData = await request.formData(); const planId = formData.get('planId') as string | null; const slotDate = formData.get('slotDate') as string | null; const recipeId = formData.get('recipeId') as string | null; if (!isValidUuid(planId) || !isValidUuid(recipeId) || !slotDate) { return { success: false, error: 'Ungültige Eingabe.' }; } const api = apiClient(fetch); const { data, error } = await api.POST('/v1/week-plans/{id}/slots', { params: { path: { id: planId } }, body: { slotDate, recipeId } }); if (error || !data) return { success: false }; return { success: true, slot: data }; } export async function updateSlotAction({ fetch, request }: { fetch: typeof globalThis.fetch; request: Request }) { const formData = await request.formData(); const planId = formData.get('planId') as string | null; const slotId = formData.get('slotId') as string | null; const recipeId = formData.get('recipeId') as string | null; if (!isValidUuid(planId) || !isValidUuid(slotId) || !isValidUuid(recipeId)) { return { success: false, error: 'Ungültige Eingabe.' }; } const api = apiClient(fetch); const { data, error } = await api.PATCH('/v1/week-plans/{planId}/slots/{slotId}', { params: { path: { planId, slotId } }, body: { recipeId } }); if (error || !data) return { success: false }; return { success: true, slot: data }; } export async function deleteSlotAction({ fetch, request }: { fetch: typeof globalThis.fetch; request: Request }) { const formData = await request.formData(); const planId = formData.get('planId') as string | null; const slotId = formData.get('slotId') as string | null; if (!isValidUuid(planId) || !isValidUuid(slotId)) { return { success: false, error: 'Ungültige Eingabe.' }; } const api = apiClient(fetch); const { error } = await api.DELETE('/v1/week-plans/{planId}/slots/{slotId}', { params: { path: { planId, slotId } } }); if (error) return { success: false }; return { success: true }; }