summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rwxr-xr-xlib/Travelynx.pm128
-rwxr-xr-xlib/Travelynx/Model/Journeys.pm140
2 files changed, 161 insertions, 107 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm
index f446cda..c30cfdf 100755
--- a/lib/Travelynx.pm
+++ b/lib/Travelynx.pm
@@ -512,20 +512,10 @@ sub startup {
my $db = $self->pg->db;
my $tx = $db->begin;
- my $journey = $db->select(
- 'journeys',
- '*',
- {
- user_id => $uid,
- id => $journey_id
- }
- )->hash;
- $db->delete(
- 'journeys',
- {
- user_id => $uid,
- id => $journey_id
- }
+ my $journey = $self->journeys->pop(
+ uid => $uid,
+ db => $db,
+ journey_id => $journey_id
);
if ( $journey->{edited} ) {
@@ -744,10 +734,10 @@ sub startup {
);
if ( $has_arrived or $force ) {
- delete $journey->{data};
- $journey->{edited} = 0;
- $journey->{checkout_time} = $now;
- $db->insert( 'journeys', $journey );
+ $self->journeys->add_from_in_transit(
+ db => $db,
+ journey => $journey
+ );
$self->in_transit->delete(
uid => $uid,
db => $db
@@ -777,12 +767,11 @@ sub startup {
# cancelled_from action -> 'cancelled journey' panel on main page
# -> cancelled_to action -> force checkout (causing the
# previous branch to be taken due to $force)
- $journey->{edited} = 0;
- $journey->{checkout_time} = $now;
- $journey->{cancelled} = 1;
- delete $journey->{data};
- $db->insert( 'journeys', $journey );
-
+ $journey->{cancelled} = 1;
+ $self->journeys->add_from_in_transit(
+ db => $db,
+ journey => $journey
+ );
$self->in_transit->set_cancelled_destination(
uid => $uid,
db => $db,
@@ -1080,47 +1069,6 @@ sub startup {
);
$self->helper(
- 'history_years' => sub {
- my ( $self, $uid ) = @_;
- $uid //= $self->current_user->{id},
-
- my $res = $self->pg->db->select(
- 'journeys',
- 'distinct extract(year from real_departure) as year',
- { user_id => $uid },
- { order_by => { -asc => 'year' } }
- );
-
- my @ret;
- for my $row ( $res->hashes->each ) {
- push( @ret, [ $row->{year}, $row->{year} ] );
- }
- return @ret;
- }
- );
-
- $self->helper(
- 'history_months' => sub {
- my ( $self, $uid ) = @_;
- $uid //= $self->current_user->{id},
-
- my $res = $self->pg->db->select(
- 'journeys',
- "distinct to_char(real_departure, 'YYYY.MM') as yearmonth",
- { user_id => $uid },
- { order_by => { -asc => 'yearmonth' } }
- );
-
- my @ret;
- for my $row ( $res->hashes->each ) {
- my ( $year, $month ) = split( qr{[.]}, $row->{yearmonth} );
- push( @ret, [ "${year}/${month}", "${month}.${year}" ] );
- }
- return @ret;
- }
- );
-
- $self->helper(
'add_route_timestamps' => sub {
my ( $self, $uid, $train, $is_departure ) = @_;
@@ -1508,24 +1456,10 @@ sub startup {
return $id;
}
- my $journey = $db->select(
- 'journeys',
- ['checkout_station_id'],
- {
- user_id => $uid,
- cancelled => 0
- },
- {
- limit => 1,
- order_by => { -desc => 'real_departure' }
- }
- )->hash;
-
- if ( not $journey ) {
- return;
- }
-
- return $journey->{checkout_station_id};
+ return $self->journeys->get_latest_checkout_station_id(
+ uid => $uid,
+ db => $db
+ );
}
);
@@ -2054,30 +1988,10 @@ sub startup {
return $ret;
}
- my $latest = $db->select(
- 'journeys_str',
- '*',
- {
- user_id => $uid,
- cancelled => 0
- },
- {
- order_by => { -desc => 'journey_id' },
- limit => 1
- }
- )->expand->hash;
-
- my $latest_cancellation = $db->select(
- 'journeys_str',
- '*',
- {
- user_id => $uid,
- },
- {
- order_by => { -desc => 'journey_id' },
- limit => 1
- }
- )->expand->hash;
+ my ( $latest, $latest_cancellation ) = $self->journeys->get_latest(
+ uid => $uid,
+ db => $db
+ );
if ( $latest_cancellation and $latest_cancellation->{cancelled} ) {
if ( my $station
diff --git a/lib/Travelynx/Model/Journeys.pm b/lib/Travelynx/Model/Journeys.pm
index 868b1bf..3f8bff7 100755
--- a/lib/Travelynx/Model/Journeys.pm
+++ b/lib/Travelynx/Model/Journeys.pm
@@ -197,6 +197,18 @@ sub add {
return ( $journey_id, undef );
}
+sub add_from_in_transit {
+ my ( $self, %opt ) = @_;
+ my $db = $opt{db};
+ my $journey = $opt{journey};
+
+ delete $journey->{data};
+ $journey->{edited} = 0;
+ $journey->{checkout_time} = DateTime->now( time_zone => 'Europe/Berlin' );
+
+ $db->insert( 'journeys', $journey );
+}
+
sub update {
my ( $self, %opt ) = @_;
@@ -414,6 +426,34 @@ sub delete {
return sprintf( 'Deleted %d rows, expected 1', $rows );
}
+# Used for undo (move journey entry to in_transit)
+sub pop {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db};
+ my $journey_id = $opt{journey_id};
+
+ my $journey = $db->select(
+ 'journeys',
+ '*',
+ {
+ user_id => $uid,
+ id => $journey_id
+ }
+ )->hash;
+
+ $db->delete(
+ 'journeys',
+ {
+ user_id => $uid,
+ id => $journey_id
+ }
+ );
+
+ return $journey;
+}
+
sub get {
my ( $self, %opt ) = @_;
@@ -564,6 +604,40 @@ sub get_single {
return $journeys[0];
}
+sub get_latest {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+
+ my $latest_successful = $db->select(
+ 'journeys_str',
+ '*',
+ {
+ user_id => $uid,
+ cancelled => 0
+ },
+ {
+ order_by => { -desc => 'journey_id' },
+ limit => 1
+ }
+ )->expand->hash;
+
+ my $latest = $db->select(
+ 'journeys_str',
+ '*',
+ {
+ user_id => $uid,
+ },
+ {
+ order_by => { -desc => 'journey_id' },
+ limit => 1
+ }
+ )->expand->hash;
+
+ return ( $latest_successful, $latest );
+}
+
sub get_oldest_ts {
my ( $self, %opt ) = @_;
my $uid = $opt{uid};
@@ -589,6 +663,72 @@ sub get_oldest_ts {
return undef;
}
+sub get_latest_checkout_station_id {
+ my ( $self, %opt ) = @_;
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+
+ my $res_h = $db->select(
+ 'journeys',
+ ['checkout_station_id'],
+ {
+ user_id => $uid,
+ cancelled => 0
+ },
+ {
+ limit => 1,
+ order_by => { -desc => 'real_departure' }
+ }
+ )->hash;
+
+ if ( not $res_h ) {
+ return;
+ }
+
+ return $res_h->{checkout_station_id};
+}
+
+sub get_years {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+
+ my $res = $db->select(
+ 'journeys',
+ 'distinct extract(year from real_departure) as year',
+ { user_id => $uid },
+ { order_by => { -asc => 'year' } }
+ );
+
+ my @ret;
+ for my $row ( $res->hashes->each ) {
+ push( @ret, [ $row->{year}, $row->{year} ] );
+ }
+ return @ret;
+}
+
+sub get_months {
+ my ( $self, %opt ) = @_;
+
+ my $uid = $opt{uid};
+ my $db = $opt{db} // $self->{pg}->db;
+
+ my $res = $db->select(
+ 'journeys',
+ "distinct to_char(real_departure, 'YYYY.MM') as yearmonth",
+ { user_id => $uid },
+ { order_by => { -asc => 'yearmonth' } }
+ );
+
+ my @ret;
+ for my $row ( $res->hashes->each ) {
+ my ( $year, $month ) = split( qr{[.]}, $row->{yearmonth} );
+ push( @ret, [ "${year}/${month}", "${month}.${year}" ] );
+ }
+ return @ret;
+}
+
sub sanity_check {
my ( $self, $journey, $lax ) = @_;