add rollup/vue for frontend assets, add wip new object form (needs success/error handling)
This commit is contained in:
@@ -131,6 +131,7 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
/* Routes */
|
||||
.service(routes::index)
|
||||
.service(routes::takeout)
|
||||
//
|
||||
.service(routes::object_model::create_form)
|
||||
.service(routes::object_model::create)
|
||||
@@ -149,6 +150,10 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(routes::property_model::update_form)
|
||||
.service(routes::property_model::update)
|
||||
.service(routes::property_model::delete)
|
||||
|
||||
.service(routes::object::create_form)
|
||||
.service(routes::object::create)
|
||||
|
||||
.service(static_files)
|
||||
.default_service(web::to(|| HttpResponse::NotFound().body("File or endpoint not found")))
|
||||
})
|
||||
|
||||
+15
-2
@@ -1,5 +1,5 @@
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::ops::DerefMut;
|
||||
use std::ops::{DerefMut, Deref};
|
||||
use std::str::FromStr;
|
||||
|
||||
use actix_session::Session;
|
||||
@@ -20,6 +20,8 @@ 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<impl Responder> {
|
||||
let rg = store.read().await;
|
||||
@@ -79,5 +81,16 @@ pub(crate) async fn index(session: Session, store: crate::YopaStoreWrapper) -> a
|
||||
ctx.insert("models", &models);
|
||||
session.render_flash(&mut ctx);
|
||||
|
||||
TERA.build_response("index", &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))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
use actix_session::Session;
|
||||
use actix_web::{Responder, web, HttpResponse};
|
||||
use crate::session_ext::SessionExt;
|
||||
use crate::routes::object_model::ObjectModelForm;
|
||||
use crate::TERA;
|
||||
use crate::tera_ext::TeraExt;
|
||||
use yopa::{ID, model};
|
||||
use yopa::data::Object;
|
||||
use serde::{Serialize,Deserialize};
|
||||
use yopa::insert::InsertObj;
|
||||
use crate::utils::redirect;
|
||||
use actix_web::web::Json;
|
||||
use serde_json::Value;
|
||||
|
||||
// we only need references here, Context serializes everything to Value.
|
||||
// cloning would be a waste of cycles
|
||||
|
||||
#[derive(Debug, Default, Serialize, Clone)]
|
||||
pub struct Schema<'a> {
|
||||
pub obj_models: Vec<&'a model::ObjectModel>,
|
||||
pub rel_models: Vec<&'a model::RelationModel>,
|
||||
pub prop_models: Vec<&'a model::PropertyModel>,
|
||||
}
|
||||
|
||||
#[derive(Serialize,Debug,Clone)]
|
||||
pub struct ObjectCreateData<'a> {
|
||||
pub model_id: ID,
|
||||
pub schema: Schema<'a>,
|
||||
pub objects: Vec<&'a Object>,
|
||||
}
|
||||
|
||||
#[get("/object/create/{model_id}")]
|
||||
pub(crate) async fn create_form(
|
||||
model_id: web::Path<ID>,
|
||||
store: crate::YopaStoreWrapper,
|
||||
session: Session
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
let mut context = tera::Context::new();
|
||||
session.render_flash(&mut context);
|
||||
|
||||
let rg = store.read().await;
|
||||
|
||||
let model = rg.get_object_model(*model_id)
|
||||
.ok_or_else(|| actix_web::error::ErrorNotFound("No such model"))?;
|
||||
|
||||
context.insert("model", model);
|
||||
|
||||
let relations : Vec<_> = rg.get_relation_models_for_object(model.id).collect();
|
||||
|
||||
let mut prop_object_ids : Vec<ID> = relations.iter().map(|r| r.id).collect();
|
||||
prop_object_ids.push(model.id);
|
||||
|
||||
prop_object_ids.sort();
|
||||
prop_object_ids.dedup();
|
||||
|
||||
let mut related_ids : Vec<_> = relations.iter().map(|r| r.related).collect();
|
||||
|
||||
related_ids.sort();
|
||||
related_ids.dedup();
|
||||
|
||||
let form_data = ObjectCreateData {
|
||||
model_id: model.id,
|
||||
schema: Schema {
|
||||
obj_models: rg.get_object_models().collect(), // TODO get only the ones that matter here
|
||||
rel_models: relations,
|
||||
prop_models: rg.get_property_models_for_parents(&prop_object_ids).collect()
|
||||
},
|
||||
objects: rg.get_objects_of_type(&related_ids).collect()
|
||||
};
|
||||
|
||||
context.insert("form_data", &form_data);
|
||||
|
||||
TERA.build_response("objects/object_create", &context)
|
||||
}
|
||||
|
||||
#[post("/object/create")]
|
||||
pub(crate) async fn create(
|
||||
form: web::Json<InsertObj>,
|
||||
store: crate::YopaStoreWrapper,
|
||||
session: Session,
|
||||
) -> actix_web::Result<impl Responder> {
|
||||
warn!("{:?}", form);
|
||||
|
||||
// let des : InsertObj = serde_json::from_value(form.into_inner()).unwrap();
|
||||
//
|
||||
// Ok(HttpResponse::Ok().finish())
|
||||
//
|
||||
let mut wg = store.write().await;
|
||||
let form = form.into_inner();
|
||||
let name = form.name.clone();
|
||||
match wg.insert_object(form) {
|
||||
Ok(_id) => {
|
||||
debug!("Object created, redirecting to root");
|
||||
session.flash_success(format!("Object \"{}\" created.", name));
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Error creating model: {}", e);
|
||||
Ok(HttpResponse::BadRequest().body(e.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ pub(crate) async fn create_form(session: Session) -> actix_web::Result<impl Resp
|
||||
context.insert("old", &ObjectModelForm::default());
|
||||
}
|
||||
|
||||
TERA.build_response("model_create", &context)
|
||||
TERA.build_response("models/model_create", &context)
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -88,7 +88,7 @@ pub(crate) async fn update_form(
|
||||
context.insert("model", model);
|
||||
}
|
||||
|
||||
TERA.build_response("model_update", &context)
|
||||
TERA.build_response("models/model_update", &context)
|
||||
}
|
||||
|
||||
#[post("/model/object/update/{model_id}")]
|
||||
|
||||
@@ -57,7 +57,7 @@ pub(crate) async fn create_form(
|
||||
};
|
||||
|
||||
context.insert("object", &object);
|
||||
TERA.build_response("property_create", &context)
|
||||
TERA.build_response("models/property_create", &context)
|
||||
}
|
||||
|
||||
#[derive(Serialize,Deserialize)]
|
||||
@@ -220,7 +220,7 @@ pub(crate) async fn update_form(
|
||||
context.insert("model", model);
|
||||
}
|
||||
|
||||
TERA.build_response("property_update", &context)
|
||||
TERA.build_response("models/property_update", &context)
|
||||
}
|
||||
|
||||
#[post("/model/property/update/{model_id}")]
|
||||
|
||||
@@ -55,7 +55,7 @@ pub(crate) async fn create_form(
|
||||
|
||||
|
||||
|
||||
TERA.build_response("relation_create", &context)
|
||||
TERA.build_response("models/relation_create", &context)
|
||||
}
|
||||
|
||||
#[derive(Serialize,Deserialize)]
|
||||
@@ -162,7 +162,7 @@ pub(crate) async fn update_form(
|
||||
context.insert("model", model);
|
||||
}
|
||||
|
||||
TERA.build_response("relation_update", &context)
|
||||
TERA.build_response("models/relation_update", &context)
|
||||
}
|
||||
|
||||
#[post("/model/relation/update/{model_id}")]
|
||||
|
||||
Reference in New Issue
Block a user