| 71 | } |
| 72 | |
| 73 | pub async fn list_relays( |
| 74 | limit: i64, |
| 75 | offset: i64, |
| 76 | filters: &RelayFilters, |
| 77 | ) -> Result<Vec<RelayListItem>, Error> { |
| 78 | let q = device::dsl::device |
| 79 | .inner_join(device_profile::table) |
| 80 | .select((device::dev_eui, device::name)); |
| 81 | |
| 82 | #[cfg(feature = "postgres")] |
| 83 | let mut q = q |
| 84 | .filter( |
| 85 | dsl::sql::<sql_types::Bool>("(relay_params->'is_relay')::boolean =") |
| 86 | .bind::<sql_types::Bool, _>(true), |
| 87 | ) |
| 88 | .into_boxed(); |
| 89 | |
| 90 | #[cfg(feature = "sqlite")] |
| 91 | let mut q = q |
| 92 | .filter( |
| 93 | dsl::sql::<sql_types::Bool>("relay_params->>'is_relay' =") |
| 94 | .bind::<sql_types::Bool, _>(true), |
| 95 | ) |
| 96 | .into_boxed(); |
| 97 | |
| 98 | if let Some(application_id) = &filters.application_id { |
| 99 | q = q.filter(device::dsl::application_id.eq(fields::Uuid::from(application_id))); |
| 100 | } |
| 101 | |
| 102 | q.order_by(device::dsl::name) |
| 103 | .limit(limit) |
| 104 | .offset(offset) |
| 105 | .load(&mut get_async_db_conn().await?) |
| 106 | .await |
| 107 | .map_err(|e| Error::from_diesel(e, "".into())) |
| 108 | } |
| 109 | |
| 110 | pub async fn get_device_count(filters: &DeviceFilters) -> Result<i64, Error> { |
| 111 | let mut q = relay_device::dsl::relay_device |