refactor(api): alle handler auf api-helpers umstellen
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
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import type { RequestHandler } from './$types';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
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,
|
||||
@@ -32,9 +33,7 @@ export const GET: RequestHandler = async ({ url }) => {
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
const body = await request.json().catch(() => null);
|
||||
const parsed = AddSchema.safeParse(body);
|
||||
if (!parsed.success) error(400, { message: 'recipe_id and profile_id required' });
|
||||
addToWishlist(getDb(), parsed.data.recipe_id, parsed.data.profile_id);
|
||||
const data = validateBody(await request.json().catch(() => null), AddSchema);
|
||||
addToWishlist(getDb(), data.recipe_id, data.profile_id);
|
||||
return json({ ok: true }, { status: 201 });
|
||||
};
|
||||
|
||||
@@ -1,26 +1,21 @@
|
||||
import type { RequestHandler } from './$types';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { getDb } from '$lib/server/db';
|
||||
import { parsePositiveIntParam } from '$lib/server/api-helpers';
|
||||
import {
|
||||
removeFromWishlist,
|
||||
removeFromWishlistForAll
|
||||
} from '$lib/server/wishlist/repository';
|
||||
|
||||
function parsePositiveInt(raw: string | null, field: string): number {
|
||||
const n = raw === null ? NaN : Number(raw);
|
||||
if (!Number.isInteger(n) || n <= 0) error(400, { message: `Invalid ${field}` });
|
||||
return n;
|
||||
}
|
||||
|
||||
// DELETE /api/wishlist/:id?profile_id=X → entfernt nur den eigenen Wunsch
|
||||
// DELETE /api/wishlist/:id?all=true → entfernt für ALLE Profile
|
||||
export const DELETE: RequestHandler = async ({ params, url }) => {
|
||||
const id = parsePositiveInt(params.recipe_id!, 'recipe_id');
|
||||
const id = parsePositiveIntParam(params.recipe_id, 'recipe_id');
|
||||
const db = getDb();
|
||||
if (url.searchParams.get('all') === 'true') {
|
||||
removeFromWishlistForAll(db, id);
|
||||
} else {
|
||||
const profileId = parsePositiveInt(url.searchParams.get('profile_id'), 'profile_id');
|
||||
const profileId = parsePositiveIntParam(url.searchParams.get('profile_id'), 'profile_id');
|
||||
removeFromWishlist(db, id, profileId);
|
||||
}
|
||||
return json({ ok: true });
|
||||
|
||||
Reference in New Issue
Block a user