diff --git a/crates/oswilno-parking-space/src/lib.rs b/crates/oswilno-parking-space/src/lib.rs index d2531a0..443dd03 100644 --- a/crates/oswilno-parking-space/src/lib.rs +++ b/crates/oswilno-parking-space/src/lib.rs @@ -14,6 +14,7 @@ use oswilno_view::{ use sea_orm::prelude::*; use sea_orm::ActiveValue::{NotSet, Set}; use std::collections::BTreeMap; +use std::rc::Rc; use std::sync::Arc; pub async fn init(db: Arc) { @@ -52,10 +53,12 @@ async fn root() -> HttpResponse { #[template(path = "../templates/parking-spaces/all-partial.html")] struct AllPartialParkingSpace { parking_space_rents: Vec, - parking_space_by_id: BTreeMap, + parking_space_by_id: BTreeMap>, + parking_spaces: Vec>, account_by_id: BTreeMap, #[allow(dead_code)] - locations: Vec, + locations: Vec>, + location_by_id: BTreeMap>, session: Option, } @@ -70,7 +73,7 @@ async fn all_parking_spaces( let session = session.into_option(); let account_id = session.as_ref().map(|s| s.account_id()); let session = session.map(Into::into); - + eprintln!("######################################################################"); tracing::debug!("session {session:?}"); eprintln!("session {session:?}"); @@ -106,26 +109,36 @@ async fn search_parking_spaces() -> HttpResponse { HttpResponse::Ok().body("") } -async fn load_parking_spaces(db: Arc, account_id: Option) -> AllPartialParkingSpace { +async fn load_parking_spaces( + db: Arc, + account_id: Option, +) -> AllPartialParkingSpace { let rents = parking_space_rents::Entity::find().all(&*db).await.unwrap(); - let (parking_space_by_id, account_ids) = { - let ids = rents - .iter() - .map(|r| r.parking_space_id) - .collect::>(); + let ids = rents + .iter() + .map(|r| r.parking_space_id) + .collect::>(); - let v = parking_spaces::Entity::find() - .filter(parking_spaces::Column::Id.is_in(ids).or(parking_spaces::Column::AccountId.eq(account_id))) - .all(&*db) - .await - .unwrap(); - - let len = v.len(); - v.into_iter().fold( + let parking_spaces: Vec<_> = parking_spaces::Entity::find() + .filter( + parking_spaces::Column::Id + .is_in(ids) + .or(parking_spaces::Column::AccountId.eq(account_id)), + ) + .all(&*db) + .await + .unwrap() + .into_iter() + .map(Rc::new) + .collect(); + + let (parking_space_by_id, account_ids) = { + let len = parking_spaces.len(); + parking_spaces.iter().fold( (BTreeMap::new(), Vec::with_capacity(len)), |(mut by_id, mut ids), space| { ids.push(space.account_id); - by_id.insert(space.id, space); + by_id.insert(space.id, space.clone()); (by_id, ids) }, ) @@ -144,13 +157,19 @@ async fn load_parking_spaces(db: Arc, account_id: Option = load_locations(db).await.into_iter().map(Rc::new).collect(); + let location_by_id = locations.iter().fold(BTreeMap::new(), |mut memo, location| { + memo.insert(location.id, location.clone()); +memo + }); AllPartialParkingSpace { account_by_id, parking_space_rents: rents, + parking_spaces, parking_space_by_id, locations, + location_by_id, session: None, } } diff --git a/crates/oswilno-parking-space/templates/parking-spaces/all-partial.html b/crates/oswilno-parking-space/templates/parking-spaces/all-partial.html index 581c97e..9302a3c 100644 --- a/crates/oswilno-parking-space/templates/parking-spaces/all-partial.html +++ b/crates/oswilno-parking-space/templates/parking-spaces/all-partial.html @@ -8,8 +8,7 @@
- {% match session %} - {% when Some with (session) %} + {% if let Some(session) = session %} - {% when None %} - {% endmatch %} + {% endif %}
- {% for (_, parking_space) in parking_space_by_id %} + {% for parking_space in parking_spaces -%} - -
{{ account.login }}
-
+ {% if let Some(location_id) = parking_space.location_id -%} + {% if let Some(location) = location_by_id.get(location_id) -%} + {% if let Some(spot) = parking_space.spot -%} + {% if let Some(account) = account_by_id.get(parking_space.account_id) -%} + {{spot}} + {{location.name}} + {{location.number}} + {{location.stage}} + {% endif %} + {% endif %} + {% endif %} + {% endif %}
{% endfor %}