Skip to content

Commit bcd90bd

Browse files
committed
extract to function
1 parent 1715036 commit bcd90bd

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

src-tauri/src/database/models/location.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt;
1+
use std::{fmt, i32};
22

33
use serde::{Deserialize, Serialize};
44
use sqlx::{prelude::Type, query, query_as, query_scalar, Error as SqlxError, SqliteExecutor};
@@ -93,19 +93,16 @@ impl Location<Id> {
9393
where
9494
E: SqliteExecutor<'e>,
9595
{
96-
let max_mode = if include_service_locations {
97-
ServiceLocationMode::AlwaysOn as i32
98-
} else {
99-
ServiceLocationMode::Disabled as i32
100-
};
96+
let max_service_location_mode =
97+
Self::get_service_location_mode_filter(include_service_locations);
10198
query_as!(
10299
Self,
103100
"SELECT id, instance_id, name, address, pubkey, endpoint, allowed_ips, dns, network_id,\
104101
route_all_traffic, keepalive_interval, \
105102
location_mfa_mode \"location_mfa_mode: LocationMfaMode\", service_location_mode \"service_location_mode: ServiceLocationMode\" \
106103
FROM location WHERE service_location_mode <= $1 \
107104
ORDER BY name ASC;",
108-
max_mode
105+
max_service_location_mode
109106
)
110107
.fetch_all(executor)
111108
.await
@@ -167,19 +164,16 @@ impl Location<Id> {
167164
where
168165
E: SqliteExecutor<'e>,
169166
{
170-
let max_mode = if include_service_locations {
171-
ServiceLocationMode::AlwaysOn as i32
172-
} else {
173-
ServiceLocationMode::Disabled as i32
174-
};
167+
let max_service_location_mode =
168+
Self::get_service_location_mode_filter(include_service_locations);
175169
query_as!(
176170
Self,
177171
"SELECT id \"id: _\", instance_id, name, address, pubkey, endpoint, allowed_ips, dns, \
178172
network_id, route_all_traffic, keepalive_interval, location_mfa_mode \"location_mfa_mode: LocationMfaMode\", service_location_mode \"service_location_mode: ServiceLocationMode\" \
179173
FROM location WHERE instance_id = $1 AND service_location_mode <= $2 \
180174
ORDER BY name ASC",
181175
instance_id,
182-
max_mode
176+
max_service_location_mode
183177
)
184178
.fetch_all(executor)
185179
.await
@@ -236,6 +230,16 @@ impl Location<Id> {
236230
LocationMfaMode::Internal | LocationMfaMode::External => true,
237231
}
238232
}
233+
234+
/// Returns a filter value that can be used in SQL queries like `service_location_mode <= ?` when querying locations
235+
/// to exclude (<= 1) or include service locations (all service locations modes).
236+
fn get_service_location_mode_filter(include_service_locations: bool) -> i32 {
237+
if include_service_locations {
238+
i32::MAX
239+
} else {
240+
ServiceLocationMode::Disabled as i32
241+
}
242+
}
239243
}
240244

241245
impl Location<NoId> {

0 commit comments

Comments
 (0)