From 29ea2629b8cebb7586c7652fd41494979178a4b5 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sat, 11 Nov 2023 20:54:21 +0100 Subject: [PATCH] feat: implemented the /backup/preset [GET] endpoint --- src/api/calls.rs | 15 +++++++++++++-- src/api/data.rs | 13 +++++++++---- src/api/handlers.rs | 12 +++++++++++- src/api/mod.rs | 1 + src/backend/backup/mod.rs | 23 +++++++++++++++++++++-- src/backend/backup/preset.rs | 30 ++++++++++++++++++++++++++++-- 6 files changed, 83 insertions(+), 11 deletions(-) diff --git a/src/api/calls.rs b/src/api/calls.rs index a0cae7e..26cc6ca 100644 --- a/src/api/calls.rs +++ b/src/api/calls.rs @@ -1,5 +1,5 @@ use crate::api::{data::*, handlers, State}; -use actix_web::{post, web, HttpResponse, Responder}; +use actix_web::{get, post, web, HttpResponse, Responder}; use log::error; // #[post("/backup/create")] @@ -21,7 +21,7 @@ use log::error; #[post("/backup/preset")] async fn backup_preset_post( data: web::Data, - body: web::Json, + body: web::Json, ) -> impl Responder { match handlers::backup_preset_post(&data.pool, body.into_inner()).await { Ok(resp) => match resp { @@ -34,3 +34,14 @@ async fn backup_preset_post( } } } + +#[get("/backup/preset")] +async fn backup_preset_get(data: web::Data) -> impl Responder { + match handlers::backup_preset_get(&data.pool).await { + Ok(resp) => HttpResponse::Ok().json(&resp), + Err(e) => { + error!("While handling /backup/preset [GET] request: {e}"); + HttpResponse::InternalServerError().finish() + } + } +} diff --git a/src/api/data.rs b/src/api/data.rs index 87a3654..48012e6 100644 --- a/src/api/data.rs +++ b/src/api/data.rs @@ -1,9 +1,9 @@ use serde::{Deserialize, Serialize}; -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct BackupConfigDocker {} -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "PascalCase")] pub struct BackupConfig { pub nginx_config: bool, @@ -22,9 +22,9 @@ pub enum BackupCreateResponse { Success, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "PascalCase")] -pub struct BackupPresetPostRequest { +pub struct BackupPreset { pub id: String, pub description: Option, pub config: BackupConfig, @@ -35,3 +35,8 @@ pub enum BackupPresetPostResponse { Success, Conflict, } + +#[derive(Debug, Serialize)] +pub struct BackupPresetGetResponse { + pub presets: Vec, +} diff --git a/src/api/handlers.rs b/src/api/handlers.rs index c9e55f8..926d78e 100644 --- a/src/api/handlers.rs +++ b/src/api/handlers.rs @@ -13,7 +13,7 @@ use sqlx::sqlite::SqlitePool; pub async fn backup_preset_post( pool: &SqlitePool, - request: BackupPresetPostRequest, + request: BackupPreset, ) -> Result { match backup::preset::Preset::load(pool, &request.id).await? { Some(_) => { @@ -29,3 +29,13 @@ pub async fn backup_preset_post( } } } + +pub async fn backup_preset_get(pool: &SqlitePool) -> Result { + Ok(BackupPresetGetResponse { + presets: backup::preset::get_all_presets(pool) + .await? + .iter() + .map(|preset| preset.clone().into()) + .collect(), + }) +} diff --git a/src/api/mod.rs b/src/api/mod.rs index a90102e..c591b98 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -16,6 +16,7 @@ pub async fn start(port: u16, pool: SqlitePool, token: String) -> Result<()> { App::new() // .service(calls::backup_create) .service(calls::backup_preset_post) + .service(calls::backup_preset_get) .app_data(web::Data::new(State { pool: pool.clone(), token: token.to_owned(), diff --git a/src/backend/backup/mod.rs b/src/backend/backup/mod.rs index 79921df..821865a 100644 --- a/src/backend/backup/mod.rs +++ b/src/backend/backup/mod.rs @@ -3,10 +3,10 @@ pub mod preset; use crate::api; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone)] struct DockerConfig {} -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone)] pub struct Config { nginx_config: bool, mail_server: bool, @@ -21,6 +21,12 @@ impl From for DockerConfig { } } +impl From for api::data::BackupConfigDocker { + fn from(_value: DockerConfig) -> Self { + Self {} + } +} + impl From for Config { fn from(value: api::data::BackupConfig) -> Self { Self { @@ -33,3 +39,16 @@ impl From for Config { } } } + +impl From for api::data::BackupConfig { + fn from(value: Config) -> Self { + Self { + nginx_config: value.nginx_config, + mail_server: value.mail_server, + docker: match value.docker { + None => None, + Some(cfg) => Some(cfg.into()), + }, + } + } +} diff --git a/src/backend/backup/preset.rs b/src/backend/backup/preset.rs index bb3581a..7a14b99 100644 --- a/src/backend/backup/preset.rs +++ b/src/backend/backup/preset.rs @@ -3,20 +3,32 @@ use anyhow::{Error, Result}; use log::info; use sqlx::SqlitePool; +#[derive(Clone)] struct RawPreset { id: String, description: Option, config: String, } +#[derive(Clone)] pub struct Preset { id: String, description: Option, config: backup::Config, } -impl From for Preset { - fn from(value: api::data::BackupPresetPostRequest) -> Self { +impl From for Preset { + fn from(value: api::data::BackupPreset) -> Self { + Self { + id: value.id, + description: value.description, + config: value.config.into(), + } + } +} + +impl From for api::data::BackupPreset { + fn from(value: Preset) -> Self { Self { id: value.id, description: value.description, @@ -75,3 +87,17 @@ impl Preset { } } } + +pub async fn get_all_presets(pool: &SqlitePool) -> Result> { + let query_result = sqlx::query_as!(RawPreset, r#"SELECT * FROM Presets;"#) + .fetch_all(pool) + .await; + + match query_result { + Ok(raw_presets) => Ok(raw_presets + .iter() + .map(|raw| raw.clone().try_into()) + .collect::>>()?), + Err(e) => Err(Error::new(e)), + } +}