From 253cd9f832306a9ed90764bc515c13aa891f369c Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Sat, 13 Jul 2024 15:05:00 +0200 Subject: [PATCH] Expose deletion in frontend --- src/frontend/item.rs | 24 +++++++++++++++++++++++- src/frontend/item_class.rs | 24 +++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/frontend/item.rs b/src/frontend/item.rs index 003230e..034631b 100644 --- a/src/frontend/item.rs +++ b/src/frontend/item.rs @@ -21,7 +21,8 @@ pub fn config(cfg: &mut web::ServiceConfig) { .service(add_item) .service(add_item_post) .service(edit_item) - .service(edit_item_post); + .service(edit_item_post) + .service(delete_item); } #[get("/item/{id}")] @@ -72,6 +73,12 @@ async fn show_item( name: "Edit".to_string(), colour: Colour::Warning, }), + (PageAction { + method: PageActionMethod::Post, + target: format!("/item/{}/delete", item.id), + name: "Delete".to_string(), + colour: Colour::Danger, + }), ], user: Some(user), ..Default::default() @@ -355,3 +362,18 @@ async fn edit_item_post( Ok(web::Redirect::to("/item/".to_owned() + &item.id.to_string()).see_other()) } + +#[post("/item/{id}/delete")] +async fn delete_item( + pool: web::Data, + path: web::Path, + _user: Identity, +) -> actix_web::Result { + let id = path.into_inner(); + + manage::item::delete(&pool, id) + .await + .map_err(error::ErrorInternalServerError)?; + + Ok(web::Redirect::to("/items").see_other()) +} diff --git a/src/frontend/item_class.rs b/src/frontend/item_class.rs index a4342ed..79e1edf 100644 --- a/src/frontend/item_class.rs +++ b/src/frontend/item_class.rs @@ -19,7 +19,8 @@ pub fn config(cfg: &mut web::ServiceConfig) { .service(add_item_class) .service(add_item_class_post) .service(edit_item_class) - .service(edit_item_class_post); + .service(edit_item_class_post) + .service(delete_item_class); } #[get("/item-class/{id}")] @@ -72,6 +73,12 @@ async fn show_item_class( name: "Edit".to_string(), colour: Colour::Warning, }); + page_actions.push(PageAction { + method: PageActionMethod::Post, + target: format!("/item-class/{}/delete", item_class.id), + name: "Delete".to_string(), + colour: Colour::Danger, + }); Ok(templates::base( TemplateConfig { @@ -309,3 +316,18 @@ async fn edit_item_class_post( Ok(web::Redirect::to("/item-class/".to_owned() + &item_class.id.to_string()).see_other()) } + +#[post("/item-class/{id}/delete")] +async fn delete_item_class( + pool: web::Data, + path: web::Path, + _user: Identity, +) -> actix_web::Result { + let id = path.into_inner(); + + manage::item_class::delete(&pool, id) + .await + .map_err(error::ErrorInternalServerError)?; + + Ok(web::Redirect::to("/item-classes").see_other()) +}