From 2f6464ff84a35814165cbe226d95eba53e59a6a3 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 16 Jul 2023 19:16:42 +0200 Subject: [PATCH] separate rest from tera --- src/lib.rs | 3 ++ src/rest/mod.rs | 123 +----------------------------------------------- src/tera/mod.rs | 1 - 3 files changed, 5 insertions(+), 122 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 78160a9..fbc4b0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,9 @@ pub mod model; #[cfg(feature = "rowing-tera")] pub mod tera; +#[cfg(feature = "rest")] +pub mod rest; + #[cfg(test)] #[macro_export] macro_rules! testdb { diff --git a/src/rest/mod.rs b/src/rest/mod.rs index 7a4e6bc..aaf4bba 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -1,126 +1,7 @@ -use rocket::{ - catch, catchers, - fairing::AdHoc, - fs::FileServer, - get, - request::FlashMessage, - response::{Flash, Redirect}, - routes, Build, Rocket, State, -}; -use rocket_dyn_templates::{tera::Context, Template}; -use serde::Deserialize; -use sqlx::SqlitePool; - -use crate::model::{ - log::Log, - tripdetails::TripDetails, - triptype::TripType, - user::User, - usertrip::{UserTrip, UserTripError}, -}; - -mod admin; -mod auth; -mod cox; -mod misc; - -#[get("/")] -async fn index(db: &State, user: User, flash: Option>) -> Template { - let mut context = Context::new(); - - if user.is_cox || user.is_admin { - let triptypes = TripType::all(db).await; - context.insert("trip_types", &triptypes); - } - - let days = user.get_days(db).await; - - 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/")] -async fn join(db: &State, trip_details_id: i64, user: User) -> Flash { - let Some(trip_details) = TripDetails::find_by_id(db, trip_details_id).await else { return Flash::error(Redirect::to("/"), "Trip_details do not exist.") }; - - match UserTrip::create(db, &user, &trip_details).await { - Ok(_) => { - Log::create( - db, - format!( - "User {} registered for trip_details.id={}", - user.name, trip_details_id - ), - ) - .await; - Flash::success(Redirect::to("/"), "Erfolgreich angemeldet!") - } - 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!") - } - Err(UserTripError::CantRegisterAtOwnEvent) => Flash::error( - Redirect::to("/"), - "Du kannst bei einer selbst ausgeschriebenen Fahrt nicht mitrudern ;)", - ), - Err(UserTripError::GuestNotAllowedForThisEvent) => Flash::error( - Redirect::to("/"), - "Bei dieser Ausfahrt können leider keine Gäste mitfahren.", - ), - } -} - -#[get("/remove/")] -async fn remove(db: &State, trip_details_id: i64, user: User) -> Flash { - let Some(trip_details) = TripDetails::find_by_id(db, trip_details_id).await else { - return Flash::error(Redirect::to("/"), "TripDetailsId does not exist"); - }; - - UserTrip::delete(db, &user, &trip_details).await; - - Log::create( - db, - format!( - "User {} unregistered for trip_details.id={}", - user.name, trip_details_id - ), - ) - .await; - - Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!") -} - -#[catch(401)] //unauthorized -fn unauthorized_error() -> Redirect { - Redirect::to("/auth") -} - -#[derive(Deserialize)] -#[serde(crate = "rocket::serde")] -pub struct Config { - rss_key: String, -} +use rocket::{fs::FileServer, Build, Rocket}; pub fn config(rocket: Rocket) -> Rocket { - rocket - .mount("/", routes![index, join, remove]) - .mount("/auth", auth::routes()) - .mount("/cox", cox::routes()) - .mount("/admin", admin::routes()) - .mount("/", misc::routes()) - .mount("/public", FileServer::from("static/")) - .register("/", catchers![unauthorized_error]) - .attach(Template::fairing()) - .attach(AdHoc::config::()) + rocket.mount("/beta", FileServer::from("svelte/build")) } //#[cfg(test)] diff --git a/src/tera/mod.rs b/src/tera/mod.rs index a86a159..7a4e6bc 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -118,7 +118,6 @@ pub fn config(rocket: Rocket) -> Rocket { .mount("/admin", admin::routes()) .mount("/", misc::routes()) .mount("/public", FileServer::from("static/")) - .mount("/beta", FileServer::from("svelte/build")) .register("/", catchers![unauthorized_error]) .attach(Template::fairing()) .attach(AdHoc::config::())