use std::fmt::{Debug, Display}; use std::ops::{DerefMut, Deref}; use std::str::FromStr; use actix_session::Session; use actix_web::{HttpRequest, HttpResponse, Responder, web}; use actix_web::http::header::IntoHeaderValue; use serde::{Deserialize, Serialize}; use yopa::{DataType, ID, Storage, StorageError, TypedValue}; use yopa::model::{ObjectModel, PropertyModel, RelationModel}; use crate::session_ext::SessionExt; use crate::TERA; use crate::tera_ext::TeraExt; use crate::routes::relation_model::RelationModelDisplay; use crate::routes::object_model::ObjectModelDisplay; pub(crate) mod object_model; pub(crate) mod relation_model; pub(crate) mod property_model; pub(crate) mod object; #[get("/")] pub(crate) async fn index(session: Session, store: crate::YopaStoreWrapper) -> actix_web::Result { 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::>(); 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::>(); 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/schema", &ctx) } #[get("/takeout")] pub(crate) async fn takeout(store: crate::YopaStoreWrapper) -> actix_web::Result { let rg = store.read().await; let encoded = serde_json::to_string_pretty(rg.deref()).unwrap(); Ok(HttpResponse::Ok() .content_type("application/json") .body(encoded)) }