summaryrefslogtreecommitdiff
path: root/lib/Travelynx
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-10-11 19:38:01 +0200
committerDaniel Friesel <derf@finalrewind.org>2020-10-11 19:38:01 +0200
commitb1348c7d4a4f8432859c36217d87b20fdc359cb2 (patch)
treee5cff2c62caa46bd0929362f510b9d3b2fd59e7b /lib/Travelynx
parentb6330217f086360d865163e29285ea2bf6c79320 (diff)
move in_transit queries to an InTransit model class
Diffstat (limited to 'lib/Travelynx')
-rw-r--r--lib/Travelynx/Command/maintenance.pm2
-rw-r--r--lib/Travelynx/Command/work.pm10
-rwxr-xr-xlib/Travelynx/Controller/Api.pm12
-rwxr-xr-xlib/Travelynx/Controller/Traveling.pm5
-rw-r--r--lib/Travelynx/Helper/IRIS.pm37
-rw-r--r--lib/Travelynx/Model/InTransit.pm298
6 files changed, 355 insertions, 9 deletions
diff --git a/lib/Travelynx/Command/maintenance.pm b/lib/Travelynx/Command/maintenance.pm
index e92018e..d9ce55a 100644
--- a/lib/Travelynx/Command/maintenance.pm
+++ b/lib/Travelynx/Command/maintenance.pm
@@ -111,6 +111,8 @@ sub run {
my $stats_res = $db->delete( 'journey_stats', { user_id => $uid } );
my $journeys_res = $db->delete( 'journeys', { user_id => $uid } );
my $transit_res = $db->delete( 'in_transit', { user_id => $uid } );
+
+ # TODO + traewelling, webhooks
my $password_res
= $db->delete( 'pending_passwords', { user_id => $uid } );
my $user_res = $db->delete( 'users', { id => $uid } );
diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm
index a97dc62..a43a5b9 100644
--- a/lib/Travelynx/Command/work.pm
+++ b/lib/Travelynx/Command/work.pm
@@ -60,8 +60,9 @@ sub run {
{
dep_platform => $train->platform,
real_departure => $train->departure,
- route =>
- $json->encode( [ $self->app->route_diff($train) ] ),
+ route => $json->encode(
+ [ $self->app->iris->route_diff($train) ]
+ ),
messages => $json->encode(
[
map { [ $_->[0]->epoch, $_->[1] ] }
@@ -170,8 +171,9 @@ sub run {
arr_platform => $train->platform,
sched_arrival => $train->sched_arrival,
real_arrival => $train->arrival,
- route =>
- $json->encode( [ $self->app->route_diff($train) ] ),
+ route => $json->encode(
+ [ $self->app->iris->route_diff($train) ]
+ ),
messages => $json->encode(
[
map { [ $_->[0]->epoch, $_->[1] ] }
diff --git a/lib/Travelynx/Controller/Api.pm b/lib/Travelynx/Controller/Api.pm
index 8d2d2a7..74260c4 100755
--- a/lib/Travelynx/Controller/Api.pm
+++ b/lib/Travelynx/Controller/Api.pm
@@ -264,8 +264,10 @@ sub travel_v1 {
uid => $uid
);
if ( $payload->{comment} and not $error ) {
- $self->update_in_transit_comment(
- sanitize( q{}, $payload->{comment} ), $uid );
+ $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(
@@ -310,8 +312,10 @@ sub travel_v1 {
}
if ( $payload->{comment} ) {
- $self->update_in_transit_comment(
- sanitize( q{}, $payload->{comment} ), $uid );
+ $self->in_transit->update_user_data(
+ uid => $uid,
+ user_data => { comment => sanitize( q{}, $payload->{comment} ) }
+ );
}
my ( $train, $error ) = $self->checkout(
diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm
index 4df1558..6a1d1e1 100755
--- a/lib/Travelynx/Controller/Traveling.pm
+++ b/lib/Travelynx/Controller/Traveling.pm
@@ -1091,7 +1091,10 @@ sub comment_form {
}
else {
$self->app->log->debug("set comment");
- $self->update_in_transit_comment( $self->param('comment') );
+ $self->in_transit->update_user_data(
+ uid => $self->current_user->{id},
+ user_data => { comment => $self->param('comment') }
+ );
$self->redirect_to('/');
}
}
diff --git a/lib/Travelynx/Helper/IRIS.pm b/lib/Travelynx/Helper/IRIS.pm
index 3b98a51..a8ca6c3 100644
--- a/lib/Travelynx/Helper/IRIS.pm
+++ b/lib/Travelynx/Helper/IRIS.pm
@@ -68,4 +68,41 @@ sub get_departures {
}
}
+sub route_diff {
+ my ( $self, $train ) = @_;
+ my @json_route;
+ my @route = $train->route;
+ my @sched_route = $train->sched_route;
+
+ my $route_idx = 0;
+ my $sched_idx = 0;
+
+ while ( $route_idx <= $#route and $sched_idx <= $#sched_route ) {
+ if ( $route[$route_idx] eq $sched_route[$sched_idx] ) {
+ push( @json_route, [ $route[$route_idx], {}, undef ] );
+ $route_idx++;
+ $sched_idx++;
+ }
+
+ # this branch is inefficient, but won't be taken frequently
+ elsif ( not( grep { $_ eq $route[$route_idx] } @sched_route ) ) {
+ push( @json_route, [ $route[$route_idx], {}, 'additional' ], );
+ $route_idx++;
+ }
+ else {
+ push( @json_route, [ $sched_route[$sched_idx], {}, 'cancelled' ], );
+ $sched_idx++;
+ }
+ }
+ while ( $route_idx <= $#route ) {
+ push( @json_route, [ $route[$route_idx], {}, 'additional' ], );
+ $route_idx++;
+ }
+ while ( $sched_idx <= $#sched_route ) {
+ push( @json_route, [ $sched_route[$sched_idx], {}, 'cancelled' ], );
+ $sched_idx++;
+ }
+ return @json_route;
+}
+
1;
diff --git a/lib/Travelynx/Model/InTransit.pm b/lib/Travelynx/Model/InTransit.pm
new file mode 100644
index 0000000..6aeab5f
--- /dev/null
+++ b/lib/Travelynx/Model/InTransit.pm
@@ -0,0 +1,298 @@
+package Travelynx::Model::InTransit;
+
+use strict;
+use warnings;
+use 5.020;
+
+use DateTime;
+use JSON;
+
+sub new {
+ my ( $class, %opt ) = @_;
+
+ return bless( \%opt, $class );
+}
+
+sub add {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $train = $opt{train};
+ my $checkin_station_id = $opt{departure_eva};
+ my $route = $opt{route};
+
+ my $json = JSON->new;
+
+ $db->insert(
+ 'in_transit',
+ {
+ user_id => $uid,
+ cancelled => $train->departure_is_cancelled
+ ? 1
+ : 0,
+ checkin_station_id => $checkin_station_id,
+ checkin_time => DateTime->now( time_zone => 'Europe/Berlin' ),
+ dep_platform => $train->platform,
+ train_type => $train->type,
+ train_line => $train->line_no,
+ train_no => $train->train_no,
+ train_id => $train->train_id,
+ sched_departure => $train->sched_departure,
+ real_departure => $train->departure,
+ route => $json->encode($route),
+ messages => $json->encode(
+ [ map { [ $_->[0]->epoch, $_->[1] ] } $train->messages ]
+ )
+ }
+ );
+}
+
+sub add_from_journey {
+ my ( $self, %opt ) = @_;
+
+ my $journey = $opt{journey};
+ my $db = $opt{db} // $self->{pg}->db;
+
+ $db->insert( 'in_transit', $journey );
+}
+
+sub delete {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+
+ $db->delete( 'in_transit', { user_id => $uid } );
+}
+
+sub get {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+
+ my $table = 'in_transit';
+
+ if ( $opt{with_timestamps} ) {
+ $table = 'in_transit_str';
+ }
+
+ my $res = $db->select( $table, '*', { user_id => $uid } );
+
+ if ( $opt{with_data} ) {
+ return $res->expand->hash;
+ }
+ return $res->hash;
+}
+
+sub get_checkout_station_id {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+
+ my $status = $db->select( 'in_transit', ['checkout_station_id'],
+ { user_id => $uid } )->hash;
+
+ if ($status) {
+ return $status->{checkout_station_id};
+ }
+ return;
+}
+
+sub set_cancelled_destination {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $cancelled_destination = $opt{cancelled_destination};
+
+ my $res_h = $db->select( 'in_transit', ['data'], { user_id => $uid } )
+ ->expand->hash;
+
+ my $data = $res_h ? $res_h->{data} : {};
+
+ $data->{cancelled_destination} = $cancelled_destination;
+
+ $db->update(
+ 'in_transit',
+ {
+ checkout_station_id => undef,
+ checkout_time => undef,
+ arr_platform => undef,
+ sched_arrival => undef,
+ real_arrival => undef,
+ data => JSON->new->encode($data),
+ },
+ { user_id => $uid }
+ );
+}
+
+sub set_arrival {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $train = $opt{train};
+ my $route = $opt{route};
+
+ my $json = JSON->new;
+
+ $db->update(
+ 'in_transit',
+ {
+ checkout_time => DateTime->now( time_zone => 'Europe/Berlin' ),
+ arr_platform => $train->platform,
+ sched_arrival => $train->sched_arrival,
+ real_arrival => $train->arrival,
+ route => $json->encode($route),
+ messages => $json->encode(
+ [ map { [ $_->[0]->epoch, $_->[1] ] } $train->messages ]
+ )
+ },
+ { user_id => $uid }
+ );
+}
+
+sub set_arrival_eva {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $checkout_station_id = $opt{arrival_eva};
+
+ $db->update(
+ 'in_transit',
+ {
+ checkout_station_id => $checkout_station_id,
+ },
+ { user_id => $uid }
+ );
+}
+
+sub set_arrival_times {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $sched_arr = $opt{sched_arrival};
+ my $rt_arr = $opt{rt_arrival};
+
+ $db->update(
+ 'in_transit',
+ {
+ sched_arrival => $sched_arr,
+ real_arrival => $rt_arr
+ },
+ { user_id => $uid }
+ );
+}
+
+sub set_polyline_id {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $polyline_id = $opt{polyline_id};
+
+ $db->update(
+ 'in_transit',
+ { polyline_id => $polyline_id },
+ { user_id => $uid }
+ );
+}
+
+sub set_route_data {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $route = $opt{route};
+ my $delay_msg = $opt{delay_messages};
+ my $qos_msg = $opt{qos_messages};
+ my $him_msg = $opt{him_messages};
+
+ my $res_h = $db->select( 'in_transit', ['data'], { user_id => $uid } )
+ ->expand->hash;
+
+ my $data = $res_h ? $res_h->{data} : {};
+
+ $data->{delay_msg} = $opt{delay_messages};
+ $data->{qos_msg} = $opt{qos_messages};
+ $data->{him_msg} = $opt{him_messages};
+
+ $db->update(
+ 'in_transit',
+ {
+ route => JSON->new->encode($route),
+ data => JSON->new->encode($data)
+ },
+ { user_id => $uid }
+ );
+}
+
+sub unset_arrival_data {
+ my ( $self, %opt ) = @_;
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+
+ $db->update(
+ 'in_transit',
+ {
+ checkout_time => undef,
+ arr_platform => undef,
+ sched_arrival => undef,
+ real_arrival => undef,
+ },
+ { user_id => $uid }
+ );
+}
+
+sub update_data {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $new_data = $opt{data} // {};
+
+ my $res_h = $db->select( 'in_transit', ['data'], { user_id => $uid } )
+ ->expand->hash;
+
+ my $data = $res_h ? $res_h->{data} : {};
+
+ while ( my ( $k, $v ) = each %{$new_data} ) {
+ $data->{$k} = $v;
+ }
+
+ $db->update(
+ 'in_transit',
+ { data => JSON->new->encode($data) },
+ { user_id => $uid }
+ );
+}
+
+sub update_user_data {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+ my $new_data = $opt{user_data} // {};
+
+ my $res_h = $db->select( 'in_transit', ['user_data'], { user_id => $uid } )
+ ->expand->hash;
+
+ my $data = $res_h ? $res_h->{user_data} : {};
+
+ while ( my ( $k, $v ) = each %{$new_data} ) {
+ $data->{$k} = $v;
+ }
+
+ $db->update(
+ 'in_transit',
+ { user_data => JSON->new->encode($data) },
+ { user_id => $uid }
+ );
+}
+
+1;