Unverified Commit 2d66be08 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Add JSON output to train details (/z/:train)

parent 0a994156
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -281,7 +281,8 @@ sub startup {
	$r->get('/map/:tripid/:lineno')->to('map#route');
	$r->get( '/z/:train/*station' => 'train_at_station' )
	  ->to('stationboard#station_train_details');
	$r->get( '/z/:train' => 'train' )->to('stationboard#train_details');
	$r->get( '/z/:train' => [ format => [ 'html', 'json' ] ] )
	  ->to( 'stationboard#train_details', format => undef )->name('train');

	$self->defaults( layout => 'app' );

+37 −19
Original line number Diff line number Diff line
@@ -1303,8 +1303,16 @@ sub train_details {
				$res->{details} = [@him_details];
			}

			$self->render(
				$self->param('ajax') ? '_train_details' : 'train_details',
			$self->respond_to(
				json => {
					json => {
						journey => $journey,
					},
				},
				any => {
					template => $self->param('ajax')
					? '_train_details'
					: 'train_details',
					description => sprintf(
						'%s %s%s%s nach %s',
						$res->{train_type},
@@ -1316,21 +1324,31 @@ sub train_details {
					departure => $res,
					linetype  => $linetype,
					dt_now    => DateTime->now( time_zone => 'Europe/Berlin' ),
				},
			);
		}
	)->catch(
		sub {
			my ($e) = @_;
			if ($e) {
				$self->render(
					'exception',
				$self->respond_to(
					json => {
						json => {
							error => $e,
						},
						status => 500,
					},
					any => {
						template  => 'exception',
						message   => $e,
						exception => undef,
					snapshot  => {}
						snapshot  => {},
						status    => 500,
					},
				);
			}
			else {
				$self->render('not_found');
				$self->render( 'not_found', status => 404 );
			}
		}
	)->wait;