2026-04-17 15:23:00 +02:00
|
|
|
import type { RequestHandler } from './$types';
|
2026-04-18 22:19:12 +02:00
|
|
|
import { json } from '@sveltejs/kit';
|
2026-04-17 15:23:00 +02:00
|
|
|
import { z } from 'zod';
|
|
|
|
|
import { getDb } from '$lib/server/db';
|
2026-04-18 22:19:12 +02:00
|
|
|
import { parsePositiveIntParam, validateBody } from '$lib/server/api-helpers';
|
2026-04-17 15:23:00 +02:00
|
|
|
import { addComment, deleteComment, listComments } from '$lib/server/recipes/actions';
|
|
|
|
|
|
|
|
|
|
const Schema = z.object({
|
|
|
|
|
profile_id: z.number().int().positive(),
|
|
|
|
|
text: z.string().min(1).max(2000)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const DeleteSchema = z.object({ comment_id: z.number().int().positive() });
|
|
|
|
|
|
|
|
|
|
export const GET: RequestHandler = async ({ params }) => {
|
2026-04-18 22:19:12 +02:00
|
|
|
const id = parsePositiveIntParam(params.id, 'id');
|
2026-04-17 15:23:00 +02:00
|
|
|
return json(listComments(getDb(), id));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const POST: RequestHandler = async ({ params, request }) => {
|
2026-04-18 22:19:12 +02:00
|
|
|
const id = parsePositiveIntParam(params.id, 'id');
|
|
|
|
|
const data = validateBody(await request.json().catch(() => null), Schema);
|
|
|
|
|
const cid = addComment(getDb(), id, data.profile_id, data.text);
|
2026-04-17 15:23:00 +02:00
|
|
|
return json({ id: cid }, { status: 201 });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const DELETE: RequestHandler = async ({ request }) => {
|
2026-04-18 22:19:12 +02:00
|
|
|
const data = validateBody(await request.json().catch(() => null), DeleteSchema);
|
|
|
|
|
deleteComment(getDb(), data.comment_id);
|
2026-04-17 15:23:00 +02:00
|
|
|
return json({ ok: true });
|
|
|
|
|
};
|