2023-04-04 12:19:56 +02:00
|
|
|
use chrono::NaiveDate;
|
|
|
|
use serde::Serialize;
|
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
2023-04-04 15:16:21 +02:00
|
|
|
#[derive(Serialize, Clone)]
|
2023-04-04 12:19:56 +02:00
|
|
|
pub struct PlannedEvent {
|
|
|
|
id: i64,
|
|
|
|
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,
|
|
|
|
cox: Vec<String>,
|
|
|
|
rower: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2023-04-04 12:19:56 +02:00
|
|
|
impl PlannedEvent {
|
2023-04-04 15:16:21 +02:00
|
|
|
pub async fn get_for_day(db: &SqlitePool, day: NaiveDate) -> Vec<PlannedEventWithUser> {
|
2023-04-04 12:23:41 +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,
|
|
|
|
"
|
|
|
|
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
|
2023-04-04 12:23:41 +02:00
|
|
|
WHERE day=?
|
|
|
|
",
|
|
|
|
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 {
|
|
|
|
ret.push(PlannedEventWithUser {
|
|
|
|
planned_event: event.clone(),
|
|
|
|
cox: Self::get_all_cox_for_id(db, event.id).await,
|
|
|
|
rower: Self::get_all_rower_for_id(db, event.id).await,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn rower_can_register(db: &SqlitePool, trip_details_id: i64) -> bool {
|
|
|
|
let amount_currently_registered = sqlx::query!(
|
|
|
|
"
|
|
|
|
SELECT COUNT(*) as count FROM user_trip WHERE trip_details_id = ?
|
|
|
|
",
|
|
|
|
trip_details_id
|
|
|
|
)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.unwrap(); //TODO: fixme
|
|
|
|
let amount_currently_registered = amount_currently_registered.count as i64;
|
|
|
|
|
|
|
|
let amount_allowed_to_register = sqlx::query!(
|
|
|
|
"
|
|
|
|
SELECT max_people FROM trip_details WHERE id = ?
|
|
|
|
",
|
|
|
|
trip_details_id
|
|
|
|
)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.unwrap(); //TODO: fixme
|
|
|
|
let amount_allowed_to_register = amount_allowed_to_register.max_people;
|
|
|
|
|
|
|
|
amount_currently_registered < amount_allowed_to_register
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_all_cox_for_id(db: &SqlitePool, id: i64) -> Vec<String> {
|
|
|
|
let res = sqlx::query!(
|
|
|
|
"
|
|
|
|
SELECT (SELECT name FROM user WHERE cox_id = id) as name FROM trip WHERE planned_event_id = ?
|
|
|
|
",
|
|
|
|
id
|
|
|
|
)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
|
|
|
.unwrap(); //TODO: fixme
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
for r in res {
|
|
|
|
ret.push(r.name);
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_all_rower_for_id(db: &SqlitePool, id: i64) -> Vec<String> {
|
|
|
|
let res = sqlx::query!(
|
|
|
|
"
|
|
|
|
SELECT (SELECT name FROM user WHERE user_trip.user_id = user.id) as name FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_event WHERE id = ?)
|
|
|
|
",
|
|
|
|
id
|
|
|
|
)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
|
|
|
.unwrap(); //TODO: fixme
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
for r in res {
|
|
|
|
ret.push(r.name);
|
|
|
|
}
|
|
|
|
ret
|
2023-04-04 12:19:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn new(
|
|
|
|
db: &SqlitePool,
|
|
|
|
name: String,
|
|
|
|
planned_amount_cox: i32,
|
|
|
|
allow_guests: bool,
|
|
|
|
trip_details_id: i64,
|
|
|
|
) {
|
|
|
|
sqlx::query!(
|
|
|
|
"INSERT INTO planned_event(name, planned_amount_cox, allow_guests, trip_details_id) VALUES(?, ?, ?, ?)",
|
|
|
|
name, planned_amount_cox, allow_guests, trip_details_id
|
|
|
|
)
|
|
|
|
.execute(db)
|
|
|
|
.await
|
|
|
|
.unwrap(); //TODO: fixme
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete(db: &SqlitePool, id: i64) {
|
|
|
|
sqlx::query!("DELETE FROM planned_event WHERE id = ?", id)
|
|
|
|
.execute(db)
|
|
|
|
.await
|
|
|
|
.unwrap(); //TODO: fixme
|
|
|
|
}
|
|
|
|
}
|