brass/web/src/models/event.rs

180 lines
6.1 KiB
Rust

use chrono::{NaiveDate, NaiveTime};
use sqlx::{query, PgPool};
use super::{event_changeset::EventChangeset, Location, Result};
#[derive(Clone, Debug)]
pub struct Event {
pub id: i32,
pub date: NaiveDate,
pub start_time: NaiveTime,
pub end_time: NaiveTime,
pub name: String,
pub location_id: i32,
pub location: Option<Location>,
pub voluntary_wachhabender: bool,
pub voluntary_fuehrungsassistent: bool,
pub amount_of_posten: i16,
pub clothing: String,
pub canceled: bool,
pub note: Option<String>,
}
impl Event {
pub async fn create(pool: &PgPool, changeset: EventChangeset) -> Result<()> {
query!(r#"
INSERT INTO event (date, startTime, endTime, name, locationId, voluntaryWachhabender, voluntaryFuehrungsassistent, amountOfPosten, clothing, note)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
"#,
changeset.date, changeset.time.0, changeset.time.1,changeset.name, changeset.location_id, changeset.voluntary_wachhabender, changeset.voluntary_fuehrungsassistent, changeset.amount_of_posten, changeset.clothing, changeset.note).execute(pool).await?;
Ok(())
}
pub async fn read_all_by_date_and_area_including_location(
pool: &PgPool,
date: NaiveDate,
area_id: i32,
) -> Result<Vec<Event>> {
let records = query!(
r#"
SELECT
event.id AS eventId,
event.date,
event.startTime,
event.endTime,
event.name,
event.locationId,
event.voluntaryWachhabender,
event.voluntaryFuehrungsassistent,
event.amountOfPosten,
event.clothing,
event.canceled,
event.note,
location.id,
location.name AS locationName,
location.areaId AS locationAreaId
FROM event
JOIN location ON event.locationId = location.id
WHERE date = $1
AND location.areaId = $2;
"#,
date,
area_id
)
.fetch_all(pool)
.await?;
let events = records
.into_iter()
.map(|record| Event {
id: record.eventid,
date: record.date,
start_time: record.starttime,
end_time: record.endtime,
name: record.name.to_string(),
location_id: record.locationid,
location: Some(Location {
id: record.locationid,
name: record.locationname.to_string(),
area_id: record.locationareaid,
area: None,
}),
voluntary_wachhabender: record.voluntarywachhabender,
voluntary_fuehrungsassistent: record.voluntaryfuehrungsassistent,
amount_of_posten: record.amountofposten,
clothing: record.clothing.to_string(),
canceled: record.canceled,
note: record.note,
})
.collect();
Ok(events)
}
pub async fn read_by_id_including_location(pool: &PgPool, id: i32) -> Result<Option<Event>> {
let record = query!(
r#"
SELECT
event.id AS eventId,
event.date,
event.startTime,
event.endTime,
event.name,
event.locationId,
event.voluntaryWachhabender,
event.voluntaryFuehrungsassistent,
event.amountOfPosten,
event.clothing,
event.canceled,
event.note,
location.id,
location.name AS locationName,
location.areaId AS locationAreaId
FROM event
JOIN location ON event.locationId = location.id
WHERE event.id = $1;
"#,
id
)
.fetch_optional(pool)
.await?;
let event = record.map(|record| Event {
id: record.eventid,
date: record.date,
start_time: record.starttime,
end_time: record.endtime,
name: record.name.to_string(),
location_id: record.locationid,
location: Some(Location {
id: record.locationid,
name: record.locationname.to_string(),
area_id: record.locationareaid,
area: None,
}),
voluntary_wachhabender: record.voluntarywachhabender,
voluntary_fuehrungsassistent: record.voluntaryfuehrungsassistent,
amount_of_posten: record.amountofposten,
clothing: record.clothing.to_string(),
canceled: record.canceled,
note: record.note,
});
Ok(event)
}
pub async fn update(pool: &PgPool, id: i32, changeset: EventChangeset) -> Result<()> {
query!(r#"
UPDATE event SET date = $1, startTime = $2, endTime = $3, name = $4, locationId = $5, voluntaryWachhabender = $6, voluntaryFuehrungsassistent = $7, amountOfPosten = $8, clothing = $9, note = $10 WHERE id = $11;
"#,
changeset.date,
changeset.time.0,
changeset.time.1,changeset.name, changeset.location_id, changeset.voluntary_wachhabender, changeset.voluntary_fuehrungsassistent, changeset.amount_of_posten, changeset.clothing, changeset.note, id)
.execute(pool)
.await?;
Ok(())
}
pub async fn update_cancelation(pool: &PgPool, id: i32, canceled: bool) -> Result<()> {
query!(
"UPDATE event SET canceled = $1 WHERE id = $2;",
canceled,
id
)
.execute(pool)
.await?;
Ok(())
}
pub async fn delete(pool: &PgPool, id: i32) -> Result<()> {
query!("DELETE FROM event WHERE id = $1;", id)
.execute(pool)
.await?;
Ok(())
}
}