From aa2d5bc7dd5dd1d64be84bebdcf26238a227bda7 Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 24 May 2023 15:09:38 +0200 Subject: [PATCH] add first draft of cal --- src/model/planned_event.rs | 18 +++++++++++++++--- src/rest/cal.rs | 33 +++++++++++++++++++++++++++++++++ src/rest/mod.rs | 2 ++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/rest/cal.rs diff --git a/src/model/planned_event.rs b/src/model/planned_event.rs index 829cc11..e672fbe 100644 --- a/src/model/planned_event.rs +++ b/src/model/planned_event.rs @@ -7,12 +7,12 @@ use super::{tripdetails::TripDetails, triptype::TripType, user::User}; #[derive(Serialize, Clone, FromRow)] pub struct PlannedEvent { pub id: i64, - name: String, + pub name: String, planned_amount_cox: i64, trip_details_id: i64, - planned_starting_time: String, + pub planned_starting_time: String, max_people: i64, - day: String, + pub day: String, notes: Option, pub allow_guests: bool, trip_type_id: Option, @@ -89,6 +89,18 @@ WHERE day=?", ret } + pub async fn all(db: &SqlitePool) -> Vec { + 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 + } + async fn get_all_cox(&self, db: &SqlitePool) -> Vec { //TODO: switch to join sqlx::query_as!( diff --git a/src/rest/cal.rs b/src/rest/cal.rs new file mode 100644 index 0000000..5f5c6fc --- /dev/null +++ b/src/rest/cal.rs @@ -0,0 +1,33 @@ +use rocket::{get, routes, Route, State}; +use sqlx::SqlitePool; + +use crate::model::planned_event::PlannedEvent; + +#[get("/")] +async fn index(db: &State) -> String { + let mut res = String::from( + r#"BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//rudernlinz.at//Trips//DE"#, + ); + + 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:{}T{}Z", + event.day, event.planned_starting_time + )); + res.push_str(&format!("\nSUMMARY:{}", event.name)); + + res.push_str("\nEND:VEVENT"); + } + + res.push_str("\nEND:VCALENDAR"); + res +} + +pub fn routes() -> Vec { + routes![index] +} diff --git a/src/rest/mod.rs b/src/rest/mod.rs index 794c6e2..1004a5f 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -21,6 +21,7 @@ use crate::model::{ mod admin; mod auth; +mod cal; mod cox; mod faq; @@ -143,6 +144,7 @@ pub fn start(db: SqlitePool) -> Rocket { .mount("/cox", cox::routes()) .mount("/admin", admin::routes()) .mount("/faq", faq::routes()) + .mount("/cal", cal::routes()) .mount("/public", FileServer::from("static/")) .register("/", catchers![unauthorized_error]) .attach(Template::fairing())