From 6be8885c13921b9b094532a5804d10d884aa66d3 Mon Sep 17 00:00:00 2001 From: Raffaele Lungarella Date: Mon, 20 Jan 2025 20:36:52 +0100 Subject: [PATCH] chore: better db integration --- src/lib/server/auth.ts | 4 ++-- src/lib/server/backend/reservation-service.ts | 10 ++++++++ src/lib/server/backend/services-service.ts | 11 +++++++++ src/lib/server/backend/user-service.ts | 23 +++++++++++++++++++ src/lib/server/db/init.ts | 5 ++-- .../newreservation/+page.server.ts | 15 +++++------- .../profile/reservations/datatable.svelte | 1 + src/routes/login/+page.server.ts | 6 ++--- src/routes/prices/+page.server.ts | 5 ++-- src/routes/signup/+page.server.ts | 12 ++++------ 10 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 src/lib/server/backend/reservation-service.ts create mode 100644 src/lib/server/backend/services-service.ts create mode 100644 src/lib/server/backend/user-service.ts diff --git a/src/lib/server/auth.ts b/src/lib/server/auth.ts index 5c49c0b..c92a5c2 100644 --- a/src/lib/server/auth.ts +++ b/src/lib/server/auth.ts @@ -4,6 +4,7 @@ import { sha256 } from '@oslojs/crypto/sha2'; import { encodeBase64url, encodeHexLowerCase } from '@oslojs/encoding'; import { db } from '$lib/server/db'; import * as table from '$lib/server/db/schema'; +import { insertSession } from './backend/user-service'; const DAY_IN_MS = 1000 * 60 * 60 * 24; @@ -22,7 +23,7 @@ export async function createSession(token: string, userID: string) { userID, expiresAt: new Date(Date.now() + DAY_IN_MS * 30) }; - await db.insert(table.session).values(session); + await insertSession(session); return session; } @@ -30,7 +31,6 @@ export async function validateSessionToken(token: string) { const sessionId = encodeHexLowerCase(sha256(new TextEncoder().encode(token))); const [result] = await db .select({ - // Adjust user table here to tweak returned data user: table.user, session: table.session }) diff --git a/src/lib/server/backend/reservation-service.ts b/src/lib/server/backend/reservation-service.ts new file mode 100644 index 0000000..6d053f1 --- /dev/null +++ b/src/lib/server/backend/reservation-service.ts @@ -0,0 +1,10 @@ +import { db } from '$lib/server/db'; +import { eq } from 'drizzle-orm'; +import * as table from '$lib/server/db/schema'; + +export async function insertReservation(res: table.Reservation) { + await db + .insert(table.reservation) + .values({ date: res.date, id: res.id, userID: res.userID, serviceID: res.serviceID }); +} + diff --git a/src/lib/server/backend/services-service.ts b/src/lib/server/backend/services-service.ts new file mode 100644 index 0000000..3a0cd3f --- /dev/null +++ b/src/lib/server/backend/services-service.ts @@ -0,0 +1,11 @@ +import { db } from '$lib/server/db'; +import { eq } from 'drizzle-orm'; +import * as table from '$lib/server/db/schema'; + +export async function getAllServices() { + return await db.select().from(table.service); +} + +export async function insertService(service: table.Service) { + await db.insert(table.service).values(service).onConflictDoNothing(); +} diff --git a/src/lib/server/backend/user-service.ts b/src/lib/server/backend/user-service.ts new file mode 100644 index 0000000..abe0b33 --- /dev/null +++ b/src/lib/server/backend/user-service.ts @@ -0,0 +1,23 @@ +import { db } from '$lib/server/db'; +import { eq } from 'drizzle-orm'; +import * as table from '$lib/server/db/schema'; + +export async function insertUser(user: table.User) { + await db.insert(table.user).values({ + id: user.id, + email: user.email, + passwordHash: user.passwordHash, + lastName: user.lastName, + firstName: user.firstName + }); +} + +export async function getUser(email: string) { + return await db.select().from(table.user).where(eq(table.user.email, email)); +} + +export async function insertSession(session: table.Session) { + return await db.insert(table.session).values(session); +} + + // const results = await db.select().from(table.user).where(eq(table.user.email, form.data.email)); diff --git a/src/lib/server/db/init.ts b/src/lib/server/db/init.ts index 4073ec0..3a58d7b 100644 --- a/src/lib/server/db/init.ts +++ b/src/lib/server/db/init.ts @@ -1,12 +1,13 @@ import { db } from '$lib/server/db'; import type { Service } from './schema'; import * as table from '$lib/server/db/schema'; +import { getAllServices, insertService } from '../backend/services-service'; export const init = async () => { - const result = await db.select().from(table.service); + const result = await getAllServices(); if (result.length === 0) { services.forEach(async (ser) => { - await db.insert(table.service).values(ser).onConflictDoNothing(); + await insertService(ser); }); } diff --git a/src/routes/(protected)/newreservation/+page.server.ts b/src/routes/(protected)/newreservation/+page.server.ts index 25c9eab..6485b00 100644 --- a/src/routes/(protected)/newreservation/+page.server.ts +++ b/src/routes/(protected)/newreservation/+page.server.ts @@ -2,10 +2,9 @@ import type { Actions, PageServerLoad } from './$types.js'; import { superValidate } from 'sveltekit-superforms'; import { reservation } from '$lib/schemas/reservation.js'; import { zod } from 'sveltekit-superforms/adapters'; -import type { Service } from '$lib/server/db/schema.js'; import { fail } from '@sveltejs/kit'; -import { db } from '$lib/server/db/index.js'; -import * as table from '$lib/server/db/schema'; +import { insertReservation } from '$lib/server/backend/reservation-service.js'; +import { getAllServices } from '$lib/server/backend/services-service.js'; export const actions: Actions = { default: async ({ request, locals }) => { @@ -37,14 +36,12 @@ export const actions: Actions = { if (!locals.user) { return fail(404, { message: 'Riprova' }); } else { - const booking: table.Reservation = { - date: date, + await insertReservation({ + date, id: crypto.randomUUID(), userID: locals.user.id, serviceID: service - }; - - await db.insert(table.reservation).values(booking); + }); return { bookingCreated: true }; @@ -53,7 +50,7 @@ export const actions: Actions = { }; export const load: PageServerLoad = async () => { - const services = await db.select().from(table.service); + const services = await getAllServices(); return { services, form: await superValidate(zod(reservation)) diff --git a/src/routes/(protected)/profile/reservations/datatable.svelte b/src/routes/(protected)/profile/reservations/datatable.svelte index def1a24..d640fd9 100644 --- a/src/routes/(protected)/profile/reservations/datatable.svelte +++ b/src/routes/(protected)/profile/reservations/datatable.svelte @@ -8,6 +8,7 @@ data: TData[]; }; + // @ts-expect-error let { data, columns }: DataTableProps = $props(); const table = createSvelteTable({ diff --git a/src/routes/login/+page.server.ts b/src/routes/login/+page.server.ts index 30c6340..6073a4e 100644 --- a/src/routes/login/+page.server.ts +++ b/src/routes/login/+page.server.ts @@ -1,13 +1,11 @@ import { verify } from '@node-rs/argon2'; import { fail, redirect } from '@sveltejs/kit'; -import { eq } from 'drizzle-orm'; import * as auth from '$lib/server/auth'; -import { db } from '$lib/server/db'; -import * as table from '$lib/server/db/schema'; import type { Actions, PageServerLoad } from './$types'; import { superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import { login } from '$lib/schemas/login'; +import { getUser } from '$lib/server/backend/user-service'; export const load: PageServerLoad = async (event) => { if (event.locals.user) { @@ -29,7 +27,7 @@ export const actions: Actions = { }); } - const results = await db.select().from(table.user).where(eq(table.user.email, form.data.email)); + const results = await getUser(form.data.email); const existingUser = results.at(0); if (!existingUser) { diff --git a/src/routes/prices/+page.server.ts b/src/routes/prices/+page.server.ts index c03e286..e3357f4 100644 --- a/src/routes/prices/+page.server.ts +++ b/src/routes/prices/+page.server.ts @@ -1,9 +1,8 @@ import type { PageServerLoad } from './$types.js'; -import { db } from '$lib/server/db/index.js'; -import * as table from '$lib/server/db/schema.js'; +import { getAllServices } from '$lib/server/backend/services-service.js'; export const load: PageServerLoad = async () => { return { - services: await db.select().from(table.service) + services: await getAllServices() }; }; diff --git a/src/routes/signup/+page.server.ts b/src/routes/signup/+page.server.ts index 6bc8a81..2d7f529 100644 --- a/src/routes/signup/+page.server.ts +++ b/src/routes/signup/+page.server.ts @@ -1,14 +1,12 @@ import { fail, redirect } from '@sveltejs/kit'; import * as auth from '$lib/server/auth'; -import { db } from '$lib/server/db'; -import { eq } from 'drizzle-orm'; -import * as table from '$lib/server/db/schema'; import { hash } from '@node-rs/argon2'; import { encodeBase32LowerCase } from '@oslojs/encoding'; import type { Actions, PageServerLoad } from './$types'; import { superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import { signup } from '$lib/schemas/signup'; +import { getUser, insertUser } from '$lib/server/backend/user-service'; export const load: PageServerLoad = async (event) => { if (event.locals.user) { @@ -30,7 +28,7 @@ export const actions: Actions = { }); } - const results = await db.select().from(table.user).where(eq(table.user.email, form.data.email)); + const results = await getUser(form.data.email); const existingUser = results.at(0); if (existingUser) { @@ -42,7 +40,7 @@ export const actions: Actions = { const { email, password, firstName, lastName } = form.data; - const userId = generateUserId(); + const userID = generateUserId(); const passwordHash = await hash(password, { // recommended minimum parameters memoryCost: 19456, @@ -52,10 +50,10 @@ export const actions: Actions = { }); try { - await db.insert(table.user).values({ id: userId, email, passwordHash, lastName, firstName }); + await insertUser({ id: userID, email, passwordHash, lastName, firstName, phoneNumber: null }); const sessionToken = auth.generateSessionToken(); - const session = await auth.createSession(sessionToken, userId); + const session = await auth.createSession(sessionToken, userID); auth.setSessionTokenCookie(event, sessionToken, session.expiresAt); } catch (e) { console.log(e);