From 4efed80a2f8dc238b420eadf97d56c3eccaadb76 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Sun, 18 Oct 2026 22:22:55 +0200 Subject: refactor: inject the database into every service Groceries, projects, references and routines become classes holding the Drizzle handle, wired once in services/index.ts alongside categories. Tests build their own instance on an in-memory database. Drops the unused projects.loadAll and projects.rename. --- src/routes/grocery/[id]/+page.server.ts | 34 ++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'src/routes/grocery') diff --git a/src/routes/grocery/[id]/+page.server.ts b/src/routes/grocery/[id]/+page.server.ts index 0f386f3..dbfecff 100644 --- a/src/routes/grocery/[id]/+page.server.ts +++ b/src/routes/grocery/[id]/+page.server.ts @@ -2,14 +2,11 @@ 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 { categoriesService } from '$lib/server/services'; 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, @@ -39,7 +36,7 @@ export const load: PageServerLoad = async ({ params, url }) => { .where(eq(categories.id, item.categoryId)) : [null]; - const refs = await loadForGrocery(id); + const refs = await referencesService.loadForGrocery(id); const cats = await categoriesService.loadAll(); const from = url.searchParams.get('from') || '/'; @@ -67,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(); @@ -88,7 +96,7 @@ export const actions: Actions = { } try { - await updateGrocery(groceryId, name, categoryId); + await groceriesService.updateGrocery(groceryId, name, categoryId); } catch (error) { if (isUniqueViolation(error)) { return fail(400, { error: 'Un article porte déjà ce nom' }); @@ -100,7 +108,7 @@ export const actions: Actions = { 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); -- cgit v1.3