diff --git a/src/routes/api/shopping-list/recipe/[recipe_id]/+server.ts b/src/routes/api/shopping-list/recipe/[recipe_id]/+server.ts new file mode 100644 index 0000000..301b539 --- /dev/null +++ b/src/routes/api/shopping-list/recipe/[recipe_id]/+server.ts @@ -0,0 +1,23 @@ +import type { RequestHandler } from './$types'; +import { json } from '@sveltejs/kit'; +import { z } from 'zod'; +import { getDb } from '$lib/server/db'; +import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers'; +import { removeRecipeFromCart, setCartServings } from '$lib/server/shopping/repository'; + +const PatchSchema = z.object({ + servings: z.number().int().min(1).max(50) +}); + +export const PATCH: RequestHandler = async ({ params, request }) => { + const id = parsePositiveIntParam(params.recipe_id, 'recipe_id'); + const data = validateBody(await request.json().catch(() => null), PatchSchema); + setCartServings(getDb(), id, data.servings); + return json({ ok: true }); +}; + +export const DELETE: RequestHandler = async ({ params }) => { + const id = parsePositiveIntParam(params.recipe_id, 'recipe_id'); + removeRecipeFromCart(getDb(), id); + return json({ ok: true }); +};