2023-04-04 12:19:56 +02:00
|
|
|
use chrono::NaiveDate;
|
|
|
|
use serde::Serialize;
|
2023-04-28 21:19:51 +02:00
|
|
|
use sqlx::{FromRow, SqlitePool};
|
2023-04-04 12:19:56 +02:00
|
|
|
|
2023-05-03 13:32:23 +02:00
|
|
|
use super::{tripdetails::TripDetails, triptype::TripType, user::User};
|
2023-04-26 17:02:47 +02:00
|
|
|
|
2023-04-28 21:19:51 +02:00
|
|
|
#[derive(Serialize, Clone, FromRow)]
|
2023-04-04 12:19:56 +02:00
|
|
|
pub struct PlannedEvent {
|
2023-04-26 16:54:53 +02:00
|
|
|
pub id: i64,
|
2023-05-24 15:09:38 +02:00
|
|
|
pub name: String,
|
2023-04-04 12:19:56 +02:00
|
|
|
planned_amount_cox: i64,
|
|
|
|
trip_details_id: i64,
|
2023-05-24 15:09:38 +02:00
|
|
|
pub planned_starting_time: String,
|
2023-04-04 12:19:56 +02:00
|
|
|
max_people: i64,
|
2023-05-24 15:09:38 +02:00
|
|
|
pub day: String,
|
2023-04-04 12:19:56 +02:00
|
|
|
notes: Option<String>,
|
2023-04-29 18:57:01 +02:00
|
|
|
pub allow_guests: bool,
|
2023-04-28 21:19:51 +02:00
|
|
|
trip_type_id: Option<i64>,
|
2023-04-04 12:19:56 +02:00
|
|
|
}
|
|
|
|
|
2023-04-04 15:16:21 +02:00
|
|
|
#[derive(Serialize)]
|
2023-04-28 21:19:51 +02:00
|
|
|
pub struct PlannedEventWithUserAndTriptype {
|
2023-04-04 15:16:21 +02:00
|
|
|
#[serde(flatten)]
|
2023-04-29 18:57:01 +02:00
|
|
|
pub planned_event: PlannedEvent,
|
2023-04-28 21:19:51 +02:00
|
|
|
trip_type: Option<TripType>,
|
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-28 19:08:17 +02:00
|
|
|
pub is_guest: bool,
|
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,
|
|
|
|
"
|
2023-04-28 21:19:51 +02:00
|
|
|
SELECT
|
2023-04-29 18:57:01 +02:00
|
|
|
planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id
|
2023-04-26 12:21:30 +02:00
|
|
|
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-28 21:19:51 +02:00
|
|
|
pub async fn get_for_day(
|
|
|
|
db: &SqlitePool,
|
|
|
|
day: NaiveDate,
|
|
|
|
) -> Vec<PlannedEventWithUserAndTriptype> {
|
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-29 18:57:01 +02:00
|
|
|
"SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id
|
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-28 21:19:51 +02:00
|
|
|
let mut trip_type = None;
|
|
|
|
if let Some(trip_type_id) = event.trip_type_id {
|
|
|
|
trip_type = TripType::find_by_id(db, trip_type_id).await;
|
|
|
|
}
|
|
|
|
ret.push(PlannedEventWithUserAndTriptype {
|
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-05-24 12:18:14 +02:00
|
|
|
planned_event: event,
|
2023-04-28 21:19:51 +02:00
|
|
|
trip_type,
|
2023-04-04 19:49:27 +02:00
|
|
|
});
|
2023-04-04 15:16:21 +02:00
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2023-05-24 15:09:38 +02:00
|
|
|
pub async fn all(db: &SqlitePool) -> Vec<PlannedEvent> {
|
|
|
|
sqlx::query_as!(
|
|
|
|
PlannedEvent,
|
|
|
|
"SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, max_people, day, notes, allow_guests, trip_type_id
|
|
|
|
FROM planned_event
|
|
|
|
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id",
|
|
|
|
)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
|
|
|
.unwrap() //TODO: fixme
|
|
|
|
}
|
|
|
|
|
2023-04-26 12:21:30 +02:00
|
|
|
async fn get_all_cox(&self, db: &SqlitePool) -> Vec<Registration> {
|
2023-05-03 13:32:23 +02:00
|
|
|
//TODO: switch to join
|
2023-04-05 17:25:22 +02:00
|
|
|
sqlx::query_as!(
|
|
|
|
Registration,
|
2023-04-04 15:16:21 +02:00
|
|
|
"
|
2023-04-28 19:08:17 +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,
|
|
|
|
(SELECT is_guest FROM user WHERE cox_id = id) as is_guest
|
|
|
|
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-05-03 13:32:23 +02:00
|
|
|
//TODO: switch to join
|
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,
|
2023-04-28 19:08:17 +02:00
|
|
|
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at,
|
|
|
|
(SELECT is_guest FROM user WHERE user_trip.user_id = user.id) as is_guest
|
2023-04-05 17:25:22 +02:00
|
|
|
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-05-03 13:32:23 +02:00
|
|
|
//TODO: add tests
|
|
|
|
pub async fn is_rower_registered(&self, db: &SqlitePool, user: &User) -> bool {
|
|
|
|
let is_rower = sqlx::query!(
|
|
|
|
"SELECT count(*) as amount
|
|
|
|
FROM user_trip
|
|
|
|
WHERE trip_details_id =
|
|
|
|
(SELECT trip_details_id FROM planned_event WHERE id = ?)
|
|
|
|
AND user_id = ?",
|
|
|
|
self.id,
|
|
|
|
user.id
|
|
|
|
)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.unwrap(); //Okay, bc planned_event can only be created with proper DB backing
|
|
|
|
is_rower.amount > 0
|
|
|
|
}
|
|
|
|
|
2023-04-04 19:49:27 +02:00
|
|
|
pub async fn create(
|
2023-04-04 12:19:56 +02:00
|
|
|
db: &SqlitePool,
|
2023-05-24 12:11:55 +02:00
|
|
|
name: &str,
|
2023-04-04 12:19:56 +02:00
|
|
|
planned_amount_cox: i32,
|
2023-04-26 17:02:47 +02:00
|
|
|
trip_details: TripDetails,
|
2023-04-04 12:19:56 +02:00
|
|
|
) {
|
|
|
|
sqlx::query!(
|
2023-04-29 18:57:01 +02:00
|
|
|
"INSERT INTO planned_event(name, planned_amount_cox, trip_details_id) VALUES(?, ?, ?)",
|
|
|
|
name,
|
|
|
|
planned_amount_cox,
|
|
|
|
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-05-03 22:40:22 +02:00
|
|
|
//TODO: create unit test
|
|
|
|
pub async fn update(
|
|
|
|
&self,
|
|
|
|
db: &SqlitePool,
|
|
|
|
planned_amount_cox: i32,
|
|
|
|
max_people: i32,
|
2023-05-24 12:11:55 +02:00
|
|
|
notes: Option<&str>,
|
2023-05-03 22:40:22 +02:00
|
|
|
) {
|
|
|
|
sqlx::query!(
|
|
|
|
"UPDATE planned_event SET planned_amount_cox = ? WHERE id = ?",
|
|
|
|
planned_amount_cox,
|
|
|
|
self.id
|
|
|
|
)
|
|
|
|
.execute(db)
|
|
|
|
.await
|
|
|
|
.unwrap(); //Okay, as planned_event can only be created with proper DB backing
|
|
|
|
|
|
|
|
sqlx::query!(
|
|
|
|
"UPDATE trip_details SET max_people = ?, notes = ? WHERE id = ?",
|
2023-05-03 22:54:10 +02:00
|
|
|
max_people,
|
2023-05-03 22:40:22 +02:00
|
|
|
notes,
|
|
|
|
self.trip_details_id
|
|
|
|
)
|
|
|
|
.execute(db)
|
|
|
|
.await
|
|
|
|
.unwrap(); //Okay, as planned_event can only be created with proper DB backing
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2023-05-24 15:36:38 +02:00
|
|
|
|
|
|
|
pub async fn get_ics_feed(db: &SqlitePool) -> String {
|
|
|
|
let mut res = String::from(
|
|
|
|
r#"BEGIN:VCALENDAR
|
|
|
|
VERSION:2.0
|
2023-05-24 15:44:43 +02:00
|
|
|
PRODID:-//rudernlinz.at//Trips//DE
|
|
|
|
X-WR-CALNAME:Ruderausfahrten"#,
|
2023-05-24 15:36:38 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let events = PlannedEvent::all(db).await;
|
|
|
|
for event in events {
|
|
|
|
res.push_str("\nBEGIN:VEVENT");
|
|
|
|
res.push_str(&format!("\nUID:{}@rudernlinz.at", event.id));
|
|
|
|
res.push_str(&format!(
|
|
|
|
"\nDTSTART;TZID=Europe/Vienna:{}T{}00",
|
|
|
|
event.day.replace("-", ""),
|
|
|
|
event.planned_starting_time.replace(":", "")
|
|
|
|
));
|
|
|
|
res.push_str(&format!("\nSUMMARY:{}", event.name));
|
|
|
|
|
|
|
|
res.push_str("\nEND:VEVENT");
|
|
|
|
}
|
|
|
|
|
|
|
|
res.push_str("\nEND:VCALENDAR");
|
|
|
|
res
|
|
|
|
}
|
2023-04-26 12:21:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
2023-04-29 18:57:01 +02:00
|
|
|
PlannedEvent::create(&pool, "new-event".into(), 2, 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
|
|
|
}
|
2023-05-24 15:36:38 +02:00
|
|
|
|
|
|
|
#[sqlx::test]
|
|
|
|
fn test_ics() {
|
|
|
|
let pool = testdb!();
|
|
|
|
|
|
|
|
let actual = PlannedEvent::get_ics_feed(&pool).await;
|
2023-05-28 20:05:51 +02:00
|
|
|
assert_eq!("BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//rudernlinz.at//Trips//DE\nX-WR-CALNAME:Ruderausfahrten\nBEGIN:VEVENT\nUID:1@rudernlinz.at\nDTSTART;TZID=Europe/Vienna:19700101T100000\nSUMMARY:test-planned-event\nEND:VEVENT\nEND:VCALENDAR", actual);
|
2023-05-24 15:36:38 +02:00
|
|
|
}
|
2023-04-04 12:19:56 +02:00
|
|
|
}
|