diff --git a/src/api/calls.rs b/src/api/calls.rs index 13f81f3..bf0dab9 100644 --- a/src/api/calls.rs +++ b/src/api/calls.rs @@ -1,5 +1,5 @@ use crate::api::{data::*, handlers, State}; -use actix_web::{get, post, web, HttpResponse, Responder}; +use actix_web::{delete, get, post, web, HttpResponse, Responder}; use actix_web_httpauth::extractors::bearer::BearerAuth; use log::error; @@ -78,3 +78,26 @@ async fn backup_preset_id_get( } } } + +#[delete("/backup/preset/{id}")] +async fn backup_preset_id_delete( + data: web::Data, + auth: BearerAuth, + path: web::Path, +) -> impl Responder { + if auth.token() != data.token { + return HttpResponse::Unauthorized().finish(); + } + + let id = &path.into_inner(); + match handlers::backup_preset_id_delete(&data.pool, id).await { + Ok(resp) => match resp { + BackupPresetIdDeleteResponse::Success => HttpResponse::Ok().finish(), + BackupPresetIdDeleteResponse::NotFound => HttpResponse::NotFound().finish(), + }, + Err(e) => { + error!("While handling /backup/preset/{id} [DELETE] request: {e}",); + HttpResponse::InternalServerError().finish() + } + } +} diff --git a/src/api/data.rs b/src/api/data.rs index d5cbc99..cb87cb9 100644 --- a/src/api/data.rs +++ b/src/api/data.rs @@ -46,3 +46,9 @@ pub enum BackupPresetIdGetResponse { Success(BackupPreset), NotFound, } + +#[derive(Debug, Serialize)] +pub enum BackupPresetIdDeleteResponse { + Success, + NotFound, +} diff --git a/src/api/handlers.rs b/src/api/handlers.rs index 8ee2671..c58d80a 100644 --- a/src/api/handlers.rs +++ b/src/api/handlers.rs @@ -47,8 +47,24 @@ pub async fn backup_preset_id_get( match backup::preset::Preset::load(pool, id).await? { Some(preset) => Ok(BackupPresetIdGetResponse::Success(preset.into())), None => { - warn!("Failed to fetch Backup Preset '{id}'"); + warn!("Failed to fetch Backup Preset '{id}' -> Preset not found!"); Ok(BackupPresetIdGetResponse::NotFound) } } } + +pub async fn backup_preset_id_delete( + pool: &SqlitePool, + id: &str, +) -> Result { + match backup::preset::Preset::load(pool, id).await? { + Some(preset) => { + preset.delete(pool).await?; + Ok(BackupPresetIdDeleteResponse::Success) + } + None => { + warn!("Failed to delete Backup Preset '{id}' -> Preset not found!"); + Ok(BackupPresetIdDeleteResponse::NotFound) + } + } +} diff --git a/src/api/mod.rs b/src/api/mod.rs index 7b43d3a..c61b95b 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -18,6 +18,7 @@ pub async fn start(port: u16, pool: SqlitePool, token: String) -> Result<()> { .service(calls::backup_preset_post) .service(calls::backup_preset_get) .service(calls::backup_preset_id_get) + .service(calls::backup_preset_id_delete) .app_data(web::Data::new(State { pool: pool.clone(), token: token.to_owned(), diff --git a/src/backend/backup/preset.rs b/src/backend/backup/preset.rs index 7a14b99..f6fd574 100644 --- a/src/backend/backup/preset.rs +++ b/src/backend/backup/preset.rs @@ -86,6 +86,13 @@ impl Preset { Err(e) => Err(Error::new(e)), } } + + pub async fn delete(&self, pool: &SqlitePool) -> Result<()> { + sqlx::query!(r#"DELETE FROM Presets WHERE id = $1;"#, self.id) + .execute(pool) + .await?; + Ok(()) + } } pub async fn get_all_presets(pool: &SqlitePool) -> Result> {