hacky-ruadat/src/model/planned_event.rs

175 lines
5.0 KiB
Rust
Raw Normal View History

2023-04-04 12:19:56 +02:00
use chrono::NaiveDate;
use serde::Serialize;
use sqlx::SqlitePool;
2023-04-26 17:02:47 +02:00
use super::tripdetails::TripDetails;
2023-04-04 15:16:21 +02:00
#[derive(Serialize, Clone)]
2023-04-04 12:19:56 +02:00
pub struct PlannedEvent {
2023-04-26 16:54:53 +02:00
pub id: i64,
2023-04-04 12:19:56 +02:00
name: String,
planned_amount_cox: i64,
allow_guests: bool,
trip_details_id: i64,
planned_starting_time: String,
max_people: i64,
day: String,
notes: Option<String>,
}
2023-04-04 15:16:21 +02:00
#[derive(Serialize)]
pub struct PlannedEventWithUser {
#[serde(flatten)]
planned_event: PlannedEvent,
2023-04-06 20:08:58 +02:00
cox_needed: bool,
2023-04-05 17:25:22 +02:00
cox: Vec<Registration>,
rower: Vec<Registration>,
}
//TODO: move to appropriate place
#[derive(Serialize)]
pub struct Registration {
pub name: String,
pub registered_at: String,
2023-04-04 15:16:21 +02:00
}
2023-04-04 12:19:56 +02:00
impl PlannedEvent {
2023-04-26 12:21:30 +02:00
pub async fn find_by_id(db: &SqlitePool, id: i64) -> Option<Self> {
sqlx::query_as!(
Self,
"
SELECT planned_event.id, name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes
FROM planned_event
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
WHERE planned_event.id like ?
",
id
)
.fetch_one(db)
.await
.ok()
}
2023-04-26 16:54:53 +02:00
2023-04-04 15:16:21 +02:00
pub async fn get_for_day(db: &SqlitePool, day: NaiveDate) -> Vec<PlannedEventWithUser> {
2023-04-04 19:49:27 +02:00
let day = format!("{day}");
2023-04-04 15:16:21 +02:00
let events = sqlx::query_as!(
2023-04-04 12:19:56 +02:00
PlannedEvent,
2023-04-04 19:49:27 +02:00
"SELECT planned_event.id, name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes
2023-04-04 12:19:56 +02:00
FROM planned_event
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
2023-04-04 19:49:27 +02:00
WHERE day=?",
2023-04-04 12:23:41 +02:00
day
2023-04-04 12:19:56 +02:00
)
.fetch_all(db)
.await
2023-04-04 15:16:21 +02:00
.unwrap(); //TODO: fixme
let mut ret = Vec::new();
for event in events {
2023-04-26 12:21:30 +02:00
let cox = event.get_all_cox(db).await;
2023-04-04 15:16:21 +02:00
ret.push(PlannedEventWithUser {
planned_event: event.clone(),
2023-04-06 20:08:58 +02:00
cox_needed: event.planned_amount_cox > cox.len() as i64,
cox,
2023-04-26 12:21:30 +02:00
rower: event.get_all_rower(db).await,
2023-04-04 19:49:27 +02:00
});
2023-04-04 15:16:21 +02:00
}
ret
}
2023-04-26 12:21:30 +02:00
async fn get_all_cox(&self, db: &SqlitePool) -> Vec<Registration> {
2023-04-05 17:25:22 +02:00
sqlx::query_as!(
Registration,
2023-04-04 15:16:21 +02:00
"
2023-04-05 17:25:22 +02:00
SELECT (SELECT name FROM user WHERE cox_id = id) as name, (SELECT created_at FROM user WHERE cox_id = id) as registered_at FROM trip WHERE planned_event_id = ?
2023-04-04 15:16:21 +02:00
",
2023-04-26 12:21:30 +02:00
self.id
2023-04-04 15:16:21 +02:00
)
.fetch_all(db)
.await
2023-04-26 12:21:30 +02:00
.unwrap() //Okay, as PlannedEvent can only be created with proper DB backing
2023-04-04 15:16:21 +02:00
}
2023-04-26 12:21:30 +02:00
async fn get_all_rower(&self, db: &SqlitePool) -> Vec<Registration> {
2023-04-05 17:25:22 +02:00
sqlx::query_as!(
Registration,
2023-04-04 15:16:21 +02:00
"
2023-04-05 17:25:22 +02:00
SELECT
(SELECT name FROM user WHERE user_trip.user_id = user.id) as name,
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at
FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_event WHERE id = ?)
2023-04-04 15:16:21 +02:00
",
2023-04-26 12:21:30 +02:00
self.id
2023-04-04 15:16:21 +02:00
)
.fetch_all(db)
.await
2023-04-26 12:21:30 +02:00
.unwrap() //Okay, as PlannedEvent can only be created with proper DB backing
2023-04-04 12:19:56 +02:00
}
2023-04-04 19:49:27 +02:00
pub async fn create(
2023-04-04 12:19:56 +02:00
db: &SqlitePool,
name: String,
planned_amount_cox: i32,
allow_guests: bool,
2023-04-26 17:02:47 +02:00
trip_details: TripDetails,
2023-04-04 12:19:56 +02:00
) {
sqlx::query!(
"INSERT INTO planned_event(name, planned_amount_cox, allow_guests, trip_details_id) VALUES(?, ?, ?, ?)",
2023-04-26 17:02:47 +02:00
name, planned_amount_cox, allow_guests, trip_details.id
2023-04-04 12:19:56 +02:00
)
.execute(db)
.await
2023-04-26 17:02:47 +02:00
.unwrap(); //Okay, as TripDetails can only be created with proper DB backing
2023-04-04 12:19:56 +02:00
}
2023-04-26 12:21:30 +02:00
pub async fn delete(&self, db: &SqlitePool) {
sqlx::query!("DELETE FROM planned_event WHERE id = ?", self.id)
2023-04-04 12:19:56 +02:00
.execute(db)
.await
2023-04-26 12:21:30 +02:00
.unwrap(); //Okay, as PlannedEvent can only be created with proper DB backing
}
}
#[cfg(test)]
mod test {
2023-04-26 17:02:47 +02:00
use crate::{model::tripdetails::TripDetails, testdb};
2023-04-26 12:21:30 +02:00
use super::PlannedEvent;
use chrono::NaiveDate;
use sqlx::SqlitePool;
#[sqlx::test]
fn test_get_day() {
let pool = testdb!();
let res =
PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await;
assert_eq!(res.len(), 1);
}
#[sqlx::test]
fn test_create() {
let pool = testdb!();
2023-04-26 17:02:47 +02:00
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
PlannedEvent::create(&pool, "new-event".into(), 2, false, trip_details).await;
2023-04-26 12:21:30 +02:00
let res =
PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await;
assert_eq!(res.len(), 2);
}
#[sqlx::test]
fn test_delete() {
let pool = testdb!();
let planned_event = PlannedEvent::find_by_id(&pool, 1).await.unwrap();
planned_event.delete(&pool).await;
let res =
PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await;
assert_eq!(res.len(), 0);
2023-04-04 12:19:56 +02:00
}
}