From 05b65b2f67730bfff879171cb7a00e2ec9ab74ab Mon Sep 17 00:00:00 2001 From: philipp Date: Wed, 24 May 2023 15:36:38 +0200 Subject: [PATCH] restructre --- src/model/planned_event.rs | 33 +++++++++++++++++++++++++++++++++ src/rest/admin/mod.rs | 18 +++++++++++++++--- src/rest/admin/rss.rs | 17 ----------------- src/rest/cal.rs | 34 ---------------------------------- src/rest/faq.rs | 13 ------------- src/rest/misc.rs | 19 +++++++++++++++++++ src/rest/mod.rs | 6 ++---- 7 files changed, 69 insertions(+), 71 deletions(-) delete mode 100644 src/rest/admin/rss.rs delete mode 100644 src/rest/cal.rs delete mode 100644 src/rest/faq.rs create mode 100644 src/rest/misc.rs diff --git a/src/model/planned_event.rs b/src/model/planned_event.rs index e672fbe..5b5b0e1 100644 --- a/src/model/planned_event.rs +++ b/src/model/planned_event.rs @@ -205,6 +205,31 @@ FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_even .await .unwrap(); //Okay, as PlannedEvent can only be created with proper DB backing } + + pub async fn get_ics_feed(db: &SqlitePool) -> 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;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 + } } #[cfg(test)] @@ -248,4 +273,12 @@ mod test { PlannedEvent::get_for_day(&pool, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()).await; assert_eq!(res.len(), 0); } + + #[sqlx::test] + fn test_ics() { + let pool = testdb!(); + + let actual = PlannedEvent::get_ics_feed(&pool).await; + assert_eq!("BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//rudernlinz.at//Trips//DE\nBEGIN:VEVENT\nUID:1@rudernlinz.at\nDTSTART;TZID=Europe/Vienna:19700101T100000\nSUMMARY:test-planned-event\nEND:VEVENT\nEND:VCALENDAR", actual); + } } diff --git a/src/rest/admin/mod.rs b/src/rest/admin/mod.rs index 4aacf47..211f0fc 100644 --- a/src/rest/admin/mod.rs +++ b/src/rest/admin/mod.rs @@ -1,13 +1,25 @@ -use rocket::Route; +use rocket::{get, routes, Route, State}; +use sqlx::SqlitePool; + +use crate::model::log::Log; pub mod planned_event; -pub mod rss; pub mod user; +#[get("/rss?")] +async fn rss(db: &State, key: Option<&str>) -> String { + match key { + Some(key) if key.eq("G9h/f2MFEr408IaB4Yd67/maVSsnAJNjcaZ2Tzl5Vo=") => { + Log::generate_feed(db).await + } + _ => "Not allowed".to_string(), + } +} + pub fn routes() -> Vec { let mut ret = Vec::new(); ret.append(&mut user::routes()); ret.append(&mut planned_event::routes()); - ret.append(&mut rss::routes()); + ret.append(&mut routes![rss]); ret } diff --git a/src/rest/admin/rss.rs b/src/rest/admin/rss.rs deleted file mode 100644 index 71d7044..0000000 --- a/src/rest/admin/rss.rs +++ /dev/null @@ -1,17 +0,0 @@ -use crate::rest::Log; -use rocket::{get, routes, Route, State}; -use sqlx::SqlitePool; - -#[get("/rss?")] -async fn index(db: &State, key: Option<&str>) -> String { - match key { - Some(key) if key.eq("G9h/f2MFEr408IaB4Yd67/maVSsnAJNjcaZ2Tzl5Vo=") => { - Log::generate_feed(db).await - } - _ => "Not allowed".to_string(), - } -} - -pub fn routes() -> Vec { - routes![index] -} diff --git a/src/rest/cal.rs b/src/rest/cal.rs deleted file mode 100644 index 9220f26..0000000 --- a/src/rest/cal.rs +++ /dev/null @@ -1,34 +0,0 @@ -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;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 -} - -pub fn routes() -> Vec { - routes![index] -} diff --git a/src/rest/faq.rs b/src/rest/faq.rs deleted file mode 100644 index 155bc12..0000000 --- a/src/rest/faq.rs +++ /dev/null @@ -1,13 +0,0 @@ -use rocket::{get, routes, Route}; -use rocket_dyn_templates::{context, Template}; - -use crate::model::user::User; - -#[get("/")] -async fn index(user: User) -> Template { - Template::render("faq", context!(loggedin_user: user)) -} - -pub fn routes() -> Vec { - routes![index] -} diff --git a/src/rest/misc.rs b/src/rest/misc.rs new file mode 100644 index 0000000..8291b7a --- /dev/null +++ b/src/rest/misc.rs @@ -0,0 +1,19 @@ +use rocket::{get, routes, Route, State}; +use rocket_dyn_templates::{context, Template}; +use sqlx::SqlitePool; + +use crate::model::{planned_event::PlannedEvent, user::User}; + +#[get("/faq")] +async fn faq(user: User) -> Template { + Template::render("faq", context!(loggedin_user: user)) +} + +#[get("/cal")] +async fn cal(db: &State) -> String { + PlannedEvent::get_ics_feed(db).await +} + +pub fn routes() -> Vec { + routes![faq, cal] +} diff --git a/src/rest/mod.rs b/src/rest/mod.rs index 1004a5f..da9fa94 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -21,9 +21,8 @@ use crate::model::{ mod admin; mod auth; -mod cal; mod cox; -mod faq; +mod misc; fn amount_days_to_show(is_cox: bool) -> i64 { if is_cox { @@ -143,8 +142,7 @@ pub fn start(db: SqlitePool) -> Rocket { .mount("/auth", auth::routes()) .mount("/cox", cox::routes()) .mount("/admin", admin::routes()) - .mount("/faq", faq::routes()) - .mount("/cal", cal::routes()) + .mount("/", misc::routes()) .mount("/public", FileServer::from("static/")) .register("/", catchers![unauthorized_error]) .attach(Template::fairing())