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};
#[derive(Template)]
#[template(path = "export/availabilityxml.html")]
#[template(path = "export/availability.html")]
struct AvailabilityExportTemplate {
user: User,
areas: Option<Vec<Area>>

View File

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

View File

@ -21,6 +21,18 @@
</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 %}
<div class="field">
<label class="label">Bereich</label>