2023-04-04 15:38:47 +02:00
|
|
|
use chrono::{Datelike, Duration, Local, NaiveDate};
|
2023-04-04 15:16:21 +02:00
|
|
|
use rocket::{
|
2023-04-05 19:24:02 +02:00
|
|
|
catch, catchers,
|
2023-05-30 14:12:08 +02:00
|
|
|
fairing::AdHoc,
|
2023-04-05 19:24:02 +02:00
|
|
|
fs::FileServer,
|
|
|
|
get,
|
2023-04-04 15:16:21 +02:00
|
|
|
request::FlashMessage,
|
|
|
|
response::{Flash, Redirect},
|
|
|
|
routes, Build, Rocket, State,
|
|
|
|
};
|
2023-04-04 15:38:47 +02:00
|
|
|
use rocket_dyn_templates::{tera::Context, Template};
|
2023-05-30 14:12:08 +02:00
|
|
|
use serde::Deserialize;
|
2023-04-03 16:11:26 +02:00
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
2023-04-05 22:01:50 +02:00
|
|
|
use crate::model::{
|
2023-04-18 12:10:11 +02:00
|
|
|
log::Log,
|
2023-04-28 19:08:17 +02:00
|
|
|
tripdetails::TripDetails,
|
2023-04-28 21:19:51 +02:00
|
|
|
triptype::TripType,
|
2023-04-05 22:01:50 +02:00
|
|
|
user::User,
|
|
|
|
usertrip::{UserTrip, UserTripError},
|
|
|
|
Day,
|
|
|
|
};
|
2023-04-03 22:03:45 +02:00
|
|
|
|
2023-04-04 10:44:14 +02:00
|
|
|
mod admin;
|
2023-04-03 16:11:26 +02:00
|
|
|
mod auth;
|
2023-04-04 15:16:21 +02:00
|
|
|
mod cox;
|
2023-05-24 15:36:38 +02:00
|
|
|
mod misc;
|
2023-03-26 14:40:56 +02:00
|
|
|
|
2023-05-03 13:32:23 +02:00
|
|
|
fn amount_days_to_show(is_cox: bool) -> i64 {
|
|
|
|
if is_cox {
|
2023-05-24 13:11:47 +02:00
|
|
|
let end_of_year = NaiveDate::from_ymd_opt(Local::now().year(), 12, 31).unwrap(); //Ok,
|
|
|
|
//december
|
|
|
|
//has 31
|
|
|
|
//days
|
2023-05-03 13:32:23 +02:00
|
|
|
end_of_year
|
|
|
|
.signed_duration_since(Local::now().date_naive())
|
|
|
|
.num_days()
|
|
|
|
+ 1
|
|
|
|
} else {
|
|
|
|
6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 15:42:26 +02:00
|
|
|
#[get("/")]
|
|
|
|
async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_>>) -> Template {
|
2023-04-04 12:19:56 +02:00
|
|
|
let mut days = Vec::new();
|
2023-04-04 15:38:47 +02:00
|
|
|
|
2023-04-28 21:19:51 +02:00
|
|
|
let mut context = Context::new();
|
|
|
|
|
|
|
|
if user.is_cox || user.is_admin {
|
|
|
|
let triptypes = TripType::all(db).await;
|
|
|
|
context.insert("trip_types", &triptypes);
|
|
|
|
}
|
2023-04-04 15:38:47 +02:00
|
|
|
|
2023-05-03 13:32:23 +02:00
|
|
|
let show_next_n_days = amount_days_to_show(user.is_cox);
|
2023-04-07 12:06:04 +02:00
|
|
|
for i in 0..show_next_n_days {
|
2023-04-04 12:19:56 +02:00
|
|
|
let date = (Local::now() + Duration::days(i)).date_naive();
|
2023-04-29 18:57:01 +02:00
|
|
|
|
|
|
|
if user.is_guest {
|
|
|
|
days.push(Day::new_guest(db, date).await);
|
|
|
|
} else {
|
|
|
|
days.push(Day::new(db, date).await);
|
|
|
|
}
|
2023-04-04 12:19:56 +02:00
|
|
|
}
|
2023-04-04 15:16:21 +02:00
|
|
|
|
|
|
|
if let Some(msg) = flash {
|
|
|
|
context.insert("flash", &msg.into_inner());
|
|
|
|
}
|
|
|
|
context.insert("loggedin_user", &user);
|
|
|
|
context.insert("days", &days);
|
|
|
|
Template::render("index", context.into_json())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/join/<trip_details_id>")]
|
|
|
|
async fn join(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
|
2023-05-30 14:47:44 +02:00
|
|
|
let Some(trip_details) = TripDetails::find_by_id(db, trip_details_id).await else { return Flash::error(Redirect::to("/"), "Trip_details do not exist.") };
|
|
|
|
|
2023-04-28 19:08:17 +02:00
|
|
|
match UserTrip::create(db, &user, &trip_details).await {
|
2023-04-18 12:10:11 +02:00
|
|
|
Ok(_) => {
|
|
|
|
Log::create(
|
|
|
|
db,
|
|
|
|
format!(
|
|
|
|
"User {} registered for trip_details.id={}",
|
|
|
|
user.name, trip_details_id
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
Flash::success(Redirect::to("/"), "Erfolgreich angemeldet!")
|
|
|
|
}
|
2023-04-05 22:01:50 +02:00
|
|
|
Err(UserTripError::EventAlreadyFull) => {
|
|
|
|
Flash::error(Redirect::to("/"), "Event bereits ausgebucht!")
|
|
|
|
}
|
|
|
|
Err(UserTripError::AlreadyRegistered) => {
|
|
|
|
Flash::error(Redirect::to("/"), "Du nimmst bereits teil!")
|
|
|
|
}
|
|
|
|
Err(UserTripError::AlreadyRegisteredAsCox) => {
|
|
|
|
Flash::error(Redirect::to("/"), "Du hilfst bereits als Steuerperson aus!")
|
|
|
|
}
|
2023-04-28 19:08:17 +02:00
|
|
|
Err(UserTripError::CantRegisterAtOwnEvent) => Flash::error(
|
|
|
|
Redirect::to("/"),
|
|
|
|
"Du kannst bei einer selbst ausgeschriebenen Fahrt nicht mitrudern ;)",
|
|
|
|
),
|
2023-04-29 18:57:01 +02:00
|
|
|
Err(UserTripError::GuestNotAllowedForThisEvent) => Flash::error(
|
|
|
|
Redirect::to("/"),
|
|
|
|
"Bei dieser Ausfahrt können leider keine Gäste mitfahren.",
|
|
|
|
),
|
2023-04-04 15:16:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/remove/<trip_details_id>")]
|
|
|
|
async fn remove(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
|
2023-05-30 14:47:44 +02:00
|
|
|
let Some(trip_details) = TripDetails::find_by_id(db, trip_details_id).await else {
|
|
|
|
return Flash::error(Redirect::to("/"), "TripDetailsId does not exist");
|
|
|
|
};
|
2023-04-28 19:08:17 +02:00
|
|
|
|
|
|
|
UserTrip::delete(db, &user, &trip_details).await;
|
2023-04-04 15:16:21 +02:00
|
|
|
|
2023-04-18 12:10:11 +02:00
|
|
|
Log::create(
|
|
|
|
db,
|
|
|
|
format!(
|
|
|
|
"User {} unregistered for trip_details.id={}",
|
|
|
|
user.name, trip_details_id
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2023-04-04 15:16:21 +02:00
|
|
|
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
|
2023-03-26 14:40:56 +02:00
|
|
|
}
|
|
|
|
|
2023-04-03 22:03:45 +02:00
|
|
|
#[catch(401)] //unauthorized
|
|
|
|
fn unauthorized_error() -> Redirect {
|
|
|
|
Redirect::to("/auth")
|
|
|
|
}
|
|
|
|
|
2023-05-30 14:12:08 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(crate = "rocket::serde")]
|
|
|
|
pub struct Config {
|
|
|
|
rss_key: String,
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:11:26 +02:00
|
|
|
pub fn start(db: SqlitePool) -> Rocket<Build> {
|
2023-03-26 16:58:45 +02:00
|
|
|
rocket::build()
|
2023-04-03 16:11:26 +02:00
|
|
|
.manage(db)
|
2023-04-04 15:16:21 +02:00
|
|
|
.mount("/", routes![index, join, remove])
|
2023-04-03 16:11:26 +02:00
|
|
|
.mount("/auth", auth::routes())
|
2023-04-04 15:16:21 +02:00
|
|
|
.mount("/cox", cox::routes())
|
2023-04-04 10:44:14 +02:00
|
|
|
.mount("/admin", admin::routes())
|
2023-05-24 15:36:38 +02:00
|
|
|
.mount("/", misc::routes())
|
2023-04-10 15:15:16 +02:00
|
|
|
.mount("/public", FileServer::from("static/"))
|
2023-04-03 22:03:45 +02:00
|
|
|
.register("/", catchers![unauthorized_error])
|
2023-03-26 16:58:45 +02:00
|
|
|
.attach(Template::fairing())
|
2023-05-30 14:12:08 +02:00
|
|
|
.attach(AdHoc::config::<Config>())
|
2023-03-26 14:40:56 +02:00
|
|
|
}
|
|
|
|
|
2023-04-10 15:24:54 +02:00
|
|
|
//#[cfg(test)]
|
|
|
|
//mod test {
|
|
|
|
// use crate::testdb;
|
|
|
|
//
|
|
|
|
// use super::start;
|
|
|
|
// use rocket::http::Status;
|
|
|
|
// use rocket::local::asynchronous::Client;
|
|
|
|
// use rocket::uri;
|
|
|
|
// use sqlx::SqlitePool;
|
|
|
|
//
|
|
|
|
// #[sqlx::test]
|
|
|
|
// fn test_not_logged_in() {
|
|
|
|
// let pool = testdb!();
|
|
|
|
//
|
|
|
|
// let client = Client::tracked(start(pool))
|
|
|
|
// .await
|
|
|
|
// .expect("valid rocket instance");
|
|
|
|
// let response = client.get(uri!(super::index)).dispatch().await;
|
|
|
|
//
|
|
|
|
// assert_eq!(response.status(), Status::SeeOther);
|
|
|
|
// let location = response.headers().get("Location").next().unwrap();
|
|
|
|
// assert_eq!(location, "/auth");
|
|
|
|
// }
|
|
|
|
//}
|