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.
69 lines
1.8 KiB
69 lines
1.8 KiB
4 years ago
|
use actix_web::{web, HttpRequest, Responder};
|
||
|
use crate::TERA;
|
||
|
use crate::tera_ext::TeraExt;
|
||
|
use yopa::Storage;
|
||
|
use serde::Serialize;
|
||
|
use yopa::model::{PropertyModel, RelationModel};
|
||
|
|
||
|
#[derive(Serialize, Debug)]
|
||
|
struct ObjectModelDisplay<'a> {
|
||
|
id : yopa::ID,
|
||
|
name : &'a str,
|
||
|
properties: Vec<&'a PropertyModel>,
|
||
|
relations: Vec<RelationModelDisplay<'a>>,
|
||
|
}
|
||
|
|
||
|
#[derive(Serialize, Debug)]
|
||
|
struct RelationModelDisplay<'a> {
|
||
|
model : &'a RelationModel,
|
||
|
related_name : &'a str,
|
||
|
properties: Vec<&'a PropertyModel>,
|
||
|
}
|
||
|
|
||
|
#[get("/")]
|
||
|
pub(crate) async fn index(req: HttpRequest, 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 models = vec![];
|
||
|
for om in models_iter {
|
||
|
let mut oprops = model_props.remove(&om.id).unwrap_or_default();
|
||
|
let mut relations = model_relations.remove(&om.id).unwrap_or_default();
|
||
|
|
||
|
let rel_displays = relations.into_iter().map(|rm| {
|
||
|
let mut rprops = model_props.remove(&rm.id).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<_>>();
|
||
|
|
||
|
oprops.sort_by_key(|m| &m.name);
|
||
|
|
||
|
models.push(ObjectModelDisplay {
|
||
|
id: om.id,
|
||
|
name: &om.name,
|
||
|
properties: oprops,
|
||
|
relations: rel_displays,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
models.sort_by_key(|m| m.name);
|
||
|
|
||
|
let mut ctx = tera::Context::new();
|
||
|
ctx.insert("models", &models);
|
||
|
|
||
|
TERA.build_response("index", &ctx)
|
||
|
}
|