diff --git a/Cargo.lock b/Cargo.lock index 8892365..f5e08ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2587,6 +2587,7 @@ dependencies = [ "serde", "serde_json", "tokio 1.30.0", + "tracing", ] [[package]] diff --git a/crates/oswilno-parking-space/Cargo.toml b/crates/oswilno-parking-space/Cargo.toml index 9bc2dfe..0e6d42a 100644 --- a/crates/oswilno-parking-space/Cargo.toml +++ b/crates/oswilno-parking-space/Cargo.toml @@ -17,3 +17,4 @@ sea-orm = { version = "0.11.1", features = ["runtime-actix-rustls", "sqlx", "sql serde = { version = "1.0.176", features = ["derive"] } serde_json = "1.0.104" tokio = { version = "1.29.1", features = ["full"] } +tracing = "0.1.37" diff --git a/crates/oswilno-parking-space/src/lib.rs b/crates/oswilno-parking-space/src/lib.rs index cc649e9..d2531a0 100644 --- a/crates/oswilno-parking-space/src/lib.rs +++ b/crates/oswilno-parking-space/src/lib.rs @@ -13,7 +13,7 @@ use oswilno_view::{ }; use sea_orm::prelude::*; use sea_orm::ActiveValue::{NotSet, Set}; -use std::collections::HashMap; +use std::collections::BTreeMap; use std::sync::Arc; pub async fn init(db: Arc) { @@ -52,8 +52,8 @@ async fn root() -> HttpResponse { #[template(path = "../templates/parking-spaces/all-partial.html")] struct AllPartialParkingSpace { parking_space_rents: Vec, - parking_space_by_id: HashMap, - account_by_id: HashMap, + parking_space_by_id: BTreeMap, + account_by_id: BTreeMap, #[allow(dead_code)] locations: Vec, session: Option, @@ -67,8 +67,15 @@ async fn all_parking_spaces( session: MaybeAuthenticated, ) -> HttpResponse { let db = db.into_inner(); - let session = session.into_option().map(Into::into); - let mut parking_spaces = load_parking_spaces(db).await; + 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:?}"); + + let mut parking_spaces = load_parking_spaces(db, account_id).await; parking_spaces.session = session.clone(); let main = Main { body: parking_spaces, @@ -99,21 +106,23 @@ async fn search_parking_spaces() -> HttpResponse { HttpResponse::Ok().body("") } -async fn load_parking_spaces(db: Arc) -> 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 v = parking_spaces::Entity::find() - .filter(parking_spaces::Column::Id.is_in(ids)) + .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( - (HashMap::with_capacity(len), Vec::with_capacity(len)), + (BTreeMap::new(), Vec::with_capacity(len)), |(mut by_id, mut ids), space| { ids.push(space.account_id); by_id.insert(space.id, space); @@ -127,10 +136,9 @@ async fn load_parking_spaces(db: Arc) -> AllPartialParkingSp .all(&*db) .await .unwrap(); - let len = accounts.len(); accounts .into_iter() - .fold(HashMap::with_capacity(len), |mut agg, account| { + .fold(BTreeMap::new(), |mut agg, account| { agg.insert(account.id, account); agg }) 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 b705dfb..581c97e 100644 --- a/crates/oswilno-parking-space/templates/parking-spaces/all-partial.html +++ b/crates/oswilno-parking-space/templates/parking-spaces/all-partial.html @@ -23,6 +23,17 @@ {% when None %} {% endmatch %} + +
+ {% for (_, parking_space) in parking_space_by_id %} + + +
{{ account.login }}
+
+
+ {% endfor %} +
+ {% for parking_space_rent in parking_space_rents -%} {% if let Some(parking_space) = parking_space_by_id.get(parking_space_rent.parking_space_id) %} diff --git a/crates/web-assets/build.sh b/crates/web-assets/build.sh index 3b09fc2..227bda1 100755 --- a/crates/web-assets/build.sh +++ b/crates/web-assets/build.sh @@ -6,7 +6,7 @@ ROOT=$(git rev-parse --show-toplevel) WEB_ASSETS_ROOT=${ROOT}/crates/web-assets cd ${ROOT} -mkdir assets +mkdir -p assets cd ${WEB_ASSETS_ROOT} yarn || pnpm i