| 323 | } |
| 324 | |
| 325 | pub async fn list( |
| 326 | limit: i64, |
| 327 | offset: i64, |
| 328 | filters: &Filters, |
| 329 | order_by: OrderBy, |
| 330 | order_by_desc: bool, |
| 331 | ) -> Result<Vec<GatewayListItem>, Error> { |
| 332 | let mut q = gateway::dsl::gateway |
| 333 | .left_join(multicast_group_gateway::table) |
| 334 | .select(( |
| 335 | gateway::tenant_id, |
| 336 | gateway::gateway_id, |
| 337 | gateway::name, |
| 338 | gateway::description, |
| 339 | gateway::created_at, |
| 340 | gateway::updated_at, |
| 341 | gateway::last_seen_at, |
| 342 | gateway::latitude, |
| 343 | gateway::longitude, |
| 344 | gateway::altitude, |
| 345 | gateway::properties, |
| 346 | gateway::stats_interval_secs, |
| 347 | gateway::downlink_priority, |
| 348 | )) |
| 349 | .distinct() |
| 350 | .into_boxed(); |
| 351 | |
| 352 | if let Some(tenant_id) = &filters.tenant_id { |
| 353 | q = q.filter(gateway::dsl::tenant_id.eq(fields::Uuid::from(tenant_id))); |
| 354 | } |
| 355 | |
| 356 | if let Some(search) = &filters.search { |
| 357 | #[cfg(feature = "postgres")] |
| 358 | { |
| 359 | q = q.filter(gateway::dsl::name.ilike(format!("%{}%", search))); |
| 360 | } |
| 361 | #[cfg(feature = "sqlite")] |
| 362 | { |
| 363 | q = q.filter(gateway::dsl::name.like(format!("%{}%", search))); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if let Some(multicast_group_id) = &filters.multicast_group_id { |
| 368 | q = q.filter( |
| 369 | multicast_group_gateway::dsl::multicast_group_id |
| 370 | .eq(fields::Uuid::from(multicast_group_id)), |
| 371 | ); |
| 372 | } |
| 373 | |
| 374 | q = match order_by_desc { |
| 375 | true => match order_by { |
| 376 | OrderBy::Name => q.order_by(gateway::dsl::name.desc()), |
| 377 | OrderBy::GatewayId => q.order_by(gateway::dsl::gateway_id.desc()), |
| 378 | OrderBy::LastSeenAt => { |
| 379 | #[cfg(feature = "postgres")] |
| 380 | { |
| 381 | q.order_by(gateway::dsl::last_seen_at.desc().nulls_last()) |
| 382 | .then_order_by(gateway::dsl::name) |