summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-10-11 21:08:22 +0200
committerDaniel Friesel <derf@finalrewind.org>2020-10-11 21:08:22 +0200
commite61550f751feb132aa1a51cd860837d753e9dcd7 (patch)
treef51e43d601da2293d5e4fff78aeae163935a0bc8
parent8abb8206ceee179e474e80525c29b7f40585abe5 (diff)
Move remaining journeys queries to journeys model class
-rwxr-xr-xlib/Travelynx.pm128
-rwxr-xr-xlib/Travelynx/Model/Journeys.pm140
-rw-r--r--templates/_history_months.html.ep2
-rw-r--r--templates/_history_years.html.ep2
4 files changed, 163 insertions, 109 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 ) = @_;
diff --git a/templates/_history_months.html.ep b/templates/_history_months.html.ep
index 6a83d74..a707943 100644
--- a/templates/_history_months.html.ep
+++ b/templates/_history_months.html.ep
@@ -1,7 +1,7 @@
<div class="row">
<div class="col s12">
<ul class="pagination">
- % for my $month (history_months()) {
+ % for my $month (journeys->get_months(uid => current_user->{id})) {
% my $link_to = $month->[0];
% my $text = $month->[1];
% my $class = $link_to eq $current ? 'active' : 'waves-effect';
diff --git a/templates/_history_years.html.ep b/templates/_history_years.html.ep
index 768c438..e0b1357 100644
--- a/templates/_history_years.html.ep
+++ b/templates/_history_years.html.ep
@@ -1,7 +1,7 @@
<div class="row">
<div class="col s12">
<ul class="pagination">
- % for my $year (history_years()) {
+ % for my $year (journeys->get_years(uid => current_user->{id})) {
% my $link_to = $year->[0];
% my $text = $year->[1];
% my $class = $link_to eq $current ? 'active' : 'waves-effect';