mirror of
https://github.com/derf/travelynx
synced 2024-12-04 02:09:09 +00:00
Move remaining journeys queries to journeys model class
This commit is contained in:
parent
8abb8206ce
commit
e61550f751
4 changed files with 163 additions and 109 deletions
128
lib/Travelynx.pm
128
lib/Travelynx.pm
|
@ -512,20 +512,10 @@ sub startup {
|
||||||
my $db = $self->pg->db;
|
my $db = $self->pg->db;
|
||||||
my $tx = $db->begin;
|
my $tx = $db->begin;
|
||||||
|
|
||||||
my $journey = $db->select(
|
my $journey = $self->journeys->pop(
|
||||||
'journeys',
|
uid => $uid,
|
||||||
'*',
|
db => $db,
|
||||||
{
|
journey_id => $journey_id
|
||||||
user_id => $uid,
|
|
||||||
id => $journey_id
|
|
||||||
}
|
|
||||||
)->hash;
|
|
||||||
$db->delete(
|
|
||||||
'journeys',
|
|
||||||
{
|
|
||||||
user_id => $uid,
|
|
||||||
id => $journey_id
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( $journey->{edited} ) {
|
if ( $journey->{edited} ) {
|
||||||
|
@ -744,10 +734,10 @@ sub startup {
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( $has_arrived or $force ) {
|
if ( $has_arrived or $force ) {
|
||||||
delete $journey->{data};
|
$self->journeys->add_from_in_transit(
|
||||||
$journey->{edited} = 0;
|
db => $db,
|
||||||
$journey->{checkout_time} = $now;
|
journey => $journey
|
||||||
$db->insert( 'journeys', $journey );
|
);
|
||||||
$self->in_transit->delete(
|
$self->in_transit->delete(
|
||||||
uid => $uid,
|
uid => $uid,
|
||||||
db => $db
|
db => $db
|
||||||
|
@ -777,12 +767,11 @@ sub startup {
|
||||||
# cancelled_from action -> 'cancelled journey' panel on main page
|
# cancelled_from action -> 'cancelled journey' panel on main page
|
||||||
# -> cancelled_to action -> force checkout (causing the
|
# -> cancelled_to action -> force checkout (causing the
|
||||||
# previous branch to be taken due to $force)
|
# previous branch to be taken due to $force)
|
||||||
$journey->{edited} = 0;
|
$journey->{cancelled} = 1;
|
||||||
$journey->{checkout_time} = $now;
|
$self->journeys->add_from_in_transit(
|
||||||
$journey->{cancelled} = 1;
|
db => $db,
|
||||||
delete $journey->{data};
|
journey => $journey
|
||||||
$db->insert( 'journeys', $journey );
|
);
|
||||||
|
|
||||||
$self->in_transit->set_cancelled_destination(
|
$self->in_transit->set_cancelled_destination(
|
||||||
uid => $uid,
|
uid => $uid,
|
||||||
db => $db,
|
db => $db,
|
||||||
|
@ -1079,47 +1068,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(
|
$self->helper(
|
||||||
'add_route_timestamps' => sub {
|
'add_route_timestamps' => sub {
|
||||||
my ( $self, $uid, $train, $is_departure ) = @_;
|
my ( $self, $uid, $train, $is_departure ) = @_;
|
||||||
|
@ -1508,24 +1456,10 @@ sub startup {
|
||||||
return $id;
|
return $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
my $journey = $db->select(
|
return $self->journeys->get_latest_checkout_station_id(
|
||||||
'journeys',
|
uid => $uid,
|
||||||
['checkout_station_id'],
|
db => $db
|
||||||
{
|
);
|
||||||
user_id => $uid,
|
|
||||||
cancelled => 0
|
|
||||||
},
|
|
||||||
{
|
|
||||||
limit => 1,
|
|
||||||
order_by => { -desc => 'real_departure' }
|
|
||||||
}
|
|
||||||
)->hash;
|
|
||||||
|
|
||||||
if ( not $journey ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $journey->{checkout_station_id};
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -2054,30 +1988,10 @@ sub startup {
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
my $latest = $db->select(
|
my ( $latest, $latest_cancellation ) = $self->journeys->get_latest(
|
||||||
'journeys_str',
|
uid => $uid,
|
||||||
'*',
|
db => $db
|
||||||
{
|
);
|
||||||
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;
|
|
||||||
|
|
||||||
if ( $latest_cancellation and $latest_cancellation->{cancelled} ) {
|
if ( $latest_cancellation and $latest_cancellation->{cancelled} ) {
|
||||||
if ( my $station
|
if ( my $station
|
||||||
|
|
|
@ -197,6 +197,18 @@ sub add {
|
||||||
return ( $journey_id, undef );
|
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 {
|
sub update {
|
||||||
my ( $self, %opt ) = @_;
|
my ( $self, %opt ) = @_;
|
||||||
|
|
||||||
|
@ -414,6 +426,34 @@ sub delete {
|
||||||
return sprintf( 'Deleted %d rows, expected 1', $rows );
|
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 {
|
sub get {
|
||||||
my ( $self, %opt ) = @_;
|
my ( $self, %opt ) = @_;
|
||||||
|
|
||||||
|
@ -564,6 +604,40 @@ sub get_single {
|
||||||
return $journeys[0];
|
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 {
|
sub get_oldest_ts {
|
||||||
my ( $self, %opt ) = @_;
|
my ( $self, %opt ) = @_;
|
||||||
my $uid = $opt{uid};
|
my $uid = $opt{uid};
|
||||||
|
@ -589,6 +663,72 @@ sub get_oldest_ts {
|
||||||
return undef;
|
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 {
|
sub sanity_check {
|
||||||
my ( $self, $journey, $lax ) = @_;
|
my ( $self, $journey, $lax ) = @_;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col s12">
|
<div class="col s12">
|
||||||
<ul class="pagination">
|
<ul class="pagination">
|
||||||
% for my $month (history_months()) {
|
% for my $month (journeys->get_months(uid => current_user->{id})) {
|
||||||
% my $link_to = $month->[0];
|
% my $link_to = $month->[0];
|
||||||
% my $text = $month->[1];
|
% my $text = $month->[1];
|
||||||
% my $class = $link_to eq $current ? 'active' : 'waves-effect';
|
% my $class = $link_to eq $current ? 'active' : 'waves-effect';
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col s12">
|
<div class="col s12">
|
||||||
<ul class="pagination">
|
<ul class="pagination">
|
||||||
% for my $year (history_years()) {
|
% for my $year (journeys->get_years(uid => current_user->{id})) {
|
||||||
% my $link_to = $year->[0];
|
% my $link_to = $year->[0];
|
||||||
% my $text = $year->[1];
|
% my $text = $year->[1];
|
||||||
% my $class = $link_to eq $current ? 'active' : 'waves-effect';
|
% my $class = $link_to eq $current ? 'active' : 'waves-effect';
|
||||||
|
|
Loading…
Reference in a new issue