1
1
import type { PageServerLoad } from "./$types" ;
2
2
import type { Station } from "$models/station" ;
3
3
import { error } from "@sveltejs/kit" ;
4
+ import type { Journey } from "$models/connection" ;
5
+ import { DateTime } from "luxon" ;
4
6
5
- export const load : PageServerLoad = async ( { fetch, params } ) : Promise < { station : Station } > => {
6
- const response = await fetch ( `http://navigator-backend:8000/api/v1/stations/${ params . evaNumber } ` , {
7
+ export const load : PageServerLoad = async ( { params, url } ) : Promise < { station : Station , journeys : Journey [ ] } > => {
8
+ const [ station , journeys ] = await Promise . all ( [
9
+ loadStation ( params . evaNumber ) ,
10
+ loadJourneys ( params . evaNumber , params . type , url . searchParams . get ( "startDate" ) ?? DateTime . now ( ) . set ( { second : 0 , millisecond : 0 } ) . toISO ( ) )
11
+ ] ) ;
12
+ return { station, journeys } ;
13
+ } ;
14
+
15
+ const loadStation = async ( evaNumber : string ) : Promise < Station > => {
16
+ const response = await fetch ( `http://navigator-backend:8000/api/v1/stations/${ evaNumber } ` , {
7
17
method : "GET"
8
18
} ) ;
9
-
10
19
if ( ! response . ok ) {
11
- throw error ( 404 , `No station found for ${ params . evaNumber } ` ) ;
20
+ throw error ( 404 , `No station found for ${ evaNumber } ` ) ;
12
21
}
13
22
14
23
const station = ( await response . json ( ) ) as Station ;
15
-
16
24
if ( station . evaNumber === undefined ) {
17
- throw error ( 404 , `No station found for ${ params . evaNumber } ` ) ;
25
+ throw error ( 404 , `No station found for ${ evaNumber } ` ) ;
18
26
}
19
-
20
- return { station : station } ;
27
+ return station ;
21
28
} ;
29
+
30
+ const loadJourneys = async ( evaNumber : string , type : string , startDate : string ) : Promise < Journey [ ] > => {
31
+ const queryString = new URLSearchParams ( {
32
+ evaNumber : evaNumber ,
33
+ type : type ,
34
+ when : startDate
35
+ } ) . toString ( ) ;
36
+
37
+ const response = await fetch ( `http://navigator-backend:8000/api/v1/timetable/combined?${ queryString } ` , {
38
+ method : "GET"
39
+ } ) ;
40
+ if ( ! response . ok ) {
41
+ throw error ( 404 , `No journeys found for ${ evaNumber } ` ) ;
42
+ }
43
+
44
+ const jsonData = await response . json ( ) ;
45
+ if ( ! Array . isArray ( jsonData ) ) {
46
+ throw error ( 500 , `No journeys found for ${ evaNumber } ` ) ;
47
+ }
48
+
49
+ return jsonData as Journey [ ] ;
50
+ }
0 commit comments