import type { RequestHandler } from './$types'; import { json, error } from '@sveltejs/kit'; import { z } from 'zod'; import { getDb } from '$lib/server/db'; import { clearRating, setRating } from '$lib/server/recipes/actions'; const Schema = z.object({ profile_id: z.number().int().positive(), stars: z.number().int().min(1).max(5) }); const DeleteSchema = z.object({ profile_id: z.number().int().positive() }); function parseId(raw: string): number { const id = Number(raw); if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' }); return id; } export const PUT: RequestHandler = async ({ params, request }) => { const id = parseId(params.id!); const body = await request.json().catch(() => null); const parsed = Schema.safeParse(body); if (!parsed.success) error(400, { message: 'Invalid body' }); setRating(getDb(), id, parsed.data.profile_id, parsed.data.stars); return json({ ok: true }); }; export const DELETE: RequestHandler = async ({ params, request }) => { const id = parseId(params.id!); const body = await request.json().catch(() => null); const parsed = DeleteSchema.safeParse(body); if (!parsed.success) error(400, { message: 'Invalid body' }); clearRating(getDb(), id, parsed.data.profile_id); return json({ ok: true }); };