aboutsummaryrefslogtreecommitdiff
path: root/src/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/+page.server.ts15
-rw-r--r--src/routes/groceries/+page.server.ts6
-rw-r--r--src/routes/groceries/catalog/+page.server.ts9
-rw-r--r--src/routes/groceries/categories/+page.server.ts13
-rw-r--r--src/routes/grocery/[id]/+page.server.ts44
-rw-r--r--src/routes/projects/[id]/+page.server.ts23
-rw-r--r--src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts9
7 files changed, 65 insertions, 54 deletions
diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts
index fe9b4e3..ac00aeb 100644
--- a/src/routes/+page.server.ts
+++ b/src/routes/+page.server.ts
@@ -1,11 +1,11 @@
import { fail } from '@sveltejs/kit';
-import { loadOverview } from '$lib/server/services/routines';
-import { add as addProject } from '$lib/server/services/projects';
+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 loadOverview(Temporal.Now.instant());
+ return routinesService.loadOverview(Temporal.Now.instant());
};
export const actions: Actions = {
@@ -14,7 +14,14 @@ export const actions: Actions = {
if (!name) {
return fail(400, { error: 'Le nom est requis' });
}
- await addProject(name);
+ try {
+ await projectsService.add(name);
+ } catch (error) {
+ if (isUniqueViolation(error)) {
+ return fail(400, { error: 'Ce projet existe déjà' });
+ }
+ throw error;
+ }
return { success: true };
},
};
diff --git a/src/routes/groceries/+page.server.ts b/src/routes/groceries/+page.server.ts
index 1cae275..d33c6a5 100644
--- a/src/routes/groceries/+page.server.ts
+++ b/src/routes/groceries/+page.server.ts
@@ -1,4 +1,4 @@
-import { loadUnchecked, loadAll } from '$lib/server/services/groceries';
+import { groceriesService } from '$lib/server/services';
import {
addGroceryAction,
addGroceryAndEditAction,
@@ -15,8 +15,8 @@ export interface GroceriesOutput {
}
export const load: PageServerLoad = async () => {
- const unchecked = await loadUnchecked();
- const all = await loadAll();
+ const unchecked = await groceriesService.loadUnchecked();
+ const all = await groceriesService.loadAll();
return {
items: unchecked.items,
categories: unchecked.categories,
diff --git a/src/routes/groceries/catalog/+page.server.ts b/src/routes/groceries/catalog/+page.server.ts
index fd71e30..8d94154 100644
--- a/src/routes/groceries/catalog/+page.server.ts
+++ b/src/routes/groceries/catalog/+page.server.ts
@@ -1,8 +1,5 @@
import { fail } from '@sveltejs/kit';
-import {
- loadAll,
- bulkUpdateGroceryCategory,
-} from '$lib/server/services/groceries';
+import { groceriesService } from '$lib/server/services';
import { toggleGroceryAction } from '$lib/server/services/grocery-actions';
import {
numericId,
@@ -18,7 +15,7 @@ export interface CatalogOutput {
}
export const load: PageServerLoad = async () => {
- return loadAll();
+ return groceriesService.loadAll();
};
export const actions: Actions = {
@@ -34,7 +31,7 @@ export const actions: Actions = {
return fail(400, { error: 'Aucun article sélectionné' });
}
- await bulkUpdateGroceryCategory(ids, categoryId);
+ await groceriesService.bulkUpdateGroceryCategory(ids, categoryId);
return { success: true, type: 'bulkUpdate' as const };
},
};
diff --git a/src/routes/groceries/categories/+page.server.ts b/src/routes/groceries/categories/+page.server.ts
index 6e0e3b7..e8794fa 100644
--- a/src/routes/groceries/categories/+page.server.ts
+++ b/src/routes/groceries/categories/+page.server.ts
@@ -1,9 +1,5 @@
import { fail } from '@sveltejs/kit';
-import {
- loadAll,
- deleteCategory,
- reorder,
-} from '$lib/server/services/categories';
+import { categoriesService } from '$lib/server/services';
import {
addCategoryAction,
idAction,
@@ -17,13 +13,14 @@ export interface CategoriesOutput {
}
export const load: PageServerLoad = async () => {
- return { categories: await loadAll() };
+ return { categories: await categoriesService.loadAll() };
};
export const actions: Actions = {
add: ({ request }) => addCategoryAction(request),
- delete: ({ request }) => idAction(request, deleteCategory),
+ delete: ({ request }) =>
+ idAction(request, (id) => categoriesService.deleteCategory(id)),
reorder: async ({ request }) => {
const data = await request.formData();
@@ -31,7 +28,7 @@ export const actions: Actions = {
if (ids.length === 0) {
return fail(400, { error: 'Aucune catégorie' });
}
- await reorder(ids);
+ await categoriesService.reorder(ids);
return { success: true };
},
};
diff --git a/src/routes/grocery/[id]/+page.server.ts b/src/routes/grocery/[id]/+page.server.ts
index a4ad377..dbfecff 100644
--- a/src/routes/grocery/[id]/+page.server.ts
+++ b/src/routes/grocery/[id]/+page.server.ts
@@ -2,20 +2,18 @@ import { error, fail, redirect } from '@sveltejs/kit';
import { db } from '$lib/server/db';
import { groceries, categories } from '$lib/server/db/schema';
import { eq } from 'drizzle-orm';
-import { loadAll as loadCategories } from '$lib/server/services/categories';
import {
- loadForGrocery,
- add as addRef,
- promote,
- deleteReference,
-} from '$lib/server/services/references';
-import { updateGrocery, deleteGrocery } from '$lib/server/services/groceries';
+ categoriesService,
+ groceriesService,
+ referencesService,
+} from '$lib/server/services';
import { idAction } from '$lib/server/services/grocery-actions';
import {
numericId,
parseFormString,
parseOptionalNumber,
} from '$lib/server/services/validation';
+import { isUniqueViolation } from '$lib/server/db/errors';
import type { Category, Grocery, Reference } from '$lib/server/services/types';
import type { Actions, PageServerLoad } from './$types';
@@ -38,8 +36,8 @@ export const load: PageServerLoad = async ({ params, url }) => {
.where(eq(categories.id, item.categoryId))
: [null];
- const refs = await loadForGrocery(id);
- const cats = await loadCategories();
+ const refs = await referencesService.loadForGrocery(id);
+ const cats = await categoriesService.loadAll();
const from = url.searchParams.get('from') || '/';
return {
@@ -66,14 +64,25 @@ export const actions: Actions = {
return fail(400, { error: 'La référence est requise' });
}
- await addRef(groceryId, reference, price, unit, qualityScore);
+ await referencesService.add(
+ groceryId,
+ reference,
+ price,
+ unit,
+ qualityScore,
+ );
return { success: true, type: 'add' as const };
},
delete: ({ request }) =>
- idAction(request, deleteReference, { type: 'delete' }),
+ idAction(request, (id) => referencesService.deleteReference(id), {
+ type: 'delete',
+ }),
- promote: ({ request }) => idAction(request, promote, { type: 'promote' }),
+ promote: ({ request }) =>
+ idAction(request, (id) => referencesService.promote(id), {
+ type: 'promote',
+ }),
update: async ({ request, params }) => {
const data = await request.formData();
@@ -86,13 +95,20 @@ export const actions: Actions = {
return fail(400, { error: 'Le nom est requis' });
}
- await updateGrocery(groceryId, name, categoryId);
+ try {
+ await groceriesService.updateGrocery(groceryId, name, categoryId);
+ } catch (error) {
+ if (isUniqueViolation(error)) {
+ return fail(400, { error: 'Un article porte déjà ce nom' });
+ }
+ throw error;
+ }
return { success: true, type: 'update' as const };
},
deleteItem: async ({ params, request }) => {
const groceryId = numericId(params.id);
- await deleteGrocery(groceryId);
+ await groceriesService.deleteGrocery(groceryId);
const data = await request.formData();
const from = parseFormString(data.get('from')) || '/';
throw redirect(303, from);
diff --git a/src/routes/projects/[id]/+page.server.ts b/src/routes/projects/[id]/+page.server.ts
index 83d7571..1df28c2 100644
--- a/src/routes/projects/[id]/+page.server.ts
+++ b/src/routes/projects/[id]/+page.server.ts
@@ -1,13 +1,5 @@
import { error, fail, redirect } from '@sveltejs/kit';
-import {
- archive as archiveProject,
- getById,
-} from '$lib/server/services/projects';
-import {
- add,
- loadProjectDashboard,
- loadArchived,
-} from '$lib/server/services/routines';
+import { projectsService, routinesService } from '$lib/server/services';
import { completeRoutineAction } from '$lib/server/services/routine-actions';
import { numericId, parseFormString } from '$lib/server/services/validation';
import { Frequency } from '$lib/shared/frequency';
@@ -15,12 +7,15 @@ import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const id = numericId(params.id);
- const project = await getById(id);
+ const project = await projectsService.getById(id);
if (!project || project.archived) {
throw error(404, 'Projet introuvable');
}
- const dashboard = await loadProjectDashboard(id, Temporal.Now.instant());
- const archived = await loadArchived(id);
+ const dashboard = await routinesService.loadProjectDashboard(
+ id,
+ Temporal.Now.instant(),
+ );
+ const archived = await routinesService.loadArchived(id);
return {
project: { id: project.id, name: project.name },
routines: dashboard.routines,
@@ -38,7 +33,7 @@ export const actions: Actions = {
return fail(400, { error: 'Le nom est requis' });
}
try {
- await add(
+ await routinesService.add(
numericId(params.id),
name,
Frequency.parse(data.get('frequency')?.toString() ?? '').toSpec(),
@@ -55,7 +50,7 @@ export const actions: Actions = {
},
archiveProject: async ({ params }) => {
- await archiveProject(numericId(params.id));
+ await projectsService.archive(numericId(params.id));
throw redirect(303, '/');
},
};
diff --git a/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts b/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts
index cb14e0d..4ace86f 100644
--- a/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts
+++ b/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts
@@ -1,6 +1,5 @@
import { error } from '@sveltejs/kit';
-import { getById } from '$lib/server/services/projects';
-import { loadCurrent, loadCompletions } from '$lib/server/services/routines';
+import { projectsService, routinesService } from '$lib/server/services';
import {
archiveRoutineAction,
completeRoutineAction,
@@ -14,17 +13,17 @@ export const load: PageServerLoad = async ({ params }) => {
const projectId = numericId(params.projectId);
const routineId = numericId(params.routineId);
- const project = await getById(projectId);
+ const project = await projectsService.getById(projectId);
if (!project || project.archived) {
throw error(404, 'Projet introuvable');
}
- const routine = await loadCurrent(routineId);
+ const routine = await routinesService.loadCurrent(routineId);
if (!routine || routine.projectId !== projectId) {
throw error(404, 'Routine introuvable');
}
- const completions = await loadCompletions(routineId);
+ const completions = await routinesService.loadCompletions(routineId);
// Archived routines are frozen at their archive instant, not now.
const at = routine.archivedAt
? Temporal.Instant.from(routine.archivedAt)