summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Model/Stations.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Travelynx/Model/Stations.pm')
-rw-r--r--lib/Travelynx/Model/Stations.pm96
1 files changed, 95 insertions, 1 deletions
diff --git a/lib/Travelynx/Model/Stations.pm b/lib/Travelynx/Model/Stations.pm
index 3d6549f..761c5de 100644
--- a/lib/Travelynx/Model/Stations.pm
+++ b/lib/Travelynx/Model/Stations.pm
@@ -1,6 +1,7 @@
package Travelynx::Model::Stations;
# Copyright (C) 2022 Birte Kristina Friesel
+# Copyright (C) 2025 networkException <git@nwex.de>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
@@ -28,6 +29,9 @@ sub get_backend_id {
if ( $opt{dbris} and $self->{backend_id}{dbris}{ $opt{dbris} } ) {
return $self->{backend_id}{dbris}{ $opt{dbris} };
}
+ if ( $opt{motis} and $self->{backend_id}{motis}{ $opt{motis} } ) {
+ return $self->{backend_id}{motis}{ $opt{motis} };
+ }
my $db = $opt{db} // $self->{pg}->db;
my $backend_id = 0;
@@ -54,6 +58,17 @@ sub get_backend_id {
)->hash->{id};
$self->{backend_id}{hafas}{ $opt{hafas} } = $backend_id;
}
+ elsif ( $opt{motis} ) {
+ $backend_id = $db->select(
+ 'backends',
+ ['id'],
+ {
+ motis => 1,
+ name => $opt{motis}
+ }
+ )->hash->{id};
+ $self->{backend_id}{motis}{ $opt{motis} } = $backend_id;
+ }
return $backend_id;
}
@@ -85,7 +100,7 @@ sub get_backends {
$opt{db} //= $self->{pg}->db;
my $res = $opt{db}
- ->select( 'backends', [ 'id', 'name', 'iris', 'hafas', 'dbris' ] );
+ ->select( 'backends', [ 'id', 'name', 'iris', 'hafas', 'dbris', 'motis' ] );
my @ret;
while ( my $row = $res->hash ) {
@@ -97,6 +112,7 @@ sub get_backends {
iris => $row->{iris},
dbris => $row->{dbris},
hafas => $row->{hafas},
+ motis => $row->{motis},
}
);
}
@@ -149,6 +165,61 @@ sub add_or_update {
return;
}
+ if ( $opt{motis} ) {
+ if (
+ my $s = $self->get_by_external_id(
+ external_id => $stop->id,
+ db => $opt{db},
+ backend_id => $opt{backend_id}
+ )
+ )
+ {
+ $opt{db}->update(
+ 'stations',
+ {
+ name => $stop->name,
+ lat => $stop->lat,
+ lon => $stop->lon,
+ archived => 0
+ },
+ {
+ eva => $s->{eva},
+ source => $opt{backend_id}
+ }
+ );
+
+ $stop->{eva} = $s->{eva};
+
+ return;
+ }
+
+ my $s = $opt{db}->query(
+ qq {
+ with new_station as (
+ insert into stations_external_ids (backend_id, external_id)
+ values (?, ?)
+ returning eva, backend_id
+ )
+
+ insert into stations (eva, name, lat, lon, source, archived)
+ values ((select eva from new_station), ?, ?, ?, (select backend_id from new_station), ?)
+ returning *
+ },
+ (
+ $opt{backend_id},
+ $stop->id,
+ $stop->name,
+ $stop->lat,
+ $stop->lon,
+ 0,
+ )
+ );
+
+ $stop->{eva} = $s->hash->{eva};
+
+ return;
+ }
+
my $loc = $stop->loc;
if (
my $s = $self->get_by_eva(
@@ -184,6 +255,8 @@ sub add_or_update {
archived => 0
}
);
+
+ return;
}
sub add_meta {
@@ -276,6 +349,27 @@ sub get_by_eva {
)->hash;
}
+# Slow
+sub get_by_external_id {
+ my ( $self, %opt ) = @_;
+
+ if ( not $opt{external_id} ) {
+ return;
+ }
+
+ $opt{db} //= $self->{pg}->db;
+ $opt{backend_id} //= $self->get_backend_id(%opt);
+
+ return $opt{db}->select(
+ 'stations_with_external_ids',
+ '*',
+ {
+ external_id => $opt{external_id},
+ source => $opt{backend_id},
+ }
+ )->hash;
+}
+
# Fast
sub get_by_evas {
my ( $self, %opt ) = @_;