brass/db/src/models/export_event_row.rs

68 lines
1.9 KiB
Rust

use chrono::{NaiveDate, NaiveDateTime};
use sqlx::{
PgPool,
postgres::{PgHasArrayType, PgTypeInfo},
query_file,
};
use super::{Function, Result};
pub struct ExportEventRow {
pub start_timestamp: NaiveDateTime,
pub end_timestamp: NaiveDateTime,
pub amount_of_posten: i16,
pub fuehrungsassistent_required: bool,
pub voluntary_wachhabender: bool,
pub location_name: String,
pub event_name: String,
pub assignments: Vec<SimpleAssignment>,
pub vehicles: Vec<String>,
}
#[derive(Debug, sqlx::Type, Clone)]
#[sqlx(type_name = "function", no_pg_array)]
pub struct SimpleAssignment {
pub name: String,
pub function: Function,
}
impl PgHasArrayType for SimpleAssignment {
fn array_type_info() -> sqlx::postgres::PgTypeInfo {
PgTypeInfo::with_name("simpleAssignment[]")
}
}
impl ExportEventRow {
pub async fn read_all_by_timerange_and_area(
pool: &PgPool,
time: (NaiveDate, NaiveDate),
area: i32,
) -> Result<Vec<ExportEventRow>> {
let rows = query_file!(
"sql/export_event_row/read_all_by_timerange_and_area.sql",
time.0,
time.1,
area
)
.fetch_all(pool)
.await?;
let export_rows = rows
.into_iter()
.map(|r| ExportEventRow {
start_timestamp: r.starttimestamp.naive_utc(),
end_timestamp: r.endtimestamp.naive_utc(),
amount_of_posten: r.amountofposten,
fuehrungsassistent_required: r.fuehrungsassistentrequired,
voluntary_wachhabender: r.voluntarywachhabender,
location_name: r.locationname,
event_name: r.eventname,
assignments: r.assignments.unwrap_or_default(),
vehicles: r.vehicles.unwrap_or_default(),
})
.collect();
Ok(export_rows)
}
}