a small relational database with user-editable schema for manual data entry
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
yopa/yopa-web/src/routes/models.rs

73 lines
2.4 KiB

use actix_session::Session;
use actix_web::Responder;
use crate::routes::models::relation::RelationModelDisplay;
use crate::routes::models::object::ObjectModelDisplay;
use crate::session_ext::SessionExt;
use crate::TERA;
use crate::tera_ext::TeraExt;
pub(crate) mod object;
pub(crate) mod relation;
pub(crate) mod property;
#[get("/models")]
pub(crate) async fn list(session: Session, store: crate::YopaStoreWrapper) -> actix_web::Result<impl Responder> {
let rg = store.read().await;
let models_iter = rg.get_object_models();
// object and relation props
let mut model_props = rg.get_grouped_prop_models();
let mut model_relations = rg.get_grouped_relation_models();
let mut model_rec_relations = rg.get_grouped_reciprocal_relation_models();
let mut models = vec![];
for om in models_iter {
let mut relations = model_relations.remove(&om.id).unwrap_or_default();
let mut relations = relations.into_iter().map(|rm| {
let mut rprops = model_props.get(&rm.id).cloned().unwrap_or_default();
rprops.sort_by_key(|m| &m.name);
RelationModelDisplay {
model: rm,
related_name: rg.get_model_name(rm.related),
properties: rprops,
}
}).collect::<Vec<_>>();
relations.sort_by_key(|d| &d.model.name);
// Relations coming INTO this model
let mut reciprocal_relations = model_rec_relations.remove(&om.id).unwrap_or_default();
let mut reciprocal_relations = reciprocal_relations.into_iter().map(|rm| {
let mut rprops = model_props.get(&rm.id).cloned().unwrap_or_default();
rprops.sort_by_key(|m| &m.name);
RelationModelDisplay {
model: rm,
related_name: rg.get_model_name(rm.object),
properties: rprops,
}
}).collect::<Vec<_>>();
reciprocal_relations.sort_by_key(|d| &d.model.reciprocal_name);
let mut properties = model_props.remove(&om.id).unwrap_or_default();
properties.sort_by_key(|m| &m.name);
models.push(ObjectModelDisplay {
id: om.id,
name: &om.name,
properties,
relations,
reciprocal_relations,
})
}
models.sort_by_key(|m| m.name);
let mut ctx = tera::Context::new();
ctx.insert("models", &models);
session.render_flash(&mut ctx);
TERA.build_response("models/index", &ctx)
}