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 -%} {%- endif -%}
{%- if prop.optional %}, OPTIONAL{% endif %} {%- if prop.optional %}, OPTIONAL{% endif %}
{%- if prop.multiple %}, MULTIPLE{% endif %} {%- if prop.multiple %}, MULTIPLE{% endif %}
<a href="/model/property/delete/{{prop.id}}">Delete property</a>
{% endmacro input %} {% endmacro input %}

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

@ -97,8 +97,11 @@ async fn main() -> std::io::Result<()> {
.service(routes::index) .service(routes::index)
.service(routes::object_model_create_form) .service(routes::object_model_create_form)
.service(routes::object_model_create) .service(routes::object_model_create)
.service(routes::object_model_delete)
.service(routes::relation_model_delete)
.service(routes::property_model_delete)
.service(static_files) .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")? .bind("127.0.0.1:8080")?
.run().await .run().await

@ -23,6 +23,12 @@ struct RelationModelDisplay<'a> {
properties: Vec<&'a PropertyModel>, 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("/")] #[get("/")]
pub(crate) async fn index(session : Session, store : crate::YopaStoreWrapper) -> actix_web::Result<impl Responder> { 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) => { Ok(_id) => {
debug!("Object created, redirecting to root"); debug!("Object created, redirecting to root");
session.flash_success(format!("Object model \"{}\" created.", form.name)); 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() #[get("/model/object/delete/{id}")]
.header("location", "/") pub(crate) async fn object_model_delete(
.finish()) 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) => { Err(e) => {
warn!("Error creating model: {}", e); warn!("Error deleting object model: {:?}", e);
session.flash_error(e.to_string()); session.flash_error(e.to_string());
redirect("/") // back?
}
}
}
// Redirect back #[get("/model/relation/delete/{id}")]
Ok(HttpResponse::SeeOther() pub(crate) async fn relation_model_delete(
.header("location", "/model/object/create") id : web::Path<String>,
.finish()) 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