aboutsummaryrefslogtreecommitdiff
path: root/src/lib/server/services/references.ts
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-07-28 15:07:57 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-07-28 15:07:57 +0200
commit04fabf115ccea31ac504b06cc51d78423b697f60 (patch)
tree26c21f8822ccd24fce3a80b7361382c8a87d0c96 /src/lib/server/services/references.ts
parent45cc0afb734a422ee7765dc5c60dccbcc6721367 (diff)
refactor: services layer, valibot validation, shared CSS utilities, aria labels, and drizzle migrate
- Extract groceries/categories/references services with db injection - Add valibot for form validation and safe numeric id parsing - Fix category delete FK behavior with onDelete: set null + migration - Replace custom migrate.js with drizzle-orm/libsql/migrator - Add shared CSS utility classes in app.css (forms, lists, buttons) - Add aria-pressed and aria-label on interactive elements - Remove redundant app.css imports and unused /stores import - Add integration tests for all services using drizzle migrate helper - Remove smoke test; add db:migrate and db:studio scripts
Diffstat (limited to 'src/lib/server/services/references.ts')
-rw-r--r--src/lib/server/services/references.ts38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/lib/server/services/references.ts b/src/lib/server/services/references.ts
new file mode 100644
index 0000000..99ba487
--- /dev/null
+++ b/src/lib/server/services/references.ts
@@ -0,0 +1,38 @@
+import { db as defaultDb } from '$lib/server/db';
+import { references } from '$lib/server/db/schema';
+import { eq, desc } from 'drizzle-orm';
+import type { AppDatabase } from './groceries';
+import type { Reference } from './types';
+
+export async function loadForGrocery(
+ groceryId: number,
+ db = defaultDb,
+): Promise<Reference[]> {
+ return db
+ .select()
+ .from(references)
+ .where(eq(references.groceryId, groceryId))
+ .orderBy(desc(references.qualityScore));
+}
+
+export async function add(
+ groceryId: number,
+ reference: string,
+ price: number | null,
+ unit: string | null,
+ qualityScore: number | null,
+ db = defaultDb,
+): Promise<Reference> {
+ const [inserted] = await db
+ .insert(references)
+ .values({ groceryId, reference, price, unit, qualityScore })
+ .returning();
+ return inserted;
+}
+
+export async function deleteReference(
+ id: number,
+ db = defaultDb,
+): Promise<void> {
+ await db.delete(references).where(eq(references.id, id));
+}