diff options
Diffstat (limited to 'lib/Travelynx/Controller/Traveling.pm')
-rwxr-xr-x | lib/Travelynx/Controller/Traveling.pm | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm new file mode 100755 index 0000000..8d71d95 --- /dev/null +++ b/lib/Travelynx/Controller/Traveling.pm @@ -0,0 +1,241 @@ +package Travelynx::Controller::Traveling; +use Mojo::Base 'Mojolicious::Controller'; + +use Travel::Status::DE::IRIS::Stations; + +my %action_type = ( + checkin => 1, + checkout => 2, + undo => 3, + cancelled_from => 4, + cancelled_to => 5, +); +my @action_types = (qw(checkin checkout undo cancelled_from cancelled_to)); + +sub homepage { + my ($self) = @_; + if ( $self->is_user_authenticated ) { + $self->render( 'landingpage', with_geolocation => 1 ); + } + else { + $self->render( 'landingpage', intro => 1 ); + } +} + +sub geolocation { + my ($self) = @_; + + my $lon = $self->param('lon'); + my $lat = $self->param('lat'); + + if ( not $lon or not $lat ) { + $self->render( json => { error => 'Invalid lon/lat received' } ); + } + else { + my @candidates = map { + { + ds100 => $_->[0][0], + name => $_->[0][1], + eva => $_->[0][2], + lon => $_->[0][3], + lat => $_->[0][4], + distance => $_->[1], + } + } Travel::Status::DE::IRIS::Stations::get_station_by_location( $lon, + $lat, 5 ); + $self->render( + json => { + candidates => [@candidates], + } + ); + } +} + +sub log_action { + my ($self) = @_; + my $params = $self->req->json; + + if ( not exists $params->{action} ) { + $params = $self->req->params->to_hash; + } + + if ( not $self->is_user_authenticated ) { + + # We deliberately do not set the HTTP status for these replies, as it + # confuses jquery. + $self->render( + json => { + success => 0, + error => 'Session error, please login again', + }, + ); + return; + } + + if ( not $params->{action} ) { + $self->render( + json => { + success => 0, + error => 'Missing action value', + }, + ); + return; + } + + my $station = $params->{station}; + + if ( $params->{action} eq 'checkin' ) { + + my ( $train, $error ) + = $self->checkin( $params->{station}, $params->{train} ); + + if ($error) { + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + else { + $self->render( + json => { + success => 1, + }, + ); + } + } + elsif ( $params->{action} eq 'checkout' ) { + my $error = $self->checkout( $params->{station}, $params->{force} ); + + if ($error) { + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + else { + $self->render( + json => { + success => 1, + }, + ); + } + } + elsif ( $params->{action} eq 'undo' ) { + my $error = $self->undo; + if ($error) { + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + else { + $self->render( + json => { + success => 1, + }, + ); + } + } + elsif ( $params->{action} eq 'cancelled_from' ) { + my ( undef, $error ) + = $self->checkin( $params->{station}, $params->{train}, + $action_type{cancelled_from} ); + + if ($error) { + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + else { + $self->render( + json => { + success => 1, + }, + ); + } + } + elsif ( $params->{action} eq 'cancelled_to' ) { + my $error = $self->checkout( $params->{station}, 1, + $action_type{cancelled_to} ); + + if ($error) { + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + else { + $self->render( + json => { + success => 1, + }, + ); + } + } + else { + $self->render( + json => { + success => 0, + error => 'invalid action value', + }, + ); + } +} + +sub station { + my ($self) = @_; + my $station = $self->stash('station'); + my $train = $self->param('train'); + + my $status = $self->get_departures($station); + + if ( $status->{errstr} ) { + $self->render( + 'landingpage', + with_geolocation => 1, + error => $status->{errstr} + ); + } + else { + # You can't check into a train which terminates here + my @results = grep { $_->departure } @{ $status->{results} }; + + @results = map { $_->[0] } + sort { $b->[1] <=> $a->[1] } + map { [ $_, $_->departure->epoch // $_->sched_departure->epoch ] } + @results; + + if ($train) { + @results + = grep { $_->type . ' ' . $_->train_no eq $train } @results; + } + + $self->render( + 'departures', + ds100 => $status->{station_ds100}, + results => \@results, + station => $status->{station_name}, + title => "travelynx: $status->{station_name}", + ); + } +} + +sub redirect_to_station { + my ($self) = @_; + my $station = $self->param('station'); + + $self->redirect_to("/s/${station}"); +} + +1; |