aboutsummaryrefslogtreecommitdiff
path: root/src/routes/+page.server.ts
blob: ac00aeb0d0304ad7fbe28b6929c4fd41cfee9221 (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

import { fail } from '@sveltejs/kit';
import { projectsService, routinesService } from '$lib/server/services';
import { parseFormString } from '$lib/server/services/validation';
import { isUniqueViolation } from '$lib/server/db/errors';
import type { Actions, PageServerLoad } from './$types';

export const load: PageServerLoad = async () => {
	return routinesService.loadOverview(Temporal.Now.instant());
};

export const actions: Actions = {
	addProject: async ({ request }) => {
		const name = parseFormString((await request.formData()).get('name'));
		if (!name) {
			return fail(400, { error: 'Le nom est requis' });
		}
		try {
			await projectsService.add(name);
		} catch (error) {
			if (isUniqueViolation(error)) {
				return fail(400, { error: 'Ce projet existe déjà' });
			}
			throw error;
		}
		return { success: true };
	},
};