diff --git a/src/routes/api/shopping-list/recipe/+server.ts b/src/routes/api/shopping-list/recipe/+server.ts new file mode 100644 index 0000000..fd3dbec --- /dev/null +++ b/src/routes/api/shopping-list/recipe/+server.ts @@ -0,0 +1,18 @@ +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 { addRecipeToCart } from '$lib/server/shopping/repository'; + +const AddSchema = z.object({ + recipe_id: z.number().int().positive(), + servings: z.number().int().min(1).max(50).optional(), + profile_id: z.number().int().positive().optional() +}); + +export const POST: RequestHandler = async ({ request }) => { + const data = validateBody(await request.json().catch(() => null), AddSchema); + addRecipeToCart(getDb(), data.recipe_id, data.profile_id ?? null, data.servings); + return json({ ok: true }, { status: 201 }); +};