Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.3 kB
2
Indexable
Never
import { db } from '$db';
import { listingsTable, cpuModelsTable, suppliersTable } from '$db/schema';
import { eq, lte } from 'drizzle-orm';
import type { PageServerLoad } from './$types';
import { error } from '@sveltejs/kit';

export const load: PageServerLoad = async ({ url }) => {
	try {
		const budget = Number(url.searchParams.get('budget')) || 800;

		const listings = await db
			.select({
				name: cpuModelsTable.name,
				price: listingsTable.price,
				shippingCost: listingsTable.shippingCost,
				supplier: suppliersTable.name,
				url: listingsTable.url
			})
			.from(listingsTable)
			.innerJoin(cpuModelsTable, eq(listingsTable.cpuModelId, cpuModelsTable.id))
			.innerJoin(suppliersTable, eq(listingsTable.supplierId, suppliersTable.id))
			.where(lte(listingsTable.price, budget * 100)) // Convert budget to cents
			.orderBy(listingsTable.price);

		return {
			listings: listings.map((listing) => ({
				...listing,
				price: listing.price / 100, // Convert price back to euros
				shippingCost: listing.shippingCost / 100 // Convert shipping cost back to euros
			}))
		};
	} catch (err) {
		console.error('Error in load function:', err);
		throw error(500, { message: 'Internal server error', error: err });
	}
};
Leave a Comment