From dc8515a9dabb0e1b6ba8f3d08123a0b296416e2a Mon Sep 17 00:00:00 2001 From: nilspenzel <116674699+nilspenzel@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:00:50 +0200 Subject: [PATCH] Read vehicles from database and display license plates in availability view (#9) --- ui/src/lib/api.ts | 7 ++++++- ui/src/lib/types.ts | 14 +++++++++++++ ui/src/routes/+page.svelte | 31 ++++++++++------------------ ui/src/routes/api/vehicle/+server.ts | 8 +++++++ 4 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 ui/src/routes/api/vehicle/+server.ts diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.ts index edfa8c4a..832148a6 100644 --- a/ui/src/lib/api.ts +++ b/ui/src/lib/api.ts @@ -1,6 +1,11 @@ -import type { Company } from './types'; +import type { Company, Vehicle } from './types'; export const getCompany = async (id: number): Promise => { const response = await fetch(`/api/company?id=${id}`); return await response.json(); }; + +export const getVehicles = async (company_id: number): Promise => { + const response = await fetch(`/api/vehicle?company=${company_id}`); + return await response.json(); +}; diff --git a/ui/src/lib/types.ts b/ui/src/lib/types.ts index d278702a..45ec479e 100644 --- a/ui/src/lib/types.ts +++ b/ui/src/lib/types.ts @@ -3,6 +3,7 @@ import { type Generated, type Insertable, type Selectable, type Updateable } fro export interface Database { zone: ZoneTable; company: CompanyTable; + vehicle: VehicleTable; } export interface ZoneTable { @@ -27,3 +28,16 @@ export interface CompanyTable { export type Company = Selectable; export type NewCompany = Insertable; export type CompanyUpdate = Updateable; + +export interface VehicleTable { + id: Generated; + license_plate: string; + company: number; + seats: number; + wheelchair_capacity: number; + storage_space: number; +} + +export type Vehicle = Selectable; +export type NewVehicle = Insertable; +export type VehicleUpdate = Updateable; diff --git a/ui/src/routes/+page.svelte b/ui/src/routes/+page.svelte index 55e8e6ca..ae12de8e 100644 --- a/ui/src/routes/+page.svelte +++ b/ui/src/routes/+page.svelte @@ -2,6 +2,8 @@ import { getCompany } from '$lib/api'; import type { Company } from '$lib/types'; + import { getVehicles } from '$lib/api'; + import { DateFormatter, today, getLocalTimeZone } from '@internationalized/date'; import CalendarIcon from 'lucide-svelte/icons/calendar'; @@ -34,12 +36,13 @@ vehicle_id!: number; } - let vehicles = $state>( - new Map([ - [ - 0, + let vehicles = $state>(new Map()); + onMount(async () => { + vehicles = new Map( + (await getVehicles(1)).map((v) => [ + v.id, { - license_plate: 'AB-XY-123', + license_plate: v.license_plate, availability: [ { from: new Date('2024-05-24T05:30:00'), @@ -51,21 +54,9 @@ } ] } - ], - [ - 1, - { - license_plate: 'AB-XY-321', - availability: [ - { - from: new Date('2024-05-24T09:30:00'), - to: new Date('2024-05-24T12:45:00') - } - ] - } - ] - ]) - ); + ]) + ); + }); let tours = $state>([ { diff --git a/ui/src/routes/api/vehicle/+server.ts b/ui/src/routes/api/vehicle/+server.ts new file mode 100644 index 00000000..bbdf8764 --- /dev/null +++ b/ui/src/routes/api/vehicle/+server.ts @@ -0,0 +1,8 @@ +import { json } from '@sveltejs/kit'; +import { db } from '$lib/database'; + +export async function GET({ url }) { + const id = Number(url.searchParams.get('company')!); + const vehicles = await db.selectFrom('vehicle').where('company', '=', id).selectAll().execute(); + return json(vehicles); +}