feat: allow json export

This commit is contained in:
Max Hohlfeld 2024-07-16 22:44:45 +02:00
parent 99b2fbafa0
commit 97367cb105
3 changed files with 28 additions and 9 deletions

View File

@ -6,7 +6,7 @@ use sqlx::PgPool;
use crate::models::{Area, User, Role}; use crate::models::{Area, User, Role};
#[derive(Template)] #[derive(Template)]
#[template(path = "export/availabilityxml.html")] #[template(path = "export/availability.html")]
struct AvailabilityExportTemplate { struct AvailabilityExportTemplate {
user: User, user: User,
areas: Option<Vec<Area>> areas: Option<Vec<Area>>

View File

@ -11,18 +11,19 @@ struct ExportQuery {
year: u16, year: u16,
month: u8, month: u8,
area_id: Option<i32>, area_id: Option<i32>,
format: String
} }
#[derive(Serialize)] #[derive(Serialize)]
struct ExportXml { struct Export {
year: u16, year: u16,
month: u8, month: u8,
area: String, area: String,
availabillities: Vec<ExportAvailabillityXml>, availabillities: Vec<ExportAvailabillity>,
} }
#[derive(Serialize)] #[derive(Serialize)]
struct ExportAvailabillityXml { struct ExportAvailabillity {
name: String, name: String,
area: String, area: String,
function: Function, function: Function,
@ -68,7 +69,7 @@ pub async fn get(
let export_availabillities = availabillities let export_availabillities = availabillities
.into_iter() .into_iter()
.map(|a| ExportAvailabillityXml { .map(|a| ExportAvailabillity {
name: a.user.as_ref().unwrap().name.clone(), name: a.user.as_ref().unwrap().name.clone(),
area: a.user.as_ref().unwrap().area.as_ref().unwrap().name.clone(), area: a.user.as_ref().unwrap().area.as_ref().unwrap().name.clone(),
function: a.user.unwrap().function, function: a.user.unwrap().function,
@ -84,18 +85,24 @@ pub async fn get(
}) })
.collect(); .collect();
let out = ExportXml { let export = Export {
year: query.year, year: query.year,
month: query.month, month: query.month,
area: area_id.to_string(), area: area_id.to_string(),
availabillities: export_availabillities, availabillities: export_availabillities,
}; };
if let Ok(xml) = quick_xml::se::to_string(&out) { let out = match query.format.as_str() {
"xml" => quick_xml::se::to_string(&export).unwrap_or(String::new()),
"json" => serde_json::to_string(&export).unwrap_or(String::new()),
_ => return HttpResponse::BadRequest().finish()
};
if !out.is_empty() {
return HttpResponse::Ok() return HttpResponse::Ok()
.content_type(ContentType::xml()) .content_type(ContentType::xml())
.insert_header((CONTENT_DISPOSITION, ContentDisposition::attachment("export.xml"))) .insert_header((CONTENT_DISPOSITION, ContentDisposition::attachment(format!("export.{}", query.format))))
.body(xml); .body(out);
} }
HttpResponse::BadRequest().finish() HttpResponse::BadRequest().finish()

View File

@ -21,6 +21,18 @@
</div> </div>
</div> </div>
<div class="field">
<label class="label">Format</label>
<div class="control">
<div class="select is-fullwidth">
<select name="format">
<option value="xml">XML</option>
<option value="json">JSON</option>
</select>
</div>
</div>
</div>
{% if user.role == Role::Admin %} {% if user.role == Role::Admin %}
<div class="field"> <div class="field">
<label class="label">Bereich</label> <label class="label">Bereich</label>