feat: implemented the /backup/preset [GET] endpoint

This commit is contained in:
antifallobst 2023-11-11 20:54:21 +01:00
parent e8043cfbd5
commit 29ea2629b8
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
6 changed files with 83 additions and 11 deletions

View File

@ -1,5 +1,5 @@
use crate::api::{data::*, handlers, State}; use crate::api::{data::*, handlers, State};
use actix_web::{post, web, HttpResponse, Responder}; use actix_web::{get, post, web, HttpResponse, Responder};
use log::error; use log::error;
// #[post("/backup/create")] // #[post("/backup/create")]
@ -21,7 +21,7 @@ use log::error;
#[post("/backup/preset")] #[post("/backup/preset")]
async fn backup_preset_post( async fn backup_preset_post(
data: web::Data<State>, data: web::Data<State>,
body: web::Json<BackupPresetPostRequest>, body: web::Json<BackupPreset>,
) -> impl Responder { ) -> impl Responder {
match handlers::backup_preset_post(&data.pool, body.into_inner()).await { match handlers::backup_preset_post(&data.pool, body.into_inner()).await {
Ok(resp) => match resp { Ok(resp) => match resp {
@ -34,3 +34,14 @@ async fn backup_preset_post(
} }
} }
} }
#[get("/backup/preset")]
async fn backup_preset_get(data: web::Data<State>) -> 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()
}
}
}

View File

@ -1,9 +1,9 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct BackupConfigDocker {} pub struct BackupConfigDocker {}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")] #[serde(rename_all = "PascalCase")]
pub struct BackupConfig { pub struct BackupConfig {
pub nginx_config: bool, pub nginx_config: bool,
@ -22,9 +22,9 @@ pub enum BackupCreateResponse {
Success, Success,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")] #[serde(rename_all = "PascalCase")]
pub struct BackupPresetPostRequest { pub struct BackupPreset {
pub id: String, pub id: String,
pub description: Option<String>, pub description: Option<String>,
pub config: BackupConfig, pub config: BackupConfig,
@ -35,3 +35,8 @@ pub enum BackupPresetPostResponse {
Success, Success,
Conflict, Conflict,
} }
#[derive(Debug, Serialize)]
pub struct BackupPresetGetResponse {
pub presets: Vec<BackupPreset>,
}

View File

@ -13,7 +13,7 @@ use sqlx::sqlite::SqlitePool;
pub async fn backup_preset_post( pub async fn backup_preset_post(
pool: &SqlitePool, pool: &SqlitePool,
request: BackupPresetPostRequest, request: BackupPreset,
) -> Result<BackupPresetPostResponse> { ) -> Result<BackupPresetPostResponse> {
match backup::preset::Preset::load(pool, &request.id).await? { match backup::preset::Preset::load(pool, &request.id).await? {
Some(_) => { Some(_) => {
@ -29,3 +29,13 @@ pub async fn backup_preset_post(
} }
} }
} }
pub async fn backup_preset_get(pool: &SqlitePool) -> Result<BackupPresetGetResponse> {
Ok(BackupPresetGetResponse {
presets: backup::preset::get_all_presets(pool)
.await?
.iter()
.map(|preset| preset.clone().into())
.collect(),
})
}

View File

@ -16,6 +16,7 @@ pub async fn start(port: u16, pool: SqlitePool, token: String) -> Result<()> {
App::new() App::new()
// .service(calls::backup_create) // .service(calls::backup_create)
.service(calls::backup_preset_post) .service(calls::backup_preset_post)
.service(calls::backup_preset_get)
.app_data(web::Data::new(State { .app_data(web::Data::new(State {
pool: pool.clone(), pool: pool.clone(),
token: token.to_owned(), token: token.to_owned(),

View File

@ -3,10 +3,10 @@ pub mod preset;
use crate::api; use crate::api;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Clone)]
struct DockerConfig {} struct DockerConfig {}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Clone)]
pub struct Config { pub struct Config {
nginx_config: bool, nginx_config: bool,
mail_server: bool, mail_server: bool,
@ -21,6 +21,12 @@ impl From<api::data::BackupConfigDocker> for DockerConfig {
} }
} }
impl From<DockerConfig> for api::data::BackupConfigDocker {
fn from(_value: DockerConfig) -> Self {
Self {}
}
}
impl From<api::data::BackupConfig> for Config { impl From<api::data::BackupConfig> for Config {
fn from(value: api::data::BackupConfig) -> Self { fn from(value: api::data::BackupConfig) -> Self {
Self { Self {
@ -33,3 +39,16 @@ impl From<api::data::BackupConfig> for Config {
} }
} }
} }
impl From<Config> 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()),
},
}
}
}

View File

@ -3,20 +3,32 @@ use anyhow::{Error, Result};
use log::info; use log::info;
use sqlx::SqlitePool; use sqlx::SqlitePool;
#[derive(Clone)]
struct RawPreset { struct RawPreset {
id: String, id: String,
description: Option<String>, description: Option<String>,
config: String, config: String,
} }
#[derive(Clone)]
pub struct Preset { pub struct Preset {
id: String, id: String,
description: Option<String>, description: Option<String>,
config: backup::Config, config: backup::Config,
} }
impl From<api::data::BackupPresetPostRequest> for Preset { impl From<api::data::BackupPreset> for Preset {
fn from(value: api::data::BackupPresetPostRequest) -> Self { fn from(value: api::data::BackupPreset) -> Self {
Self {
id: value.id,
description: value.description,
config: value.config.into(),
}
}
}
impl From<Preset> for api::data::BackupPreset {
fn from(value: Preset) -> Self {
Self { Self {
id: value.id, id: value.id,
description: value.description, description: value.description,
@ -75,3 +87,17 @@ impl Preset {
} }
} }
} }
pub async fn get_all_presets(pool: &SqlitePool) -> Result<Vec<Preset>> {
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::<Result<Vec<Preset>>>()?),
Err(e) => Err(Error::new(e)),
}
}