diff options
author | Birte Kristina Friesel <derf@finalrewind.org> | 2023-07-09 14:46:28 +0200 |
---|---|---|
committer | Birte Kristina Friesel <derf@finalrewind.org> | 2023-07-09 14:46:28 +0200 |
commit | 1074f248cc415a397f16d0328a8f45fa6a867513 (patch) | |
tree | 0dfcf10ecbcbb98b7d5159056acd83e6a5a3b4d9 /lib/Travelynx | |
parent | f7e3bb490d33640cd7b8e92ffa3ac7c71d42d191 (diff) |
use promises for checkin and traewelling_to_travelynx
Diffstat (limited to 'lib/Travelynx')
-rw-r--r-- | lib/Travelynx/Command/traewelling.pm | 2 | ||||
-rwxr-xr-x | lib/Travelynx/Controller/Api.pm | 74 | ||||
-rwxr-xr-x | lib/Travelynx/Controller/Traveling.pm | 114 |
3 files changed, 104 insertions, 86 deletions
diff --git a/lib/Travelynx/Command/traewelling.pm b/lib/Travelynx/Command/traewelling.pm index 1974352..4c47e84 100644 --- a/lib/Travelynx/Command/traewelling.pm +++ b/lib/Travelynx/Command/traewelling.pm @@ -50,7 +50,7 @@ sub pull_sync { sub { my ($traewelling) = @_; $pull_result{ $traewelling->{http} } += 1; - $self->app->traewelling_to_travelynx( + return $self->app->traewelling_to_travelynx_p( traewelling => $traewelling, user_data => $account_data ); diff --git a/lib/Travelynx/Controller/Api.pm b/lib/Travelynx/Controller/Api.pm index ac436ba..fbccbaa 100755 --- a/lib/Travelynx/Controller/Api.pm +++ b/lib/Travelynx/Controller/Api.pm @@ -261,43 +261,53 @@ sub travel_v1 { $train_id = $train->train_id; } - my ( $train, $error ) = $self->checkin( + $self->render_later; + + $self->checkin_p( station => $from_station, train_id => $train_id, uid => $uid - ); - if ( $payload->{comment} and not $error ) { - $self->in_transit->update_user_data( - uid => $uid, - user_data => { comment => sanitize( q{}, $payload->{comment} ) } - ); - } - if ( $to_station and not $error ) { - ( $train, $error ) = $self->checkout( - station => $to_station, - force => 0, - uid => $uid - ); - } - if ($error) { - $self->render( - json => { - success => \0, - deprecated => \0, - error => 'Checkin/Checkout error: ' . $error, - status => $self->get_user_status_json_v1( uid => $uid ) + )->then( + sub { + my ($train) = @_; + if ( $payload->{comment} ) { + $self->in_transit->update_user_data( + uid => $uid, + user_data => + { comment => sanitize( q{}, $payload->{comment} ) } + ); } - ); - } - else { - $self->render( - json => { - success => \1, - deprecated => \0, - status => $self->get_user_status_json_v1( uid => $uid ) + if ($to_station) { + my ( $train2, $error ) = $self->checkout( + station => $to_station, + force => 0, + uid => $uid + ); + if ($error) { + return Mojo::Promise->reject($error); + } } - ); - } + $self->render( + json => { + success => \1, + deprecated => \0, + status => $self->get_user_status_json_v1( uid => $uid ) + } + ); + } + )->catch( + sub { + my ($error) = @_; + $self->render( + json => { + success => \0, + deprecated => \0, + error => 'Checkin/Checkout error: ' . $error, + status => $self->get_user_status_json_v1( uid => $uid ) + } + ); + } + )->wait; } elsif ( $payload->{action} eq 'checkout' ) { my $to_station = sanitize( q{}, $payload->{toStation} ); diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index 7c2fe33..96acdd2 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -615,43 +615,48 @@ sub travel_action { if ( $params->{action} eq 'checkin' ) { - my ( $train, $error ) = $self->checkin( + $self->render_later; + $self->checkin_p( station => $params->{station}, train_id => $params->{train} - ); - my $destination = $params->{dest}; + )->then( + sub { + my $destination = $params->{dest}; + if ( not $destination ) { + $self->render( + json => { + success => 1, + redirect_to => '/', + }, + ); + return; + } - if ($error) { - $self->render( - json => { - success => 0, - error => $error, - }, - ); - } - elsif ( not $destination ) { - $self->render( - json => { - success => 1, - redirect_to => '/', - }, - ); - } - 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( - station => $destination, - force => 0 - ); - my $station_link = '/s/' . $destination; - $self->render( - json => { - success => 1, - redirect_to => $still_checked_in ? '/' : $station_link, - }, - ); - } + # Silently ignore errors -- if they are permanent, the user will see + # them when selecting the destination manually. + my ( $still_checked_in, undef ) = $self->checkout( + station => $destination, + force => 0 + ); + my $station_link = '/s/' . $destination; + $self->render( + json => { + success => 1, + redirect_to => $still_checked_in ? '/' : $station_link, + }, + ); + } + )->catch( + sub { + my ($error) = @_; + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + )->wait; } elsif ( $params->{action} eq 'checkout' ) { my ( $still_checked_in, $error ) = $self->checkout( @@ -702,27 +707,30 @@ sub travel_action { } } elsif ( $params->{action} eq 'cancelled_from' ) { - my ( undef, $error ) = $self->checkin( + $self->render_later; + $self->checkin_p( station => $params->{station}, train_id => $params->{train} - ); - - if ($error) { - $self->render( - json => { - success => 0, - error => $error, - }, - ); - } - else { - $self->render( - json => { - success => 1, - redirect_to => '/', - }, - ); - } + )->then( + sub { + $self->render( + json => { + success => 1, + redirect_to => '/', + }, + ); + } + )->catch( + sub { + my ($error) = @_; + $self->render( + json => { + success => 0, + error => $error, + }, + ); + } + )->wait; } elsif ( $params->{action} eq 'cancelled_to' ) { my ( undef, $error ) = $self->checkout( |