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.rs

97 lines
3.1 KiB

use std::fmt::{Debug, Display};
use std::ops::{DerefMut, Deref};
4 years ago
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};
4 years ago
use yopa::{DataType, ID, Storage, StorageError, TypedValue};
use yopa::model::{ObjectModel, PropertyModel, RelationModel};
4 years ago
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;
4 years ago
pub(crate) mod object_model;
pub(crate) mod relation_model;
pub(crate) mod property_model;
4 years ago
pub(crate) mod object;
#[get("/")]
pub(crate) async fn index(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();
4 years ago
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();
4 years ago
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,
}
4 years ago
}).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);
4 years ago
RelationModelDisplay {
model: rm,
related_name: rg.get_model_name(rm.object),
properties: rprops,
4 years ago
}
}).collect::<Vec<_>>();
4 years ago
reciprocal_relations.sort_by_key(|d| &d.model.reciprocal_name);
4 years ago
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,
4 years ago
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<impl Responder> {
let rg = store.read().await;
let encoded = serde_json::to_string_pretty(rg.deref()).unwrap();
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(encoded))
}