summaryrefslogtreecommitdiff
path: root/lib/Travelynx/Model/Stations.pm
blob: ac4019c3805b99618930f00ce68cf1c047580624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package Travelynx::Model::Stations;

# Copyright (C) 2022 Birte Kristina Friesel
#
# SPDX-License-Identifier: AGPL-3.0-or-later

use strict;
use warnings;
use 5.020;

sub new {
	my ( $class, %opt ) = @_;

	return bless( \%opt, $class );
}

sub add_or_update {
	my ( $self, %opt ) = @_;
	my $stop   = $opt{stop};
	my $loc    = $stop->loc;
	my $source = 1;
	my $db     = $opt{db} // $self->{pg}->db;

	if ( my $s = $self->get_by_eva( $loc->eva, db => $db ) ) {
		if ( $source == 1 and $s->{source} == 0 and not $s->{archived} ) {
			return;
		}
		$db->update(
			'stations',
			{
				name     => $loc->name,
				lat      => $loc->lat,
				lon      => $loc->lon,
				source   => $source,
				archived => 0
			},
			{ eva => $loc->eva }
		);
		return;
	}
	$db->insert(
		'stations',
		{
			eva      => $loc->eva,
			name     => $loc->name,
			lat      => $loc->lat,
			lon      => $loc->lon,
			source   => $source,
			archived => 0
		}
	);
}

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 }
			);
		}
	}
}

sub get_db_iterator {
	my ($self) = @_;

	return $self->{pg}->db->select( 'stations', '*' );
}

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;
}

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;
}

# 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_latlon_by_name {
	my ( $self, %opt ) = @_;

	my $db = $opt{db} // $self->{pg}->db;

	my %location;
	my $res = $db->select( 'stations', [ 'name', 'lat', 'lon' ] );
	while ( my $row = $res->hash ) {
		$location{ $row->{name} } = [ $row->{lat}, $row->{lon} ];
	}
	return \%location;
}

# 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;
}

# Slow
sub get_by_names {
	my ( $self, @names ) = @_;

	my @ret
	  = $self->{pg}->db->select( 'stations', '*', { name => { '=', \@names } } )
	  ->hashes->each;
	return @ret;
}

# 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 );
}

# 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;
}

1;