Add deletion to API

It is not yet exposed to the frontend.
This commit is contained in:
Simon Bruder 2024-07-07 14:04:31 +02:00
parent 775bc6ba9e
commit 7b47d89666
Signed by: simon
GPG key ID: 347FF8699CDA0776
4 changed files with 48 additions and 3 deletions

View file

@ -2,7 +2,7 @@
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
use actix_web::{error, get, post, put, web, HttpResponse, Responder}; use actix_web::{delete, error, get, post, put, web, HttpResponse, Responder};
use uuid::Uuid; use uuid::Uuid;
use crate::manage; use crate::manage;
@ -14,6 +14,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.service(list) .service(list)
.service(show) .service(show)
.service(update) .service(update)
.service(delete)
.service(parents); .service(parents);
} }
@ -72,6 +73,20 @@ async fn update(
Ok(HttpResponse::Ok().json(item)) Ok(HttpResponse::Ok().json(item))
} }
#[delete("/item/{id}")]
async fn delete(
pool: web::Data<DbPool>,
path: web::Path<Uuid>,
) -> actix_web::Result<impl Responder> {
let id = path.into_inner();
web::block(move || manage::item::delete(&mut pool.get().unwrap(), id))
.await?
.map_err(error::ErrorInternalServerError)?;
Ok(HttpResponse::Ok())
}
#[get("/item/{id}/parents")] #[get("/item/{id}/parents")]
async fn parents( async fn parents(
pool: web::Data<DbPool>, pool: web::Data<DbPool>,

View file

@ -2,7 +2,7 @@
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
use actix_web::{error, get, post, put, web, HttpResponse, Responder}; use actix_web::{delete, error, get, post, put, web, HttpResponse, Responder};
use uuid::Uuid; use uuid::Uuid;
use crate::manage; use crate::manage;
@ -14,7 +14,8 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.service(list) .service(list)
.service(show) .service(show)
.service(items) .service(items)
.service(update); .service(update)
.service(delete);
} }
#[put("/item-class")] #[put("/item-class")]
@ -81,3 +82,17 @@ async fn update(
Ok(HttpResponse::Ok().json(item_class)) Ok(HttpResponse::Ok().json(item_class))
} }
#[delete("/item-class/{id}")]
async fn delete(
pool: web::Data<DbPool>,
path: web::Path<Uuid>,
) -> actix_web::Result<impl Responder> {
let id = path.into_inner();
web::block(move || manage::item_class::delete(&mut pool.get().unwrap(), id))
.await?
.map_err(error::ErrorInternalServerError)?;
Ok(HttpResponse::Ok())
}

View file

@ -39,6 +39,13 @@ pub fn update(
.get_result(conn) .get_result(conn)
} }
pub fn delete(conn: &mut PgConnection, id: Uuid) -> Result<(), diesel::result::Error> {
let num_deleted =
diesel::delete(schema::items::table.filter(schema::items::id.eq(id))).execute(conn)?;
assert_eq!(num_deleted, 1);
Ok(())
}
/// Helper type for querying parents of items /// Helper type for querying parents of items
#[derive(Debug, Queryable, Selectable)] #[derive(Debug, Queryable, Selectable)]
#[diesel(table_name = schema::item_tree)] #[diesel(table_name = schema::item_tree)]

View file

@ -59,3 +59,11 @@ pub fn items(conn: &mut PgConnection, id: Uuid) -> Result<Vec<Item>, diesel::res
.select(Item::as_select()) .select(Item::as_select())
.load(conn) .load(conn)
} }
pub fn delete(conn: &mut PgConnection, id: Uuid) -> Result<(), diesel::result::Error> {
let num_deleted =
diesel::delete(schema::item_classes::table.filter(schema::item_classes::id.eq(id)))
.execute(conn)?;
assert_eq!(num_deleted, 1);
Ok(())
}