diff options
author | Birte Kristina Friesel <derf@finalrewind.org> | 2025-08-16 19:17:16 +0200 |
---|---|---|
committer | Birte Kristina Friesel <derf@finalrewind.org> | 2025-08-16 19:17:16 +0200 |
commit | e97f7892a6d1a74e729dc676fb90db8afc408db5 (patch) | |
tree | 651a4693106dca62750d91f9660eba00ec5a0e97 /lib/Travelynx | |
parent | 7d37905add82ce990c69e598f919edbf51c345bf (diff) |
Closes #288
Diffstat (limited to 'lib/Travelynx')
-rw-r--r-- | lib/Travelynx/Controller/Account.pm | 7 | ||||
-rwxr-xr-x | lib/Travelynx/Model/Journeys.pm | 30 |
2 files changed, 37 insertions, 0 deletions
diff --git a/lib/Travelynx/Controller/Account.pm b/lib/Travelynx/Controller/Account.pm index 3a1e281..96be200 100644 --- a/lib/Travelynx/Controller/Account.pm +++ b/lib/Travelynx/Controller/Account.pm @@ -1055,6 +1055,7 @@ sub backend_form { my ($self) = @_; my $user = $self->current_user; + my %backend_by_id; my @backends = $self->stations->get_backends; my @suggested_backends; @@ -1272,8 +1273,13 @@ sub backend_form { } } $backend->{type} = $type; + + $backend_by_id{ $backend->{id} } = $backend; } + my @frequent_backends = map { $backend_by_id{$_} } + $self->journeys->get_frequent_backend_ids( uid => $user->{id} ); + @backends = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ lc( $_->{name} ), $_ ] } grep { $_->{type} } @backends; @@ -1281,6 +1287,7 @@ sub backend_form { $self->render( 'select_backend', suggestions => \@suggested_backends, + frequent => \@frequent_backends, backends => \@backends, user => $user, redirect_to => $self->req->param('redirect_to') // '/', diff --git a/lib/Travelynx/Model/Journeys.pm b/lib/Travelynx/Model/Journeys.pm index 9efa365..8e83374 100755 --- a/lib/Travelynx/Model/Journeys.pm +++ b/lib/Travelynx/Model/Journeys.pm @@ -1943,6 +1943,36 @@ sub get_latest_dest_ids { ); } +sub get_frequent_backend_ids { + my ( $self, %opt ) = @_; + + my $uid = $opt{uid}; + my $threshold = $opt{threshold} + // DateTime->now( time_zone => 'Europe/Berlin' )->subtract( months => 4 ); + my $limit = $opt{limit} // 5; + my $db = $opt{db} //= $self->{pg}->db; + + my $res = $db->select( + 'journeys', + 'count(*) as count, backend_id', + { + user_id => $uid, + real_departure => { '>', $threshold }, + }, + { + group_by => ['backend_id'], + order_by => { -desc => 'count' }, + limit => $limit, + } + ); + + my @backend_ids = $res->hashes->map( sub { shift->{backend_id} } )->each; + + say join( ' ', @backend_ids ); + + return @backend_ids; +} + # Returns a listref of {eva, name} hashrefs for the specified backend. sub get_connection_targets { my ( $self, %opt ) = @_; |