Skip to content

Commit

Permalink
Read vehicles from database and display license plates in availabilit…
Browse files Browse the repository at this point in the history
…y view (#9)
  • Loading branch information
nilspenzel authored Jun 11, 2024
1 parent c6f5635 commit dc8515a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
7 changes: 6 additions & 1 deletion ui/src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { Company } from './types';
import type { Company, Vehicle } from './types';

export const getCompany = async (id: number): Promise<Company> => {
const response = await fetch(`/api/company?id=${id}`);
return await response.json();
};

export const getVehicles = async (company_id: number): Promise<Vehicle[]> => {
const response = await fetch(`/api/vehicle?company=${company_id}`);
return await response.json();
};
14 changes: 14 additions & 0 deletions ui/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,3 +28,16 @@ export interface CompanyTable {
export type Company = Selectable<CompanyTable>;
export type NewCompany = Insertable<CompanyTable>;
export type CompanyUpdate = Updateable<CompanyTable>;

export interface VehicleTable {
id: Generated<number>;
license_plate: string;
company: number;
seats: number;
wheelchair_capacity: number;
storage_space: number;
}

export type Vehicle = Selectable<VehicleTable>;
export type NewVehicle = Insertable<VehicleTable>;
export type VehicleUpdate = Updateable<VehicleTable>;
31 changes: 11 additions & 20 deletions ui/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -34,12 +36,13 @@
vehicle_id!: number;
}
let vehicles = $state<Map<number, Vehicle>>(
new Map<number, Vehicle>([
[
0,
let vehicles = $state<Map<number, Vehicle>>(new Map<number, Vehicle>());
onMount(async () => {
vehicles = new Map<number, Vehicle>(
(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'),
Expand All @@ -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<Array<Tour>>([
{
Expand Down
8 changes: 8 additions & 0 deletions ui/src/routes/api/vehicle/+server.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit dc8515a

Please sign in to comment.