feat(shopping): PATCH/DELETE /api/shopping-list/recipe/[id]
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
Some checks failed
Build & Publish Docker Image / build-and-push (push) Has been cancelled
This commit is contained in:
23
src/routes/api/shopping-list/recipe/[recipe_id]/+server.ts
Normal file
23
src/routes/api/shopping-list/recipe/[recipe_id]/+server.ts
Normal file
@@ -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 });
|
||||
};
|
||||
Reference in New Issue
Block a user