diff options
-rwxr-xr-x | lib/Travelynx.pm | 104 | ||||
-rw-r--r-- | lib/Travelynx/Model/InTransit.pm | 55 |
2 files changed, 64 insertions, 95 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 6bf9f02..4d04e9e 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -621,52 +621,11 @@ sub startup { } if ($polyline) { - my $coords = $polyline->{coords}; - my $from_eva = $polyline->{from_eva}; - my $to_eva = $polyline->{to_eva}; - - my $polyline_str = JSON->new->encode($coords); - - my $pl_res = $db->select( - 'polylines', - ['id'], - { - origin_eva => $from_eva, - destination_eva => $to_eva, - polyline => $polyline_str - }, - { limit => 1 } + $self->in_transit->set_polyline( + uid => $uid, + db => $db, + polyline => $polyline, ); - - my $polyline_id; - if ( my $h = $pl_res->hash ) { - $polyline_id = $h->{id}; - } - else { - eval { - $polyline_id = $db->insert( - 'polylines', - { - origin_eva => $from_eva, - destination_eva => $to_eva, - polyline => $polyline_str - }, - { returning => 'id' } - )->hash->{id}; - }; - if ($@) { - $self->log->warn( - "add_route_timestamps: insert polyline: $@" - ); - } - } - if ($polyline_id) { - $self->in_transit->set_polyline_id( - uid => $uid, - db => $db, - polyline_id => $polyline_id - ); - } } # mustn't be called during a transaction @@ -1372,57 +1331,12 @@ sub startup { ); if ($polyline) { - my $coords = $polyline->{coords}; - my $from_eva = $polyline->{from_eva}; - my $to_eva = $polyline->{to_eva}; - - my $polyline_str = JSON->new->encode($coords); - - my $pl_res = $db->select( - 'polylines', - ['id'], - { - origin_eva => $from_eva, - destination_eva => $to_eva, - polyline => $polyline_str - }, - { limit => 1 } + $self->in_transit->set_polyline( + uid => $uid, + db => $db, + polyline => $polyline, + old_id => $in_transit->{polyline_id}, ); - - my $polyline_id; - if ( my $h = $pl_res->hash ) { - $polyline_id = $h->{id}; - } - else { - eval { - $polyline_id = $db->insert( - 'polylines', - { - origin_eva => $from_eva, - destination_eva => $to_eva, - polyline => $polyline_str - }, - { returning => 'id' } - )->hash->{id}; - }; - if ($@) { - $self->app->log->warn( - "add_route_timestamps: insert polyline: $@" - ); - } - } - if ( - $polyline_id - and ( not $in_transit->{polyline_id} - or $polyline_id != $in_transit->{polyline_id} ) - ) - { - $self->in_transit->set_polyline_id( - uid => $uid, - db => $db, - polyline_id => $polyline_id - ); - } } return; diff --git a/lib/Travelynx/Model/InTransit.pm b/lib/Travelynx/Model/InTransit.pm index 0859aed..69026ac 100644 --- a/lib/Travelynx/Model/InTransit.pm +++ b/lib/Travelynx/Model/InTransit.pm @@ -517,6 +517,61 @@ sub set_arrival_times { ); } +sub set_polyline { + my ( $self, %opt ) = @_; + + my $uid = $opt{uid}; + my $db = $opt{db} // $self->{pg}->db; + my $polyline = $opt{polyline}; + my $old_id = $opt{old_id}; + + my $coords = $polyline->{coords}; + my $from_eva = $polyline->{from_eva}; + my $to_eva = $polyline->{to_eva}; + + my $polyline_str = JSON->new->encode($coords); + + my $pl_res = $db->select( + 'polylines', + ['id'], + { + origin_eva => $from_eva, + destination_eva => $to_eva, + polyline => $polyline_str, + }, + { limit => 1 } + ); + + my $polyline_id; + if ( my $h = $pl_res->hash ) { + $polyline_id = $h->{id}; + } + else { + eval { + $polyline_id = $db->insert( + 'polylines', + { + origin_eva => $from_eva, + destination_eva => $to_eva, + polyline => $polyline_str + }, + { returning => 'id' } + )->hash->{id}; + }; + if ($@) { + $self->{log}->warn("add_route_timestamps: insert polyline: $@"); + } + } + if ( $polyline_id and ( not defined $old_id or $polyline_id != $old_id ) ) { + $self->set_polyline_id( + uid => $uid, + db => $db, + polyline_id => $polyline_id + ); + } + +} + sub set_polyline_id { my ( $self, %opt ) = @_; |