implement model deleting routes

master
Ondřej Hruška 4 years ago
parent 9706cf0a62
commit 35272674f6
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 1
      yopa-web/resources/templates/_macros.html.tera
  2. 18
      yopa-web/resources/templates/index.html.tera
  3. 5
      yopa-web/src/main.rs
  4. 78
      yopa-web/src/routes.rs

@ -5,4 +5,5 @@
{%- endif -%}
{%- if prop.optional %}, OPTIONAL{% endif %}
{%- if prop.multiple %}, MULTIPLE{% endif %}
<a href="/model/property/delete/{{prop.id}}">Delete property</a>
{% endmacro input %}

@ -17,11 +17,17 @@
<h2>Defined models:</h2>
{% if models %}
<ul>
{% for model in models %}
<li>
<b title="{{model.id}}">{{model.name}}</b><br>
<a href="/model/object/delete/{{model.id}}">Delete model</a> &middot;
<a href="/model/relation/create/{{model.id}}">New relation</a> &middot;
<a href="/model/property/create/{{model.id}}">New property</a>
<br>
{% if model.properties %}
Properties:
<ul>
@ -33,7 +39,6 @@
</ul>
{% endif %}
<a href="/model/property/create/{{model.id}}">Add a property</a><br>
{% if model.relations %}
Relations:
@ -45,6 +50,10 @@
{%- if rel.model.multiple %}, MULTIPLE{% endif %}
<br>
<a href="/model/relation/delete/{{rel.model.id}}">Delete relation</a> &middot;
<a href="/model/property/create/{{rel.model.id}}">New property</a>
<br>
{% if rel.properties %}
Properties:
<ul>
@ -55,16 +64,15 @@
{% endfor %}
</ul>
{% endif %}
<a href="/model/property/create/{{rel.model.id}}">Add a property</a>
</li>
{% endfor %}
</ul>
{% endif %}
<a href="/model/relation/create/{{model.id}}">Add a relation</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>No models defined.</p>
{% endif %}
{%- endblock %}

@ -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

@ -23,6 +23,12 @@ struct RelationModelDisplay<'a> {
properties: Vec<&'a PropertyModel>,
}
fn redirect(path : impl AsRef<str>) -> actix_web::Result<impl Responder> {
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<impl Responder> {
@ -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<String>,
store : crate::YopaStoreWrapper,
session : Session
) -> actix_web::Result<impl Responder> {
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<String>,
store : crate::YopaStoreWrapper,
session : Session
) -> actix_web::Result<impl Responder> {
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<String>,
store : crate::YopaStoreWrapper,
session : Session
) -> actix_web::Result<impl Responder> {
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?
}
}
}

Loading…
Cancel
Save