diff options
Diffstat (limited to 'lib/Travelynx/Controller')
-rwxr-xr-x | lib/Travelynx/Controller/Api.pm | 20 | ||||
-rw-r--r-- | lib/Travelynx/Controller/Traewelling.pm | 104 | ||||
-rwxr-xr-x | lib/Travelynx/Controller/Traveling.pm | 30 |
3 files changed, 139 insertions, 15 deletions
diff --git a/lib/Travelynx/Controller/Api.pm b/lib/Travelynx/Controller/Api.pm index c7e58a9..8d2d2a7 100755 --- a/lib/Travelynx/Controller/Api.pm +++ b/lib/Travelynx/Controller/Api.pm @@ -258,14 +258,21 @@ sub travel_v1 { $train_id = $train->train_id; } - my ( $train, $error ) - = $self->checkin( $from_station, $train_id, $uid ); + my ( $train, $error ) = $self->checkin( + station => $from_station, + train_id => $train_id, + uid => $uid + ); if ( $payload->{comment} and not $error ) { $self->update_in_transit_comment( sanitize( q{}, $payload->{comment} ), $uid ); } if ( $to_station and not $error ) { - ( $train, $error ) = $self->checkout( $to_station, 0, $uid ); + ( $train, $error ) = $self->checkout( + station => $to_station, + force => 0, + uid => $uid + ); } if ($error) { $self->render( @@ -307,8 +314,11 @@ sub travel_v1 { sanitize( q{}, $payload->{comment} ), $uid ); } - my ( $train, $error ) - = $self->checkout( $to_station, $payload->{force} ? 1 : 0, $uid ); + my ( $train, $error ) = $self->checkout( + station => $to_station, + force => $payload->{force} ? 1 : 0, + uid => $uid + ); if ($error) { $self->render( json => { diff --git a/lib/Travelynx/Controller/Traewelling.pm b/lib/Travelynx/Controller/Traewelling.pm new file mode 100644 index 0000000..78c501f --- /dev/null +++ b/lib/Travelynx/Controller/Traewelling.pm @@ -0,0 +1,104 @@ +package Travelynx::Controller::Traewelling; +use Mojo::Base 'Mojolicious::Controller'; +use Mojo::Promise; + +sub settings { + my ($self) = @_; + + my $uid = $self->current_user->{id}; + + if ( $self->param('action') + and $self->validation->csrf_protect->has_error('csrf_token') ) + { + $self->render( + 'traewelling', + invalid => 'csrf', + ); + return; + } + + if ( $self->param('action') and $self->param('action') eq 'login' ) { + my $email = $self->param('email'); + my $password = $self->param('password'); + $self->render_later; + $self->traewelling_api->login_p( + uid => $uid, + email => $email, + password => $password + )->then( + sub { + my $traewelling = $self->traewelling->get($uid); + $self->param( sync_source => 'none' ); + $self->render( + 'traewelling', + traewelling => $traewelling, + new_traewelling => 1, + ); + } + )->catch( + sub { + my ($err) = @_; + $self->render( + 'traewelling', + traewelling => {}, + new_traewelling => 1, + login_error => $err, + ); + } + )->wait; + return; + } + elsif ( $self->param('action') and $self->param('action') eq 'logout' ) { + $self->render_later; + my $traewelling = $self->traewelling->get($uid); + $self->traewelling_api->logout_p( + uid => $uid, + token => $traewelling->{token} + )->then( + sub { + $self->flash( success => 'traewelling' ); + $self->redirect_to('account'); + } + )->catch( + sub { + my ($err) = @_; + $self->render( + 'traewelling', + traewelling => {}, + new_traewelling => 1, + logout_error => $err, + ); + } + )->wait; + return; + } + elsif ( $self->param('action') and $self->param('action') eq 'config' ) { + $self->traewelling->set_sync( + uid => $uid, + push_sync => $self->param('sync_source') eq 'travelynx' ? 1 : 0, + pull_sync => $self->param('sync_source') eq 'traewelling' ? 1 : 0 + ); + $self->flash( success => 'traewelling' ); + $self->redirect_to('account'); + return; + } + + my $traewelling = $self->traewelling->get($uid); + + if ( $traewelling->{push_sync} ) { + $self->param( sync_source => 'travelynx' ); + } + elsif ( $traewelling->{pull_sync} ) { + $self->param( sync_source => 'traewelling' ); + } + else { + $self->param( sync_source => 'none' ); + } + + $self->render( + 'traewelling', + traewelling => $traewelling, + ); +} + +1; diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index f5e3255..4df1558 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -424,8 +424,10 @@ sub log_action { if ( $params->{action} eq 'checkin' ) { - my ( $train, $error ) - = $self->checkin( $params->{station}, $params->{train} ); + my ( $train, $error ) = $self->checkin( + station => $params->{station}, + train_id => $params->{train} + ); my $destination = $params->{dest}; if ($error) { @@ -447,8 +449,10 @@ sub log_action { else { # Silently ignore errors -- if they are permanent, the user will see # them when selecting the destination manually. - my ( $still_checked_in, undef ) - = $self->checkout( $destination, 0 ); + my ( $still_checked_in, undef ) = $self->checkout( + station => $destination, + force => 0 + ); my $station_link = '/s/' . $destination; $self->render( json => { @@ -459,8 +463,10 @@ sub log_action { } } elsif ( $params->{action} eq 'checkout' ) { - my ( $still_checked_in, $error ) - = $self->checkout( $params->{station}, $params->{force} ); + my ( $still_checked_in, $error ) = $self->checkout( + station => $params->{station}, + force => $params->{force} + ); my $station_link = '/s/' . $params->{station}; if ($error) { @@ -505,8 +511,10 @@ sub log_action { } } elsif ( $params->{action} eq 'cancelled_from' ) { - my ( undef, $error ) - = $self->checkin( $params->{station}, $params->{train} ); + my ( undef, $error ) = $self->checkin( + station => $params->{station}, + train_id => $params->{train} + ); if ($error) { $self->render( @@ -526,8 +534,10 @@ sub log_action { } } elsif ( $params->{action} eq 'cancelled_to' ) { - my ( undef, $error ) - = $self->checkout( $params->{station}, 1 ); + my ( undef, $error ) = $self->checkout( + station => $params->{station}, + force => 1 + ); if ($error) { $self->render( |