import type { RequestHandler } from './$types'; import { json, error } from '@sveltejs/kit'; import { z } from 'zod'; import { getDb } from '$lib/server/db'; import { deleteRecipe, getRecipeById } from '$lib/server/recipes/repository'; import { listComments, listCookingLog, listRatings, renameRecipe } from '$lib/server/recipes/actions'; const RenameSchema = z.object({ title: z.string().min(1).max(200) }); function parseId(raw: string): number { const id = Number(raw); if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' }); return id; } export const GET: RequestHandler = async ({ params }) => { const id = parseId(params.id!); const db = getDb(); const recipe = getRecipeById(db, id); if (!recipe) error(404, { message: 'Recipe not found' }); const ratings = listRatings(db, id); const comments = listComments(db, id); const cooking_log = listCookingLog(db, id); const avg_stars = ratings.length === 0 ? null : ratings.reduce((s, r) => s + r.stars, 0) / ratings.length; return json({ recipe, ratings, comments, cooking_log, avg_stars }); }; export const PATCH: RequestHandler = async ({ params, request }) => { const id = parseId(params.id!); const body = await request.json().catch(() => null); const parsed = RenameSchema.safeParse(body); if (!parsed.success) error(400, { message: 'Invalid body' }); renameRecipe(getDb(), id, parsed.data.title); return json({ ok: true }); }; export const DELETE: RequestHandler = async ({ params }) => { const id = parseId(params.id!); deleteRecipe(getDb(), id); return json({ ok: true }); };