brass/src/models/event.rs

58 lines
1.8 KiB
Rust

use chrono::{NaiveDate, NaiveTime};
use sqlx::{query, PgPool};
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 voluntary_wachhabender: bool,
pub amount_of_posten: i16,
pub clothing: String,
pub canceled: bool,
}
impl Event {
pub async fn create(
pool: &PgPool,
date: NaiveDate,
start_time: NaiveTime,
end_time: NaiveTime,
name: String,
location_id: i32,
voluntary_wachhabender: bool,
amount_of_posten: i16,
clothing: String,
) -> anyhow::Result<i32> {
let result = query!("INSERT INTO event (date, startTime, endTime, name, locationId, voluntaryWachhabender, amountOfPosten, clothing) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id;", date, start_time, end_time, name, location_id, voluntary_wachhabender, amount_of_posten, clothing).fetch_one(pool).await?;
Ok(result.id)
}
pub async fn read_by_date(pool: &PgPool, date: NaiveDate) -> anyhow::Result<Vec<Event>> {
let records = query!("SELECT * FROM event WHERE date = $1;", date)
.fetch_all(pool)
.await?;
let events = records
.iter()
.map(|record| Event {
id: record.id,
date: record.date,
start_time: record.starttime,
end_time: record.endtime,
name: record.name.to_string(),
location_id: record.locationid,
voluntary_wachhabender: record.voluntarywachhabender,
amount_of_posten: record.amountofposten,
clothing: record.clothing.to_string(),
canceled: record.canceled,
})
.collect();
Ok(events)
}
}