From e168d9cd39c38b8e5a1994c8bf11376d26e9ea77 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 23 Apr 2019 18:08:07 +0200 Subject: Use one row per journey instead of split checkin/checkout entries Whether a user is in transit or not is now determined by an entry in the in_transit table instead of a dangling checkin. All completed journeys are stored in the "journeys" table. This does most of the work needed for automatic checkout. However, note that the corresponding worker process is not implemented yet. --- lib/Travelynx/Controller/Account.pm | 47 ++++------------ lib/Travelynx/Controller/Api.pm | 16 +++--- lib/Travelynx/Controller/Static.pm | 3 +- lib/Travelynx/Controller/Traveling.pm | 101 +++++++++++++++++----------------- 4 files changed, 71 insertions(+), 96 deletions(-) (limited to 'lib/Travelynx/Controller') diff --git a/lib/Travelynx/Controller/Account.pm b/lib/Travelynx/Controller/Account.pm index 0037e16..e60f1d3 100644 --- a/lib/Travelynx/Controller/Account.pm +++ b/lib/Travelynx/Controller/Account.pm @@ -286,45 +286,18 @@ sub account { sub json_export { my ($self) = @_; - my $uid = $self->current_user->{id}; - my $query = $self->app->get_all_actions_query; - - $query->execute($uid); - - my @entries; - - while ( my @row = $query->fetchrow_array ) { - my ( - $action_id, $action, $raw_ts, $ds100, - $name, $train_type, $train_line, $train_no, - $train_id, $raw_sched_ts, $raw_real_ts, $raw_route, - $raw_messages - ) = @row; - - push( - @entries, - { - action => $self->app->action_types->[ $action - 1 ], - action_ts => $raw_ts, - station_ds100 => $ds100, - station_name => $name, - train_type => $train_type, - train_line => $train_line, - train_no => $train_no, - train_id => $train_id, - scheduled_ts => $raw_sched_ts, - realtime_ts => $raw_real_ts, - messages => $raw_messages - ? [ map { [ split(qr{:}) ] } split( qr{[|]}, $raw_messages ) ] - : undef, - route => $raw_route ? [ split( qr{[|]}, $raw_route ) ] - : undef, - } - ); - } + my $uid = $self->current_user->{id}; + + my $db = $self->pg->db; $self->render( - json => [@entries], + json => { + account => $db->select( 'users', '*', { id => $uid } )->hash, + journeys => [ + $db->select( 'journeys', '*', { user_id => $uid } ) + ->hashes->each + ], + } ); } diff --git a/lib/Travelynx/Controller/Api.pm b/lib/Travelynx/Controller/Api.pm index a9500f1..8e72374 100755 --- a/lib/Travelynx/Controller/Api.pm +++ b/lib/Travelynx/Controller/Api.pm @@ -48,10 +48,10 @@ sub get_v0 { my $station_lon = undef; my $station_lat = undef; - if ( $status->{station_ds100} ) { + if ( $status->{arr_ds100} // $status->{dep_ds100} ) { @station_descriptions = Travel::Status::DE::IRIS::Stations::get_station( - $status->{station_ds100} ); + $status->{arr_ds100} // $status->{dep_ds100} ); } if ( @station_descriptions == 1 ) { ( undef, undef, $station_eva, $station_lon, $station_lat ) @@ -59,14 +59,14 @@ sub get_v0 { } $self->render( json => { - deprecated => \0, + deprecated => \1, checked_in => ( $status->{checked_in} or $status->{cancelled} ) ? \1 : \0, station => { - ds100 => $status->{station_ds100}, - name => $status->{station_name}, + ds100 => $status->{arr_ds100} // $status->{dep_ds100}, + name => $status->{arr_ds100} // $status->{dep_ds100}, uic => $station_eva, longitude => $station_lon, latitude => $station_lat, @@ -77,8 +77,10 @@ sub get_v0 { no => $status->{train_no}, }, actionTime => $status->{timestamp}->epoch, - scheduledTime => $status->{sched_ts}->epoch, - realTime => $status->{real_ts}->epoch, + scheduledTime => $status->{sched_arrival}->epoch + || $status->{sched_departure}->epoch, + realTime => $status->{real_arrival}->epoch + || $status->{real_departure}->epoch, }, ); } diff --git a/lib/Travelynx/Controller/Static.pm b/lib/Travelynx/Controller/Static.pm index 0144d83..09d7f51 100644 --- a/lib/Travelynx/Controller/Static.pm +++ b/lib/Travelynx/Controller/Static.pm @@ -6,7 +6,8 @@ my $travelynx_version = qx{git describe --dirty} || 'experimental'; sub about { my ($self) = @_; - $self->render( 'about', version => $self->app->config->{version} // 'UNKNOWN' ); + $self->render( 'about', + version => $self->app->config->{version} // 'UNKNOWN' ); } sub imprint { diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index b43c891..d8e5e03 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -97,13 +97,16 @@ sub log_action { else { $self->render( json => { - success => 1, + success => 1, + redirect_to => '/', }, ); } } elsif ( $params->{action} eq 'checkout' ) { - my $error = $self->checkout( $params->{station}, $params->{force} ); + my ( $still_checked_in, $error ) + = $self->checkout( $params->{station}, $params->{force} ); + my $station_link = '/s/' . $params->{station}; if ($error) { $self->render( @@ -116,7 +119,8 @@ sub log_action { else { $self->render( json => { - success => 1, + success => 1, + redirect_to => $still_checked_in ? '/' : $station_link, }, ); } @@ -134,7 +138,8 @@ sub log_action { else { $self->render( json => { - success => 1, + success => 1, + redirect_to => '/', }, ); } @@ -155,13 +160,15 @@ sub log_action { else { $self->render( json => { - success => 1, + success => 1, + redirect_to => '/', }, ); } } elsif ( $params->{action} eq 'cancelled_to' ) { - my $error = $self->checkout( $params->{station}, 1, + my ( undef, $error ) + = $self->checkout( $params->{station}, 1, $self->app->action_type->{cancelled_to} ); if ($error) { @@ -175,14 +182,14 @@ sub log_action { else { $self->render( json => { - success => 1, + success => 1, + redirect_to => '/', }, ); } } elsif ( $params->{action} eq 'delete' ) { - my ( $from, $to ) = split( qr{,}, $params->{ids} ); - my $error = $self->delete_journey( $from, $to, $params->{checkin}, + my $error = $self->delete_journey( $params->{id}, $params->{checkin}, $params->{checkout} ); if ($error) { $self->render( @@ -195,7 +202,8 @@ sub log_action { else { $self->render( json => { - success => 1, + success => 1, + redirect_to => '/history', }, ); } @@ -215,7 +223,7 @@ sub station { my $station = $self->stash('station'); my $train = $self->param('train'); - my $status = $self->get_departures($station); + my $status = $self->get_departures( $station, 120, 30 ); if ( $status->{errstr} ) { $self->render( @@ -382,11 +390,13 @@ sub monthly_history { sub journey_details { my ($self) = @_; - my ( $uid, $checkout_id ) = split( qr{-}, $self->stash('id') ); + my $journey_id = $self->stash('id'); + + my $uid = $self->current_user->{id}; - $self->param( journey_id => $checkout_id ); + $self->param( journey_id => $journey_id ); - if ( not( $uid == $self->current_user->{id} and $checkout_id ) ) { + if ( not($journey_id) ) { $self->render( 'journey', error => 'notfound', @@ -395,36 +405,35 @@ sub journey_details { return; } - my @journeys = $self->get_user_travels( - uid => $uid, - checkout_id => $checkout_id, - verbose => 1, + my $journey = $self->get_journey( + uid => $uid, + journey_id => $journey_id, + verbose => 1, ); - if ( @journeys == 0 - or not $journeys[0]{completed} - or $journeys[0]{ids}[1] != $checkout_id ) - { + + if ($journey) { + $self->render( + 'journey', + error => undef, + journey => $journey, + ); + } + else { $self->render( 'journey', error => 'notfound', journey => {} ); - return; } - $self->render( - 'journey', - error => undef, - journey => $journeys[0] - ); } sub edit_journey { - my ($self) = @_; - my $checkout_id = $self->param('journey_id'); - my $uid = $self->current_user->{id}; + my ($self) = @_; + my $journey_id = $self->param('journey_id'); + my $uid = $self->current_user->{id}; - if ( not( $uid == $self->current_user->{id} and $checkout_id ) ) { + if ( not( $journey_id =~ m{ ^ \d+ $ }x ) ) { $self->render( 'edit_journey', error => 'notfound', @@ -434,8 +443,8 @@ sub edit_journey { } my $journey = $self->get_journey( - uid => $uid, - checkout_id => $checkout_id + uid => $uid, + journey_id => $journey_id ); if ( not $journey ) { @@ -449,11 +458,6 @@ sub edit_journey { my $error = undef; - if ( $self->param('action') and $self->param('action') eq 'cancel' ) { - $self->redirect_to("/journey/${uid}-${checkout_id}"); - return; - } - if ( $self->param('action') and $self->param('action') eq 'save' ) { my $parser = DateTime::Format::Strptime->new( pattern => '%d.%m.%Y %H:%M', @@ -468,12 +472,8 @@ sub edit_journey { { my $datetime = $parser->parse_datetime( $self->param($key) ); if ( $datetime and $datetime->epoch ne $journey->{$key}->epoch ) { - $error = $self->update_journey_part( - $db, - $journey->{ids}[0], - $journey->{ids}[1], - $key, $datetime - ); + $error = $self->update_journey_part( $db, $journey->{id}, + $key, $datetime ); if ($error) { last; } @@ -482,17 +482,16 @@ sub edit_journey { if ( not $error ) { $journey = $self->get_journey( - uid => $uid, - db => $db, - checkout_id => $checkout_id, - verbose => 1 + uid => $uid, + db => $db, + journey_id => $journey_id, + verbose => 1 ); $error = $self->journey_sanity_check($journey); } if ( not $error ) { $tx->commit; - $self->redirect_to("/journey/${uid}-${checkout_id}"); - $self->invalidate_stats_cache( $journey->{checkout} ); + $self->redirect_to("/journey/${journey_id}"); return; } } -- cgit v1.2.3