File size: 976 Bytes
bbb1195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use axum::Json;
use serde::Serialize;

use crate::models::AppConfig;
use crate::modules;
use crate::error::AppError;

/// API response wrapper
#[derive(Serialize)]
pub struct ApiResponse<T> {
    pub success: bool,
    pub data: Option<T>,
    pub error: Option<String>,
}

impl<T: Serialize> ApiResponse<T> {
    pub fn success(data: T) -> Json<Self> {
        Json(Self {
            success: true,
            data: Some(data),
            error: None,
        })
    }
}

/// Load configuration
pub async fn load_config() -> Result<Json<ApiResponse<AppConfig>>, AppError> {
    let config = modules::load_app_config().map_err(AppError::Config)?;
    Ok(ApiResponse::success(config))
}

/// Save configuration
pub async fn save_config(
    Json(config): Json<AppConfig>,
) -> Result<Json<ApiResponse<()>>, AppError> {
    modules::save_app_config(&config).map_err(AppError::Config)?;
    modules::logger::log_info("Configuration saved");
    Ok(ApiResponse::success(()))
}