render location

This commit is contained in:
Adrian Woźniak 2023-09-05 16:09:26 +02:00
parent cacd56b31b
commit a7a02a227e
2 changed files with 53 additions and 27 deletions

View File

@ -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<sea_orm::DatabaseConnection>) {
@ -52,10 +53,12 @@ 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: BTreeMap<i32, parking_spaces::Model>,
parking_space_by_id: BTreeMap<i32, Rc<parking_spaces::Model>>,
parking_spaces: Vec<Rc<parking_spaces::Model>>,
account_by_id: BTreeMap<i32, accounts::Model>,
#[allow(dead_code)]
locations: Vec<parking_space_locations::Model>,
locations: Vec<Rc<parking_space_locations::Model>>,
location_by_id: BTreeMap<i32, Rc<parking_space_locations::Model>>,
session: Option<SessionOpts>,
}
@ -106,26 +109,36 @@ async fn search_parking_spaces() -> HttpResponse {
HttpResponse::Ok().body("")
}
async fn load_parking_spaces(db: Arc<DatabaseConnection>, account_id: Option<i32>) -> 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 ids = rents
.iter()
.map(|r| r.parking_space_id)
.collect::<Vec<i32>>();
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 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).or(parking_spaces::Column::AccountId.eq(account_id)))
.all(&*db)
.await
.unwrap();
let len = v.len();
v.into_iter().fold(
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<DatabaseConnection>, account_id: Option<i32
})
};
let locations = load_locations(db).await;
let locations: Vec<_> = 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,
}
}

View File

@ -8,8 +8,7 @@
</div>
<div>
</div>
{% match session %}
{% when Some with (session) %}
{% if let Some(session) = session %}
<div>
<a
href="/parking-spaces/form"
@ -20,16 +19,24 @@
<i class="fas fa-plus"></i>
</a>
</div>
{% when None %}
{% endmatch %}
{% endif %}
</section>
<section id="own-parking-spaces">
{% for (_, parking_space) in parking_space_by_id %}
{% for parking_space in parking_spaces -%}
<oswilno-parking-space id="parking-space-{{ parking_space.id }}">
<oswilno-account id="account-{{ account.id }}">
<div>{{ account.login }}</div>
</oswilno-account>
{% 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) -%}
<span>{{spot}}</span>
<span>{{location.name}}</span>
<span>{{location.number}}</span>
<span>{{location.stage}}</span>
{% endif %}
{% endif %}
{% endif %}
{% endif %}
</oswilno-parking-space>
{% endfor %}
</section>