feat: implemented the /backup/preset/{id} [DELETE] endpoint

This commit is contained in:
antifallobst 2023-11-11 22:01:26 +01:00
parent 0727faae09
commit 3f12b92e9e
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
5 changed files with 55 additions and 2 deletions

View File

@ -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<State>,
auth: BearerAuth,
path: web::Path<String>,
) -> 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()
}
}
}

View File

@ -46,3 +46,9 @@ pub enum BackupPresetIdGetResponse {
Success(BackupPreset),
NotFound,
}
#[derive(Debug, Serialize)]
pub enum BackupPresetIdDeleteResponse {
Success,
NotFound,
}

View File

@ -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<BackupPresetIdDeleteResponse> {
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)
}
}
}

View File

@ -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(),

View File

@ -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<Vec<Preset>> {