feat(api): add recipe detail, search, rating, favorite, cooked, comments endpoints
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
40
src/routes/api/recipes/[id]/comments/+server.ts
Normal file
40
src/routes/api/recipes/[id]/comments/+server.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { RequestHandler } from './$types';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import { z } from 'zod';
|
||||
import { getDb } from '$lib/server/db';
|
||||
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() });
|
||||
|
||||
function parseId(raw: string): number {
|
||||
const id = Number(raw);
|
||||
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
|
||||
return id;
|
||||
}
|
||||
|
||||
export const GET: RequestHandler = async ({ params }) => {
|
||||
const id = parseId(params.id!);
|
||||
return json(listComments(getDb(), id));
|
||||
};
|
||||
|
||||
export const POST: 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' });
|
||||
const cid = addComment(getDb(), id, parsed.data.profile_id, parsed.data.text);
|
||||
return json({ id: cid }, { status: 201 });
|
||||
};
|
||||
|
||||
export const DELETE: RequestHandler = async ({ request }) => {
|
||||
const body = await request.json().catch(() => null);
|
||||
const parsed = DeleteSchema.safeParse(body);
|
||||
if (!parsed.success) error(400, { message: 'Invalid body' });
|
||||
deleteComment(getDb(), parsed.data.comment_id);
|
||||
return json({ ok: true });
|
||||
};
|
||||
Reference in New Issue
Block a user