Expose deletion in frontend

This commit is contained in:
Simon Bruder 2024-07-13 15:05:00 +02:00
parent 629e7ae825
commit 253cd9f832
Signed by: simon
GPG key ID: 347FF8699CDA0776
2 changed files with 46 additions and 2 deletions

View file

@ -21,7 +21,8 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.service(add_item) .service(add_item)
.service(add_item_post) .service(add_item_post)
.service(edit_item) .service(edit_item)
.service(edit_item_post); .service(edit_item_post)
.service(delete_item);
} }
#[get("/item/{id}")] #[get("/item/{id}")]
@ -72,6 +73,12 @@ async fn show_item(
name: "Edit".to_string(), name: "Edit".to_string(),
colour: Colour::Warning, colour: Colour::Warning,
}), }),
(PageAction {
method: PageActionMethod::Post,
target: format!("/item/{}/delete", item.id),
name: "Delete".to_string(),
colour: Colour::Danger,
}),
], ],
user: Some(user), user: Some(user),
..Default::default() ..Default::default()
@ -355,3 +362,18 @@ async fn edit_item_post(
Ok(web::Redirect::to("/item/".to_owned() + &item.id.to_string()).see_other()) Ok(web::Redirect::to("/item/".to_owned() + &item.id.to_string()).see_other())
} }
#[post("/item/{id}/delete")]
async fn delete_item(
pool: web::Data<PgPool>,
path: web::Path<Uuid>,
_user: Identity,
) -> actix_web::Result<impl Responder> {
let id = path.into_inner();
manage::item::delete(&pool, id)
.await
.map_err(error::ErrorInternalServerError)?;
Ok(web::Redirect::to("/items").see_other())
}

View file

@ -19,7 +19,8 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.service(add_item_class) .service(add_item_class)
.service(add_item_class_post) .service(add_item_class_post)
.service(edit_item_class) .service(edit_item_class)
.service(edit_item_class_post); .service(edit_item_class_post)
.service(delete_item_class);
} }
#[get("/item-class/{id}")] #[get("/item-class/{id}")]
@ -72,6 +73,12 @@ async fn show_item_class(
name: "Edit".to_string(), name: "Edit".to_string(),
colour: Colour::Warning, 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( Ok(templates::base(
TemplateConfig { 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()) 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<PgPool>,
path: web::Path<Uuid>,
_user: Identity,
) -> actix_web::Result<impl Responder> {
let id = path.into_inner();
manage::item_class::delete(&pool, id)
.await
.map_err(error::ErrorInternalServerError)?;
Ok(web::Redirect::to("/item-classes").see_other())
}