From 0727faae09926d0a8a52867b1a976d852807070d Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sat, 11 Nov 2023 21:48:44 +0100 Subject: [PATCH] feat: implemented the /backup/preset/{id} [GET] endpoint --- src/api/calls.rs | 23 +++++++++++++++++++++++ src/api/data.rs | 6 ++++++ src/api/handlers.rs | 13 +++++++++++++ src/api/mod.rs | 1 + src/main.rs | 2 +- 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/api/calls.rs b/src/api/calls.rs index 34a1c31..13f81f3 100644 --- a/src/api/calls.rs +++ b/src/api/calls.rs @@ -55,3 +55,26 @@ async fn backup_preset_get(data: web::Data, auth: BearerAuth) -> impl Res } } } + +#[get("/backup/preset/{id}")] +async fn backup_preset_id_get( + 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_get(&data.pool, id).await { + Ok(resp) => match resp { + BackupPresetIdGetResponse::Success(b) => HttpResponse::Ok().json(&b), + BackupPresetIdGetResponse::NotFound => HttpResponse::NotFound().finish(), + }, + Err(e) => { + error!("While handling /backup/preset/{id} [GET] request: {e}",); + HttpResponse::InternalServerError().finish() + } + } +} diff --git a/src/api/data.rs b/src/api/data.rs index 48012e6..d5cbc99 100644 --- a/src/api/data.rs +++ b/src/api/data.rs @@ -40,3 +40,9 @@ pub enum BackupPresetPostResponse { pub struct BackupPresetGetResponse { pub presets: Vec, } + +#[derive(Debug, Serialize)] +pub enum BackupPresetIdGetResponse { + Success(BackupPreset), + NotFound, +} diff --git a/src/api/handlers.rs b/src/api/handlers.rs index 926d78e..8ee2671 100644 --- a/src/api/handlers.rs +++ b/src/api/handlers.rs @@ -39,3 +39,16 @@ pub async fn backup_preset_get(pool: &SqlitePool) -> Result Result { + match backup::preset::Preset::load(pool, id).await? { + Some(preset) => Ok(BackupPresetIdGetResponse::Success(preset.into())), + None => { + warn!("Failed to fetch Backup Preset '{id}'"); + Ok(BackupPresetIdGetResponse::NotFound) + } + } +} diff --git a/src/api/mod.rs b/src/api/mod.rs index c591b98..7b43d3a 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -17,6 +17,7 @@ pub async fn start(port: u16, pool: SqlitePool, token: String) -> Result<()> { // .service(calls::backup_create) .service(calls::backup_preset_post) .service(calls::backup_preset_get) + .service(calls::backup_preset_id_get) .app_data(web::Data::new(State { pool: pool.clone(), token: token.to_owned(), diff --git a/src/main.rs b/src/main.rs index a9832bd..c96347a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ async fn main() -> Result<()> { .await?; backend::prepare(&pool).await?; - api::start(6969, pool, token.to_string()).await?; + api::start(6969, pool, token.to_owned()).await?; Ok(()) }