From 2375dd91dec5db3fbc367b165b30f1756382ca65 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 16 Jul 2023 18:42:59 +0200 Subject: [PATCH] rename folder --- Cargo.toml | 3 +- src/main.rs | 3 + src/rest/mod.rs | 149 ++++++++++++++++++ {rot_app => svelte}/.eslintignore | 0 {rot_app => svelte}/.eslintrc.cjs | 0 {rot_app => svelte}/.gitignore | 0 {rot_app => svelte}/.npmrc | 0 {rot_app => svelte}/.prettierignore | 0 {rot_app => svelte}/.prettierrc | 0 {rot_app => svelte}/README.md | 0 {rot_app => svelte}/package-lock.json | 0 {rot_app => svelte}/package.json | 0 {rot_app => svelte}/playwright.config.ts | 0 {rot_app => svelte}/postcss.config.js | 0 {rot_app => svelte}/src/+layout.js/.ts | 0 {rot_app => svelte}/src/app.css | 0 {rot_app => svelte}/src/app.d.ts | 0 {rot_app => svelte}/src/app.html | 0 {rot_app => svelte}/src/lib/images/github.svg | 0 .../src/lib/images/svelte-logo.svg | 0 .../src/lib/images/svelte-welcome.png | Bin .../src/lib/images/svelte-welcome.webp | Bin {rot_app => svelte}/src/routes/+layout.svelte | 0 {rot_app => svelte}/src/routes/+page.svelte | 0 {rot_app => svelte}/src/routes/+page.ts | 0 {rot_app => svelte}/src/routes/Counter.svelte | 0 {rot_app => svelte}/src/routes/Header.svelte | 0 {rot_app => svelte}/src/routes/Login.svelte | 0 .../src/routes/about/+page.svelte | 0 {rot_app => svelte}/src/routes/about/+page.ts | 0 .../src/routes/admin/+page.svelte | 0 {rot_app => svelte}/src/routes/admin/+page.ts | 0 .../src/routes/faq/+page.svelte | 0 {rot_app => svelte}/src/routes/faq/+page.ts | 0 {rot_app => svelte}/src/routes/faq/FAQ.svelte | 0 .../src/routes/sverdle/+page.server.ts | 0 .../src/routes/sverdle/+page.svelte | 0 .../src/routes/sverdle/game.test.ts | 0 .../src/routes/sverdle/game.ts | 0 .../routes/sverdle/how-to-play/+page.svelte | 0 .../src/routes/sverdle/how-to-play/+page.ts | 0 .../src/routes/sverdle/reduced-motion.ts | 0 .../src/routes/sverdle/words.server.ts | 0 .../src/routes/test/+page.svelte | 0 {rot_app => svelte}/src/routes/test/+page.ts | 0 {rot_app => svelte}/src/stores/User.js | 0 {rot_app => svelte}/static/favicon.ico | Bin {rot_app => svelte}/static/robots.txt | 0 {rot_app => svelte}/svelte.config.js | 0 {rot_app => svelte}/tailwind.config.js | 0 {rot_app => svelte}/tests/test.ts | 0 {rot_app => svelte}/tsconfig.json | 0 {rot_app => svelte}/vite.config.ts | 0 53 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/rest/mod.rs rename {rot_app => svelte}/.eslintignore (100%) rename {rot_app => svelte}/.eslintrc.cjs (100%) rename {rot_app => svelte}/.gitignore (100%) rename {rot_app => svelte}/.npmrc (100%) rename {rot_app => svelte}/.prettierignore (100%) rename {rot_app => svelte}/.prettierrc (100%) rename {rot_app => svelte}/README.md (100%) rename {rot_app => svelte}/package-lock.json (100%) rename {rot_app => svelte}/package.json (100%) rename {rot_app => svelte}/playwright.config.ts (100%) rename {rot_app => svelte}/postcss.config.js (100%) rename {rot_app => svelte}/src/+layout.js/.ts (100%) rename {rot_app => svelte}/src/app.css (100%) rename {rot_app => svelte}/src/app.d.ts (100%) rename {rot_app => svelte}/src/app.html (100%) rename {rot_app => svelte}/src/lib/images/github.svg (100%) rename {rot_app => svelte}/src/lib/images/svelte-logo.svg (100%) rename {rot_app => svelte}/src/lib/images/svelte-welcome.png (100%) rename {rot_app => svelte}/src/lib/images/svelte-welcome.webp (100%) rename {rot_app => svelte}/src/routes/+layout.svelte (100%) rename {rot_app => svelte}/src/routes/+page.svelte (100%) rename {rot_app => svelte}/src/routes/+page.ts (100%) rename {rot_app => svelte}/src/routes/Counter.svelte (100%) rename {rot_app => svelte}/src/routes/Header.svelte (100%) rename {rot_app => svelte}/src/routes/Login.svelte (100%) rename {rot_app => svelte}/src/routes/about/+page.svelte (100%) rename {rot_app => svelte}/src/routes/about/+page.ts (100%) rename {rot_app => svelte}/src/routes/admin/+page.svelte (100%) rename {rot_app => svelte}/src/routes/admin/+page.ts (100%) rename {rot_app => svelte}/src/routes/faq/+page.svelte (100%) rename {rot_app => svelte}/src/routes/faq/+page.ts (100%) rename {rot_app => svelte}/src/routes/faq/FAQ.svelte (100%) rename {rot_app => svelte}/src/routes/sverdle/+page.server.ts (100%) rename {rot_app => svelte}/src/routes/sverdle/+page.svelte (100%) rename {rot_app => svelte}/src/routes/sverdle/game.test.ts (100%) rename {rot_app => svelte}/src/routes/sverdle/game.ts (100%) rename {rot_app => svelte}/src/routes/sverdle/how-to-play/+page.svelte (100%) rename {rot_app => svelte}/src/routes/sverdle/how-to-play/+page.ts (100%) rename {rot_app => svelte}/src/routes/sverdle/reduced-motion.ts (100%) rename {rot_app => svelte}/src/routes/sverdle/words.server.ts (100%) rename {rot_app => svelte}/src/routes/test/+page.svelte (100%) rename {rot_app => svelte}/src/routes/test/+page.ts (100%) rename {rot_app => svelte}/src/stores/User.js (100%) rename {rot_app => svelte}/static/favicon.ico (100%) rename {rot_app => svelte}/static/robots.txt (100%) rename {rot_app => svelte}/svelte.config.js (100%) rename {rot_app => svelte}/tailwind.config.js (100%) rename {rot_app => svelte}/tests/test.ts (100%) rename {rot_app => svelte}/tsconfig.json (100%) rename {rot_app => svelte}/vite.config.ts (100%) diff --git a/Cargo.toml b/Cargo.toml index 4d24841..82053c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,9 @@ version = "0.1.0" edition = "2021" [features] -default = ["rowing-tera"] +default = ["rowing-tera", "rest"] rowing-tera = ["rocket_dyn_templates", "tera"] +rest = [] [dependencies] rocket = { version = "0.5.0-rc.3", features = ["secrets"]} diff --git a/src/main.rs b/src/main.rs index e677f93..e5afec2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,5 +26,8 @@ async fn rocket() -> _ { #[cfg(feature = "rowing-tera")] let rocket = tera::config(rocket); + #[cfg(feature = "rest")] + let rocket = rest::config(rocket); + rocket } diff --git a/src/rest/mod.rs b/src/rest/mod.rs new file mode 100644 index 0000000..7a4e6bc --- /dev/null +++ b/src/rest/mod.rs @@ -0,0 +1,149 @@ +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, +} + +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::()) +} + +//#[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"); +// } +//} diff --git a/rot_app/.eslintignore b/svelte/.eslintignore similarity index 100% rename from rot_app/.eslintignore rename to svelte/.eslintignore diff --git a/rot_app/.eslintrc.cjs b/svelte/.eslintrc.cjs similarity index 100% rename from rot_app/.eslintrc.cjs rename to svelte/.eslintrc.cjs diff --git a/rot_app/.gitignore b/svelte/.gitignore similarity index 100% rename from rot_app/.gitignore rename to svelte/.gitignore diff --git a/rot_app/.npmrc b/svelte/.npmrc similarity index 100% rename from rot_app/.npmrc rename to svelte/.npmrc diff --git a/rot_app/.prettierignore b/svelte/.prettierignore similarity index 100% rename from rot_app/.prettierignore rename to svelte/.prettierignore diff --git a/rot_app/.prettierrc b/svelte/.prettierrc similarity index 100% rename from rot_app/.prettierrc rename to svelte/.prettierrc diff --git a/rot_app/README.md b/svelte/README.md similarity index 100% rename from rot_app/README.md rename to svelte/README.md diff --git a/rot_app/package-lock.json b/svelte/package-lock.json similarity index 100% rename from rot_app/package-lock.json rename to svelte/package-lock.json diff --git a/rot_app/package.json b/svelte/package.json similarity index 100% rename from rot_app/package.json rename to svelte/package.json diff --git a/rot_app/playwright.config.ts b/svelte/playwright.config.ts similarity index 100% rename from rot_app/playwright.config.ts rename to svelte/playwright.config.ts diff --git a/rot_app/postcss.config.js b/svelte/postcss.config.js similarity index 100% rename from rot_app/postcss.config.js rename to svelte/postcss.config.js diff --git a/rot_app/src/+layout.js/.ts b/svelte/src/+layout.js/.ts similarity index 100% rename from rot_app/src/+layout.js/.ts rename to svelte/src/+layout.js/.ts diff --git a/rot_app/src/app.css b/svelte/src/app.css similarity index 100% rename from rot_app/src/app.css rename to svelte/src/app.css diff --git a/rot_app/src/app.d.ts b/svelte/src/app.d.ts similarity index 100% rename from rot_app/src/app.d.ts rename to svelte/src/app.d.ts diff --git a/rot_app/src/app.html b/svelte/src/app.html similarity index 100% rename from rot_app/src/app.html rename to svelte/src/app.html diff --git a/rot_app/src/lib/images/github.svg b/svelte/src/lib/images/github.svg similarity index 100% rename from rot_app/src/lib/images/github.svg rename to svelte/src/lib/images/github.svg diff --git a/rot_app/src/lib/images/svelte-logo.svg b/svelte/src/lib/images/svelte-logo.svg similarity index 100% rename from rot_app/src/lib/images/svelte-logo.svg rename to svelte/src/lib/images/svelte-logo.svg diff --git a/rot_app/src/lib/images/svelte-welcome.png b/svelte/src/lib/images/svelte-welcome.png similarity index 100% rename from rot_app/src/lib/images/svelte-welcome.png rename to svelte/src/lib/images/svelte-welcome.png diff --git a/rot_app/src/lib/images/svelte-welcome.webp b/svelte/src/lib/images/svelte-welcome.webp similarity index 100% rename from rot_app/src/lib/images/svelte-welcome.webp rename to svelte/src/lib/images/svelte-welcome.webp diff --git a/rot_app/src/routes/+layout.svelte b/svelte/src/routes/+layout.svelte similarity index 100% rename from rot_app/src/routes/+layout.svelte rename to svelte/src/routes/+layout.svelte diff --git a/rot_app/src/routes/+page.svelte b/svelte/src/routes/+page.svelte similarity index 100% rename from rot_app/src/routes/+page.svelte rename to svelte/src/routes/+page.svelte diff --git a/rot_app/src/routes/+page.ts b/svelte/src/routes/+page.ts similarity index 100% rename from rot_app/src/routes/+page.ts rename to svelte/src/routes/+page.ts diff --git a/rot_app/src/routes/Counter.svelte b/svelte/src/routes/Counter.svelte similarity index 100% rename from rot_app/src/routes/Counter.svelte rename to svelte/src/routes/Counter.svelte diff --git a/rot_app/src/routes/Header.svelte b/svelte/src/routes/Header.svelte similarity index 100% rename from rot_app/src/routes/Header.svelte rename to svelte/src/routes/Header.svelte diff --git a/rot_app/src/routes/Login.svelte b/svelte/src/routes/Login.svelte similarity index 100% rename from rot_app/src/routes/Login.svelte rename to svelte/src/routes/Login.svelte diff --git a/rot_app/src/routes/about/+page.svelte b/svelte/src/routes/about/+page.svelte similarity index 100% rename from rot_app/src/routes/about/+page.svelte rename to svelte/src/routes/about/+page.svelte diff --git a/rot_app/src/routes/about/+page.ts b/svelte/src/routes/about/+page.ts similarity index 100% rename from rot_app/src/routes/about/+page.ts rename to svelte/src/routes/about/+page.ts diff --git a/rot_app/src/routes/admin/+page.svelte b/svelte/src/routes/admin/+page.svelte similarity index 100% rename from rot_app/src/routes/admin/+page.svelte rename to svelte/src/routes/admin/+page.svelte diff --git a/rot_app/src/routes/admin/+page.ts b/svelte/src/routes/admin/+page.ts similarity index 100% rename from rot_app/src/routes/admin/+page.ts rename to svelte/src/routes/admin/+page.ts diff --git a/rot_app/src/routes/faq/+page.svelte b/svelte/src/routes/faq/+page.svelte similarity index 100% rename from rot_app/src/routes/faq/+page.svelte rename to svelte/src/routes/faq/+page.svelte diff --git a/rot_app/src/routes/faq/+page.ts b/svelte/src/routes/faq/+page.ts similarity index 100% rename from rot_app/src/routes/faq/+page.ts rename to svelte/src/routes/faq/+page.ts diff --git a/rot_app/src/routes/faq/FAQ.svelte b/svelte/src/routes/faq/FAQ.svelte similarity index 100% rename from rot_app/src/routes/faq/FAQ.svelte rename to svelte/src/routes/faq/FAQ.svelte diff --git a/rot_app/src/routes/sverdle/+page.server.ts b/svelte/src/routes/sverdle/+page.server.ts similarity index 100% rename from rot_app/src/routes/sverdle/+page.server.ts rename to svelte/src/routes/sverdle/+page.server.ts diff --git a/rot_app/src/routes/sverdle/+page.svelte b/svelte/src/routes/sverdle/+page.svelte similarity index 100% rename from rot_app/src/routes/sverdle/+page.svelte rename to svelte/src/routes/sverdle/+page.svelte diff --git a/rot_app/src/routes/sverdle/game.test.ts b/svelte/src/routes/sverdle/game.test.ts similarity index 100% rename from rot_app/src/routes/sverdle/game.test.ts rename to svelte/src/routes/sverdle/game.test.ts diff --git a/rot_app/src/routes/sverdle/game.ts b/svelte/src/routes/sverdle/game.ts similarity index 100% rename from rot_app/src/routes/sverdle/game.ts rename to svelte/src/routes/sverdle/game.ts diff --git a/rot_app/src/routes/sverdle/how-to-play/+page.svelte b/svelte/src/routes/sverdle/how-to-play/+page.svelte similarity index 100% rename from rot_app/src/routes/sverdle/how-to-play/+page.svelte rename to svelte/src/routes/sverdle/how-to-play/+page.svelte diff --git a/rot_app/src/routes/sverdle/how-to-play/+page.ts b/svelte/src/routes/sverdle/how-to-play/+page.ts similarity index 100% rename from rot_app/src/routes/sverdle/how-to-play/+page.ts rename to svelte/src/routes/sverdle/how-to-play/+page.ts diff --git a/rot_app/src/routes/sverdle/reduced-motion.ts b/svelte/src/routes/sverdle/reduced-motion.ts similarity index 100% rename from rot_app/src/routes/sverdle/reduced-motion.ts rename to svelte/src/routes/sverdle/reduced-motion.ts diff --git a/rot_app/src/routes/sverdle/words.server.ts b/svelte/src/routes/sverdle/words.server.ts similarity index 100% rename from rot_app/src/routes/sverdle/words.server.ts rename to svelte/src/routes/sverdle/words.server.ts diff --git a/rot_app/src/routes/test/+page.svelte b/svelte/src/routes/test/+page.svelte similarity index 100% rename from rot_app/src/routes/test/+page.svelte rename to svelte/src/routes/test/+page.svelte diff --git a/rot_app/src/routes/test/+page.ts b/svelte/src/routes/test/+page.ts similarity index 100% rename from rot_app/src/routes/test/+page.ts rename to svelte/src/routes/test/+page.ts diff --git a/rot_app/src/stores/User.js b/svelte/src/stores/User.js similarity index 100% rename from rot_app/src/stores/User.js rename to svelte/src/stores/User.js diff --git a/rot_app/static/favicon.ico b/svelte/static/favicon.ico similarity index 100% rename from rot_app/static/favicon.ico rename to svelte/static/favicon.ico diff --git a/rot_app/static/robots.txt b/svelte/static/robots.txt similarity index 100% rename from rot_app/static/robots.txt rename to svelte/static/robots.txt diff --git a/rot_app/svelte.config.js b/svelte/svelte.config.js similarity index 100% rename from rot_app/svelte.config.js rename to svelte/svelte.config.js diff --git a/rot_app/tailwind.config.js b/svelte/tailwind.config.js similarity index 100% rename from rot_app/tailwind.config.js rename to svelte/tailwind.config.js diff --git a/rot_app/tests/test.ts b/svelte/tests/test.ts similarity index 100% rename from rot_app/tests/test.ts rename to svelte/tests/test.ts diff --git a/rot_app/tsconfig.json b/svelte/tsconfig.json similarity index 100% rename from rot_app/tsconfig.json rename to svelte/tsconfig.json diff --git a/rot_app/vite.config.ts b/svelte/vite.config.ts similarity index 100% rename from rot_app/vite.config.ts rename to svelte/vite.config.ts