aboutsummaryrefslogtreecommitdiff
path: root/src/routes/catalog/+page.server.ts
blob: 69a775aa62a3c43191164d54b96bc8d353117cdb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

import { fail } from '@sveltejs/kit';
import { loadAll, toggle } from '$lib/server/services/groceries';
import { numericId } from '$lib/server/services/validation';
import type { EnrichedGrocery } from '$lib/server/services/groceries';
import type { Category } from '$lib/server/services/types';
import type { Actions, PageServerLoad } from './$types';

export interface CatalogOutput {
	items: EnrichedGrocery[];
	categories: Category[];
}

export const load: PageServerLoad = async () => {
	return loadAll();
};

export const actions: Actions = {
	toggle: async ({ request }) => {
		const data = await request.formData();
		try {
			const id = numericId(data.get('id'));
			await toggle(id);
		} catch {
			return fail(400, { error: 'ID invalide' });
		}
		return { success: true };
	},
};