Code-Review-Finding zu commit 82d4348: das Sibling-Endpoint
recipes/[id]/+server.ts nutzt schon parsePositiveIntParam aus
api-helpers.ts. Der neue View-Endpoint hatte die Logik inline
nachgebaut — jetzt aufgeraeumt fuer Konsistenz.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
28 lines
916 B
TypeScript
28 lines
916 B
TypeScript
import type { RequestHandler } from './$types';
|
|
import { z } from 'zod';
|
|
import { error } from '@sveltejs/kit';
|
|
import { getDb } from '$lib/server/db';
|
|
import { validateBody, parsePositiveIntParam } from '$lib/server/api-helpers';
|
|
import { recordView } from '$lib/server/recipes/views';
|
|
|
|
const Schema = z.object({
|
|
profile_id: z.number().int().positive()
|
|
});
|
|
|
|
export const POST: RequestHandler = async ({ params, request }) => {
|
|
const recipeId = parsePositiveIntParam(params.id, 'id');
|
|
const body = validateBody(await request.json().catch(() => null), Schema);
|
|
|
|
try {
|
|
recordView(getDb(), body.profile_id, recipeId);
|
|
} catch (e) {
|
|
// FK violation (unknown profile or recipe) → 404
|
|
if (e instanceof Error && /FOREIGN KEY constraint failed/i.test(e.message)) {
|
|
error(404, { message: 'Recipe or profile not found' });
|
|
}
|
|
throw e;
|
|
}
|
|
|
|
return new Response(null, { status: 204 });
|
|
};
|