import type { RequestHandler } from './$types'; import { json } from '@sveltejs/kit'; import { z } from 'zod'; import { getDb } from '$lib/server/db'; import { validateBody } from '$lib/server/api-helpers'; import { addToWishlist, listWishlist, type SortKey } from '$lib/server/wishlist/repository'; const AddSchema = z.object({ recipe_id: z.number().int().positive(), profile_id: z.number().int().positive() }); const VALID_SORTS: readonly SortKey[] = ['popular', 'newest', 'oldest'] as const; function parseSort(raw: string | null): SortKey { return VALID_SORTS.includes(raw as SortKey) ? (raw as SortKey) : 'popular'; } function parseProfileId(raw: string | null): number | null { if (!raw) return null; const n = Number(raw); return Number.isInteger(n) && n > 0 ? n : null; } export const GET: RequestHandler = async ({ url }) => { const sort = parseSort(url.searchParams.get('sort')); const profileId = parseProfileId(url.searchParams.get('profile_id')); return json({ sort, entries: listWishlist(getDb(), profileId, sort) }); }; export const POST: RequestHandler = async ({ request }) => { const data = validateBody(await request.json().catch(() => null), AddSchema); addToWishlist(getDb(), data.recipe_id, data.profile_id); return json({ ok: true }, { status: 201 }); };