2022-12-07 18:42:48 +00:00
|
|
|
package Travelynx::Model::Stations;
|
|
|
|
|
2023-07-03 15:59:25 +00:00
|
|
|
# Copyright (C) 2022 Birte Kristina Friesel
|
2022-12-07 18:42:48 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use 5.020;
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
my ( $class, %opt ) = @_;
|
|
|
|
|
|
|
|
return bless( \%opt, $class );
|
|
|
|
}
|
|
|
|
|
2023-08-13 10:51:15 +00:00
|
|
|
sub add_or_update {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $stop = $opt{stop};
|
2023-12-27 09:59:35 +00:00
|
|
|
my $loc = $stop->loc;
|
2023-08-13 10:51:15 +00:00
|
|
|
my $source = 1;
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
|
2023-12-27 09:59:35 +00:00
|
|
|
if ( my $s = $self->get_by_eva( $loc->eva, db => $db ) ) {
|
2023-08-13 10:51:15 +00:00
|
|
|
if ( $source == 1 and $s->{source} == 0 and not $s->{archived} ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$db->update(
|
|
|
|
'stations',
|
|
|
|
{
|
2023-12-27 09:59:35 +00:00
|
|
|
name => $loc->name,
|
|
|
|
lat => $loc->lat,
|
|
|
|
lon => $loc->lon,
|
2023-08-13 10:51:15 +00:00
|
|
|
source => $source,
|
|
|
|
archived => 0
|
|
|
|
},
|
2023-12-27 09:59:35 +00:00
|
|
|
{ eva => $loc->eva }
|
2023-08-13 10:51:15 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$db->insert(
|
|
|
|
'stations',
|
|
|
|
{
|
2023-12-27 09:59:35 +00:00
|
|
|
eva => $loc->eva,
|
|
|
|
name => $loc->name,
|
|
|
|
lat => $loc->lat,
|
|
|
|
lon => $loc->lon,
|
2023-08-13 10:51:15 +00:00
|
|
|
source => $source,
|
|
|
|
archived => 0
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-01 05:11:58 +00:00
|
|
|
sub add_meta {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
my $eva = $opt{eva};
|
|
|
|
my @meta = @{ $opt{meta} };
|
|
|
|
|
|
|
|
for my $meta (@meta) {
|
|
|
|
if ( $meta != $eva ) {
|
|
|
|
$db->insert(
|
|
|
|
'related_stations',
|
|
|
|
{
|
|
|
|
eva => $eva,
|
|
|
|
meta => $meta
|
|
|
|
},
|
|
|
|
{ on_conflict => undef }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-09 18:09:16 +00:00
|
|
|
sub get_db_iterator {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
return $self->{pg}->db->select( 'stations', '*' );
|
|
|
|
}
|
|
|
|
|
2023-10-01 05:11:58 +00:00
|
|
|
sub get_meta {
|
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
my $eva = $opt{eva};
|
|
|
|
|
|
|
|
my $res = $db->select( 'related_stations', ['meta'], { eva => $eva } );
|
|
|
|
my @ret;
|
|
|
|
|
|
|
|
while ( my $row = $res->hash ) {
|
|
|
|
push( @ret, $row->{meta} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return @ret;
|
|
|
|
}
|
|
|
|
|
2023-08-26 07:17:28 +00:00
|
|
|
sub get_for_autocomplete {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $res = $self->{pg}->db->select( 'stations', ['name'] );
|
|
|
|
my %ret;
|
|
|
|
|
|
|
|
while ( my $row = $res->hash ) {
|
|
|
|
$ret{ $row->{name} } = undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return \%ret;
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:42:48 +00:00
|
|
|
# Fast
|
|
|
|
sub get_by_eva {
|
|
|
|
my ( $self, $eva, %opt ) = @_;
|
|
|
|
|
|
|
|
if ( not $eva ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
|
|
|
|
return $db->select( 'stations', '*', { eva => $eva } )->hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Fast
|
|
|
|
sub get_by_evas {
|
|
|
|
my ( $self, @evas ) = @_;
|
|
|
|
|
|
|
|
my @ret
|
|
|
|
= $self->{pg}->db->select( 'stations', '*', { eva => { '=', \@evas } } )
|
|
|
|
->hashes->each;
|
|
|
|
return @ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Slow
|
|
|
|
sub get_by_name {
|
|
|
|
my ( $self, $name, %opt ) = @_;
|
|
|
|
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
|
|
|
|
return $db->select( 'stations', '*', { name => $name }, { limit => 1 } )
|
|
|
|
->hash;
|
|
|
|
}
|
|
|
|
|
2022-12-08 17:22:09 +00:00
|
|
|
# Slow
|
|
|
|
sub get_by_names {
|
|
|
|
my ( $self, @names ) = @_;
|
|
|
|
|
|
|
|
my @ret
|
|
|
|
= $self->{pg}->db->select( 'stations', '*', { name => { '=', \@names } } )
|
|
|
|
->hashes->each;
|
|
|
|
return @ret;
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:42:48 +00:00
|
|
|
# Slow
|
|
|
|
sub get_by_ds100 {
|
|
|
|
my ( $self, $ds100, %opt ) = @_;
|
|
|
|
|
|
|
|
my $db = $opt{db} // $self->{pg}->db;
|
|
|
|
|
|
|
|
return $db->select( 'stations', '*', { ds100 => $ds100 }, { limit => 1 } )
|
|
|
|
->hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Can be slow
|
|
|
|
sub search {
|
|
|
|
my ( $self, $identifier, %opt ) = @_;
|
|
|
|
|
|
|
|
if ( $identifier =~ m{ ^ \d+ $ }x ) {
|
|
|
|
return $self->get_by_eva( $identifier, %opt )
|
|
|
|
// $self->get_by_ds100( $identifier, %opt )
|
|
|
|
// $self->get_by_name( $identifier, %opt );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->get_by_ds100( $identifier, %opt )
|
|
|
|
// $self->get_by_name( $identifier, %opt );
|
|
|
|
}
|
|
|
|
|
2022-12-08 17:22:09 +00:00
|
|
|
# Slow
|
|
|
|
sub grep_unknown {
|
|
|
|
my ( $self, @stations ) = @_;
|
|
|
|
|
|
|
|
my %station = map { $_->{name} => 1 } $self->get_by_names(@stations);
|
|
|
|
my @unknown_stations = grep { not $station{$_} } @stations;
|
|
|
|
|
|
|
|
return @unknown_stations;
|
|
|
|
}
|
|
|
|
|
2022-12-07 18:42:48 +00:00
|
|
|
1;
|