Lines
100 %
Functions
25 %
Branches
//! The category breakdown table as a CSV download.
use std::sync::Arc;
use axum::Extension;
use axum::extract::{Query, State};
use axum::http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
use axum::response::IntoResponse;
use server::command::report::view::category_csv;
use super::category_breakdown_query::ask;
use crate::pages::forms::unprocessable_fields;
use crate::{AppState, jwt_auth::JWTAuthMiddleware, response::WebError};
pub async fn category_breakdown_report_csv(
Query(pairs): Query<Vec<(String, String)>>,
State(_data): State<Arc<AppState>>,
Extension(jwt_auth): Extension<JWTAuthMiddleware>,
) -> Result<impl IntoResponse, WebError> {
let asked = ask(jwt_auth.user.id, pairs).await?;
let data = asked.report.map_err(unprocessable_fields)?;
let day = |key| asked.values.text(key).unwrap_or_default();
let (from, to) = (day("from"), day("to"));
Ok((
[
(CONTENT_TYPE, "text/csv; charset=utf-8".to_string()),
(
CONTENT_DISPOSITION,
format!("attachment; filename=\"category-report-{from}-{to}.csv\""),
),
],
category_csv(&data),
))
}