Files
kochwas/src/routes/api/wishlist/+server.ts

41 lines
1.4 KiB
TypeScript
Raw Normal View History

import type { RequestHandler } from './$types';
import { json, error } from '@sveltejs/kit';
import { z } from 'zod';
import { getDb } from '$lib/server/db';
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().nullable().optional()
});
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 body = await request.json().catch(() => null);
const parsed = AddSchema.safeParse(body);
if (!parsed.success) error(400, { message: 'Invalid body' });
addToWishlist(getDb(), parsed.data.recipe_id, parsed.data.profile_id ?? null);
return json({ ok: true }, { status: 201 });
};