From 35272674f6953e427b9a20437c90a5ab7d535bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 7 Feb 2021 19:12:25 +0100 Subject: [PATCH] implement model deleting routes --- .../resources/templates/_macros.html.tera | 1 + yopa-web/resources/templates/index.html.tera | 18 +++-- yopa-web/src/main.rs | 5 +- yopa-web/src/routes.rs | 78 +++++++++++++++++-- 4 files changed, 88 insertions(+), 14 deletions(-) diff --git a/yopa-web/resources/templates/_macros.html.tera b/yopa-web/resources/templates/_macros.html.tera index 1860451..2ee00a7 100644 --- a/yopa-web/resources/templates/_macros.html.tera +++ b/yopa-web/resources/templates/_macros.html.tera @@ -5,4 +5,5 @@ {%- endif -%} {%- if prop.optional %}, OPTIONAL{% endif %} {%- if prop.multiple %}, MULTIPLE{% endif %} + Delete property {% endmacro input %} diff --git a/yopa-web/resources/templates/index.html.tera b/yopa-web/resources/templates/index.html.tera index 513ce8c..86b626b 100644 --- a/yopa-web/resources/templates/index.html.tera +++ b/yopa-web/resources/templates/index.html.tera @@ -17,11 +17,17 @@

Defined models:

+{% if models %} {% endif %} - - Add a relation {% endfor %} +{% else %} +

No models defined.

+{% endif %} {%- endblock %} diff --git a/yopa-web/src/main.rs b/yopa-web/src/main.rs index c83a89c..5f6f373 100644 --- a/yopa-web/src/main.rs +++ b/yopa-web/src/main.rs @@ -97,8 +97,11 @@ async fn main() -> std::io::Result<()> { .service(routes::index) .service(routes::object_model_create_form) .service(routes::object_model_create) + .service(routes::object_model_delete) + .service(routes::relation_model_delete) + .service(routes::property_model_delete) .service(static_files) - .default_service(web::to(|| HttpResponse::NotFound().body("Not found"))) + .default_service(web::to(|| HttpResponse::NotFound().body("File or endpoint not found"))) }) .bind("127.0.0.1:8080")? .run().await diff --git a/yopa-web/src/routes.rs b/yopa-web/src/routes.rs index 14571f7..c26630f 100644 --- a/yopa-web/src/routes.rs +++ b/yopa-web/src/routes.rs @@ -23,6 +23,12 @@ struct RelationModelDisplay<'a> { properties: Vec<&'a PropertyModel>, } +fn redirect(path : impl AsRef) -> actix_web::Result { + Ok(HttpResponse::SeeOther() + .header("location", path.as_ref()) // back - to where? + .finish()) +} + #[get("/")] pub(crate) async fn index(session : Session, store : crate::YopaStoreWrapper) -> actix_web::Result { @@ -99,19 +105,75 @@ pub(crate) async fn object_model_create( Ok(_id) => { debug!("Object created, redirecting to root"); session.flash_success(format!("Object model \"{}\" created.", form.name)); + redirect("/") + } + Err(e) => { + warn!("Error creating model: {:?}", e); + session.flash_error(e.to_string()); + redirect("/model/object/create") + } + } +} - Ok(HttpResponse::SeeOther() - .header("location", "/") - .finish()) +#[get("/model/object/delete/{id}")] +pub(crate) async fn object_model_delete( + id : web::Path, + store : crate::YopaStoreWrapper, + session : Session +) -> actix_web::Result { + let mut wg = store.write().await; + match wg.undefine_object(id.parse().map_err(|e| actix_web::error::ErrorBadRequest(e))?) { + Ok(om) => { + debug!("Object model deleted, redirecting to root"); + session.flash_success(format!("Object model \"{}\" deleted.", om.name)); + redirect("/") } Err(e) => { - warn!("Error creating model: {}", e); + warn!("Error deleting object model: {:?}", e); session.flash_error(e.to_string()); + redirect("/") // back? + } + } +} - // Redirect back - Ok(HttpResponse::SeeOther() - .header("location", "/model/object/create") - .finish()) +#[get("/model/relation/delete/{id}")] +pub(crate) async fn relation_model_delete( + id : web::Path, + store : crate::YopaStoreWrapper, + session : Session +) -> actix_web::Result { + let mut wg = store.write().await; + match wg.undefine_relation(id.parse().map_err(|e| actix_web::error::ErrorBadRequest(e))?) { + Ok(rm) => { + debug!("Relation deleted, redirecting to root"); + session.flash_success(format!("Relation model \"{}\" deleted.", rm.name)); + redirect("/") + } + Err(e) => { + warn!("Error deleting relation model: {:?}", e); + session.flash_error(e.to_string()); + redirect("/") // back? + } + } +} + +#[get("/model/property/delete/{id}")] +pub(crate) async fn property_model_delete( + id : web::Path, + store : crate::YopaStoreWrapper, + session : Session +) -> actix_web::Result { + let mut wg = store.write().await; + match wg.undefine_property(id.parse().map_err(|e| actix_web::error::ErrorBadRequest(e))?) { + Ok(rm) => { + debug!("Property deleted, redirecting to root"); + session.flash_success(format!("Property \"{}\" deleted.", rm.name)); + redirect("/") + } + Err(e) => { + warn!("Error deleting property: {:?}", e); + session.flash_error(e.to_string()); + redirect("/") // back? } } }