summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Controller
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2023-07-09 19:23:11 +0200
committerBirte Kristina Friesel <derf@finalrewind.org>2023-07-09 19:23:11 +0200
commit8a422d16f7c44732eb120ad93bf1584611488920 (patch)
tree9c849715710216d110af520d1c2f4497cafa9517 /lib/Travelynx/Controller
parent5b88a4bb3d1bbf64e4985deb034b7cd4a265d26a (diff)
Api: use get_departures_p
Diffstat (limited to 'lib/Travelynx/Controller')
-rwxr-xr-xlib/Travelynx/Controller/Api.pm65
1 files changed, 32 insertions, 33 deletions
diff --git a/lib/Travelynx/Controller/Api.pm b/lib/Travelynx/Controller/Api.pm
index fbccbaa..9685582 100755
--- a/lib/Travelynx/Controller/Api.pm
+++ b/lib/Travelynx/Controller/Api.pm
@@ -220,53 +220,52 @@ sub travel_v1 {
return;
}
+ my $train_p;
+
if ( exists $payload->{train}{id} ) {
- $train_id = sanitize( 0, $payload->{train}{id} );
+ $train_p
+ = Mojo::Promise->resolve( sanitize( 0, $payload->{train}{id} ) );
}
else {
my $train_type = sanitize( q{}, $payload->{train}{type} );
my $train_no = sanitize( q{}, $payload->{train}{no} );
- my $status = $self->iris->get_departures(
+
+ $train_p = $self->iris->get_departures_p(
station => $from_station,
lookbehind => 140,
lookahead => 40
- );
- if ( $status->{errstr} ) {
- $self->render(
- json => {
- success => \0,
- error =>
- 'Error requesting departures from fromStation: '
- . $status->{errstr},
- status => $self->get_user_status_json_v1( uid => $uid )
+ )->then(
+ sub {
+ my ($status) = @_;
+ if ( $status->{errstr} ) {
+ return Mojo::Promise->reject(
+ 'Error requesting departures from fromStation: '
+ . $status->{errstr} );
}
- );
- return;
- }
- my ($train) = List::Util::first {
- $_->type eq $train_type and $_->train_no eq $train_no
- }
- @{ $status->{results} };
- if ( not defined $train ) {
- $self->render(
- json => {
- success => \0,
- deprecated => \0,
- error => 'Train not found at fromStation',
- status => $self->get_user_status_json_v1( uid => $uid )
+ my ($train) = List::Util::first {
+ $_->type eq $train_type and $_->train_no eq $train_no
}
- );
- return;
- }
- $train_id = $train->train_id;
+ @{ $status->{results} };
+ if ( not defined $train ) {
+ return Mojo::Promise->reject(
+ 'Train not found at fromStation');
+ }
+ return Mojo::Promise->resolve( $train->train_id );
+ }
+ );
}
$self->render_later;
- $self->checkin_p(
- station => $from_station,
- train_id => $train_id,
- uid => $uid
+ $train_p->then(
+ sub {
+ my ($train_id) = @_;
+ return $self->checkin_p(
+ station => $from_station,
+ train_id => $train_id,
+ uid => $uid
+ );
+ }
)->then(
sub {
my ($train) = @_;