hacky-ruadat/src/model/planned_event.rs

314 lines
9.6 KiB
Rust
Raw Normal View History

2023-06-05 14:43:31 +02:00
use std::io::Write;
2023-04-04 12:19:56 +02:00
use chrono::NaiveDate;
2023-06-05 14:43:31 +02:00
use ics::{
2023-06-06 10:09:48 +02:00
properties::{DtStart, Summary},
2023-06-05 14:43:31 +02:00
Event, ICalendar,
};
2023-04-04 12:19:56 +02:00
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>,
pub allow_guests: bool,
2023-06-08 10:57:42 +02:00
always_show: 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)]
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-06-08 10:57:42 +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, always_show
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-06-08 10:57:42 +02:00
pub async fn get_pinned_for_day(
db: &SqlitePool,
day: NaiveDate,
) -> Vec<PlannedEventWithUserAndTriptype> {
let mut events = Self::get_for_day(db, day).await;
events.retain(|e| e.planned_event.always_show);
events
}
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-06-08 10:57:42 +02:00
"SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, always_show, 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-06-08 10:57:42 +02:00
pub async fn pinned_days(db: &SqlitePool, amount_days_to_skip: i64) -> Vec<NaiveDate> {
let query = format!(
"SELECT DISTINCT day
FROM planned_event
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
WHERE always_show=true AND day > datetime('now' , '+{} days')
ORDER BY day;",
amount_days_to_skip
);
let days: Vec<String> = sqlx::query_scalar(&query).fetch_all(db).await.unwrap();
days.into_iter()
.map(|a| NaiveDate::parse_from_str(&a, "%Y-%m-%d").unwrap())
.collect()
}
2023-05-24 15:09:38 +02:00
pub async fn all(db: &SqlitePool) -> Vec<PlannedEvent> {
sqlx::query_as!(
PlannedEvent,
2023-06-08 10:57:42 +02:00
"SELECT planned_event.id, planned_event.name, planned_amount_cox, trip_details_id, planned_starting_time, always_show, max_people, day, notes, allow_guests, trip_type_id
2023-05-24 15:09:38 +02:00
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-06-08 10:57:42 +02:00
always_show: bool,
2023-04-26 17:02:47 +02:00
trip_details: TripDetails,
2023-04-04 12:19:56 +02:00
) {
sqlx::query!(
2023-06-08 10:57:42 +02:00
"INSERT INTO planned_event(name, planned_amount_cox, trip_details_id, always_show) VALUES(?, ?, ?, ?)",
name,
planned_amount_cox,
2023-06-08 10:57:42 +02:00
trip_details.id,
always_show
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-06-08 11:28:25 +02:00
always_show: bool,
2023-05-03 22:40:22 +02:00
) {
sqlx::query!(
2023-06-08 11:28:25 +02:00
"UPDATE planned_event SET planned_amount_cox = ?, always_show=? WHERE id = ?",
2023-05-03 22:40:22 +02:00
planned_amount_cox,
2023-06-08 11:28:25 +02:00
always_show,
2023-05-03 22:40:22 +02:00
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 {
2023-06-05 14:43:31 +02:00
let mut calendar = ICalendar::new("2.0", "ics-rs");
2023-05-24 15:36:38 +02:00
let events = PlannedEvent::all(db).await;
for event in events {
2023-06-05 14:43:31 +02:00
let mut vevent = Event::new(format!("{}@rudernlinz.at", event.id), "19900101T180000");
vevent.push(DtStart::new(format!(
2023-06-05 14:50:01 +02:00
"{}T{}00",
2023-05-30 14:36:23 +02:00
event.day.replace('-', ""),
event.planned_starting_time.replace(':', "")
2023-06-05 14:43:31 +02:00
)));
vevent.push(Summary::new(event.name));
calendar.add_event(vevent);
2023-05-24 15:36:38 +02:00
}
2023-06-05 14:43:31 +02:00
let mut buf = Vec::new();
write!(&mut buf, "{}", calendar).unwrap();
String::from_utf8(buf).unwrap()
2023-05-24 15:36:38 +02:00
}
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-06-08 10:57:42 +02:00
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
}
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-06-06 12:26:48 +02:00
assert_eq!("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:ics-rs\r\nBEGIN:VEVENT\r\nUID:1@rudernlinz.at\r\nDTSTAMP:19900101T180000\r\nDTSTART:19700101T100000\r\nSUMMARY:test-planned-event\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", actual);
2023-05-24 15:36:38 +02:00
}
2023-04-04 12:19:56 +02:00
}