feat: implemented the /backup/preset [GET] endpoint
This commit is contained in:
parent
e8043cfbd5
commit
29ea2629b8
|
@ -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<State>,
|
||||
body: web::Json<BackupPresetPostRequest>,
|
||||
body: web::Json<BackupPreset>,
|
||||
) -> 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<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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String>,
|
||||
pub config: BackupConfig,
|
||||
|
@ -35,3 +35,8 @@ pub enum BackupPresetPostResponse {
|
|||
Success,
|
||||
Conflict,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct BackupPresetGetResponse {
|
||||
pub presets: Vec<BackupPreset>,
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ use sqlx::sqlite::SqlitePool;
|
|||
|
||||
pub async fn backup_preset_post(
|
||||
pool: &SqlitePool,
|
||||
request: BackupPresetPostRequest,
|
||||
request: BackupPreset,
|
||||
) -> Result<BackupPresetPostResponse> {
|
||||
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<BackupPresetGetResponse> {
|
||||
Ok(BackupPresetGetResponse {
|
||||
presets: backup::preset::get_all_presets(pool)
|
||||
.await?
|
||||
.iter()
|
||||
.map(|preset| preset.clone().into())
|
||||
.collect(),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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<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 {
|
||||
fn from(value: api::data::BackupConfig) -> 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()),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,20 +3,32 @@ use anyhow::{Error, Result};
|
|||
use log::info;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct RawPreset {
|
||||
id: String,
|
||||
description: Option<String>,
|
||||
config: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Preset {
|
||||
id: String,
|
||||
description: Option<String>,
|
||||
config: backup::Config,
|
||||
}
|
||||
|
||||
impl From<api::data::BackupPresetPostRequest> for Preset {
|
||||
fn from(value: api::data::BackupPresetPostRequest) -> Self {
|
||||
impl From<api::data::BackupPreset> for Preset {
|
||||
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 {
|
||||
id: value.id,
|
||||
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)),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue