Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I28cf5b7b7cff8e90e123d624d97cf9656a6a6964
68 lines
2 KiB
Rust
68 lines
2 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use axum::{Json, extract::State};
|
|
use serde::Deserialize;
|
|
|
|
use crate::{error::ApiError, state::AppState};
|
|
|
|
#[derive(Debug, Deserialize, utoipa::ToSchema)]
|
|
pub struct ExportRequest {
|
|
pub format: String,
|
|
#[schema(value_type = String)]
|
|
pub destination: PathBuf,
|
|
}
|
|
|
|
#[utoipa::path(
|
|
post,
|
|
path = "/api/v1/export",
|
|
tag = "export",
|
|
responses(
|
|
(status = 200, description = "Export job submitted"),
|
|
(status = 401, description = "Unauthorized"),
|
|
(status = 403, description = "Forbidden"),
|
|
(status = 500, description = "Internal server error"),
|
|
),
|
|
security(("bearer_auth" = []))
|
|
)]
|
|
pub async fn trigger_export(
|
|
State(state): State<AppState>,
|
|
) -> Result<Json<serde_json::Value>, ApiError> {
|
|
// Default export to JSON in data dir
|
|
let dest =
|
|
pinakes_core::config::Config::default_data_dir().join("export.json");
|
|
let kind = pinakes_core::jobs::JobKind::Export {
|
|
format: pinakes_core::jobs::ExportFormat::Json,
|
|
destination: dest,
|
|
};
|
|
let job_id = state.job_queue.submit(kind).await;
|
|
Ok(Json(serde_json::json!({ "job_id": job_id.to_string() })))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
post,
|
|
path = "/api/v1/export/options",
|
|
tag = "export",
|
|
request_body = ExportRequest,
|
|
responses(
|
|
(status = 200, description = "Export job submitted"),
|
|
(status = 401, description = "Unauthorized"),
|
|
(status = 403, description = "Forbidden"),
|
|
(status = 500, description = "Internal server error"),
|
|
),
|
|
security(("bearer_auth" = []))
|
|
)]
|
|
pub async fn trigger_export_with_options(
|
|
State(state): State<AppState>,
|
|
Json(req): Json<ExportRequest>,
|
|
) -> Result<Json<serde_json::Value>, ApiError> {
|
|
let format = match req.format.as_str() {
|
|
"csv" => pinakes_core::jobs::ExportFormat::Csv,
|
|
_ => pinakes_core::jobs::ExportFormat::Json,
|
|
};
|
|
let kind = pinakes_core::jobs::JobKind::Export {
|
|
format,
|
|
destination: req.destination,
|
|
};
|
|
let job_id = state.job_queue.submit(kind).await;
|
|
Ok(Json(serde_json::json!({ "job_id": job_id.to_string() })))
|
|
}
|