Display own parking spaces

This commit is contained in:
Adrian Woźniak 2023-09-04 15:02:53 +02:00
parent 0b145bec5d
commit cacd56b31b
5 changed files with 32 additions and 11 deletions

1
Cargo.lock generated
View File

@ -2587,6 +2587,7 @@ dependencies = [
"serde",
"serde_json",
"tokio 1.30.0",
"tracing",
]
[[package]]

View File

@ -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"

View File

@ -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<sea_orm::DatabaseConnection>) {
@ -52,8 +52,8 @@ async fn root() -> HttpResponse {
#[template(path = "../templates/parking-spaces/all-partial.html")]
struct AllPartialParkingSpace {
parking_space_rents: Vec<parking_space_rents::Model>,
parking_space_by_id: HashMap<i32, parking_spaces::Model>,
account_by_id: HashMap<i32, accounts::Model>,
parking_space_by_id: BTreeMap<i32, parking_spaces::Model>,
account_by_id: BTreeMap<i32, accounts::Model>,
#[allow(dead_code)]
locations: Vec<parking_space_locations::Model>,
session: Option<SessionOpts>,
@ -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<DatabaseConnection>) -> AllPartialParkingSpace {
async fn load_parking_spaces(db: Arc<DatabaseConnection>, account_id: Option<i32>) -> 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::<Vec<i32>>();
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<DatabaseConnection>) -> 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
})

View File

@ -23,6 +23,17 @@
{% when None %}
{% endmatch %}
</section>
<section id="own-parking-spaces">
{% for (_, parking_space) in parking_space_by_id %}
<oswilno-parking-space id="parking-space-{{ parking_space.id }}">
<oswilno-account id="account-{{ account.id }}">
<div>{{ account.login }}</div>
</oswilno-account>
</oswilno-parking-space>
{% endfor %}
</section>
<oswilno-parking-space-rents>
{% for parking_space_rent in parking_space_rents -%}
{% if let Some(parking_space) = parking_space_by_id.get(parking_space_rent.parking_space_id) %}

View File

@ -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