13 +server.ts handler nutzen jetzt parsePositiveIntParam und
validateBody statt jeweils lokaler parseId/safeParse-Bloecke.
Konsequenzen:
- 9 lokale parseId/parsePositiveInt Definitionen geloescht
- 11 safeParse + manueller error()-Throws ersetzt
- domains/[id], domains, profiles: catch-Block reicht jetzt HttpError
durch (isHttpError) — vorher wurde ein 404 vom updateDomain als 409
re-emittiert
- recipes/[id]/image: kein function-clutter mehr neben den FormData-Pfaden
- Konsistente Error-Bodies: validateBody schickt {message, issues},
parsePositiveIntParam {message: 'Missing X' / 'Invalid X'}
Findings aus REVIEW-2026-04-18.md (Refactor A) und redundancy.md
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
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 {
|
|
addToWishlist,
|
|
listWishlist,
|
|
type SortKey
|
|
} from '$lib/server/wishlist/repository';
|
|
|
|
const AddSchema = z.object({
|
|
recipe_id: z.number().int().positive(),
|
|
profile_id: z.number().int().positive()
|
|
});
|
|
|
|
const VALID_SORTS: readonly SortKey[] = ['popular', 'newest', 'oldest'] as const;
|
|
|
|
function parseSort(raw: string | null): SortKey {
|
|
return VALID_SORTS.includes(raw as SortKey) ? (raw as SortKey) : 'popular';
|
|
}
|
|
|
|
function parseProfileId(raw: string | null): number | null {
|
|
if (!raw) return null;
|
|
const n = Number(raw);
|
|
return Number.isInteger(n) && n > 0 ? n : null;
|
|
}
|
|
|
|
export const GET: RequestHandler = async ({ url }) => {
|
|
const sort = parseSort(url.searchParams.get('sort'));
|
|
const profileId = parseProfileId(url.searchParams.get('profile_id'));
|
|
return json({ sort, entries: listWishlist(getDb(), profileId, sort) });
|
|
};
|
|
|
|
export const POST: RequestHandler = async ({ request }) => {
|
|
const data = validateBody(await request.json().catch(() => null), AddSchema);
|
|
addToWishlist(getDb(), data.recipe_id, data.profile_id);
|
|
return json({ ok: true }, { status: 201 });
|
|
};
|