rename subfolder

This commit is contained in:
2023-06-28 14:06:28 +02:00
parent 509412599f
commit 0fbdcc084e
11 changed files with 15 additions and 8 deletions

View File

@ -1,23 +0,0 @@
use rocket::{get, routes, Route, State};
use sqlx::SqlitePool;
use crate::{model::log::Log, rest::Config};
pub mod planned_event;
pub mod user;
#[get("/rss?<key>")]
async fn rss(db: &State<SqlitePool>, key: Option<&str>, config: &State<Config>) -> String {
match key {
Some(key) if key.eq(&config.rss_key) => Log::generate_feed(db).await,
_ => "Not allowed".to_string(),
}
}
pub fn routes() -> Vec<Route> {
let mut ret = Vec::new();
ret.append(&mut user::routes());
ret.append(&mut planned_event::routes());
ret.append(&mut routes![rss]);
ret
}

View File

@ -1,104 +0,0 @@
use rocket::{
form::Form,
get, post, put,
response::{Flash, Redirect},
routes, FromForm, Route, State,
};
use sqlx::SqlitePool;
use crate::model::{planned_event::PlannedEvent, tripdetails::TripDetails, user::AdminUser};
//TODO: add constraints (e.g. planned_amount_cox > 0)
#[derive(FromForm)]
struct AddPlannedEventForm<'r> {
day: &'r str,
name: &'r str,
planned_amount_cox: i32,
allow_guests: bool,
planned_starting_time: &'r str,
max_people: i32,
always_show: bool,
notes: Option<&'r str>,
trip_type: Option<i64>,
}
#[post("/planned-event", data = "<data>")]
async fn create(
db: &State<SqlitePool>,
data: Form<AddPlannedEventForm<'_>>,
_admin: AdminUser,
) -> Flash<Redirect> {
let trip_details_id = TripDetails::create(
db,
data.planned_starting_time,
data.max_people,
data.day,
data.notes,
data.allow_guests,
data.trip_type,
)
.await;
let trip_details = TripDetails::find_by_id(db, trip_details_id).await.unwrap(); //Okay, bc. we
//just created
//the object
PlannedEvent::create(
db,
data.name,
data.planned_amount_cox,
data.always_show,
trip_details,
)
.await;
Flash::success(Redirect::to("/"), "Successfully planned the event")
}
//TODO: add constraints (e.g. planned_amount_cox > 0)
#[derive(FromForm)]
struct UpdatePlannedEventForm<'r> {
id: i64,
planned_amount_cox: i32,
max_people: i32,
notes: Option<&'r str>,
always_show: bool,
}
#[put("/planned-event", data = "<data>")]
async fn update(
db: &State<SqlitePool>,
data: Form<UpdatePlannedEventForm<'_>>,
_admin: AdminUser,
) -> Flash<Redirect> {
match PlannedEvent::find_by_id(db, data.id).await {
Some(planned_event) => {
planned_event
.update(
db,
data.planned_amount_cox,
data.max_people,
data.notes,
data.always_show,
)
.await;
Flash::success(Redirect::to("/"), "Successfully edited the event")
}
None => Flash::error(Redirect::to("/"), "Planned event id not found"),
}
}
#[get("/planned-event/<id>/delete")]
async fn delete(db: &State<SqlitePool>, id: i64, _admin: AdminUser) -> Flash<Redirect> {
match PlannedEvent::find_by_id(db, id).await {
Some(planned_event) => {
planned_event.delete(db).await;
Flash::success(Redirect::to("/"), "Successfully deleted the event")
}
None => Flash::error(Redirect::to("/"), "PlannedEvent does not exist"),
}
}
pub fn routes() -> Vec<Route> {
routes![create, delete, update]
}

View File

@ -1,112 +0,0 @@
use crate::model::user::{AdminUser, User};
use rocket::{
form::Form,
get, post,
request::FlashMessage,
response::{Flash, Redirect},
routes, FromForm, Route, State,
};
use rocket_dyn_templates::{tera::Context, Template};
use sqlx::SqlitePool;
#[get("/user")]
async fn index(
db: &State<SqlitePool>,
admin: AdminUser,
flash: Option<FlashMessage<'_>>,
) -> Template {
let users = User::all(db).await;
let mut context = Context::new();
if let Some(msg) = flash {
context.insert("flash", &msg.into_inner());
}
context.insert("users", &users);
context.insert("loggedin_user", &admin.user);
Template::render("admin/user/index", context.into_json())
}
#[get("/user/<user>/reset-pw")]
async fn resetpw(db: &State<SqlitePool>, _admin: AdminUser, user: i32) -> Flash<Redirect> {
let user = User::find_by_id(db, user).await;
match user {
Some(user) => {
user.reset_pw(db).await;
Flash::success(
Redirect::to("/admin/user"),
format!("Successfully reset pw of {}", user.name),
)
}
None => Flash::error(Redirect::to("/admin/user"), "User does not exist"),
}
}
#[get("/user/<user>/delete")]
async fn delete(db: &State<SqlitePool>, _admin: AdminUser, user: i32) -> Flash<Redirect> {
let user = User::find_by_id(db, user).await;
match user {
Some(user) => {
user.delete(db).await;
Flash::success(
Redirect::to("/admin/user"),
format!("Sucessfully deleted user {}", user.name),
)
}
None => Flash::error(Redirect::to("/admin/user"), "User does not exist"),
}
}
#[derive(FromForm)]
struct UserEditForm {
id: i32,
is_guest: bool,
is_cox: bool,
is_admin: bool,
}
#[post("/user", data = "<data>")]
async fn update(
db: &State<SqlitePool>,
data: Form<UserEditForm>,
_admin: AdminUser,
) -> Flash<Redirect> {
let user = User::find_by_id(db, data.id).await;
let Some(user) = user else {
return Flash::error(
Redirect::to("/admin/user"),
format!("User with ID {} does not exist!", data.id),
)
};
user.update(db, data.is_cox, data.is_admin, data.is_guest)
.await;
Flash::success(Redirect::to("/admin/user"), "Successfully updated user")
}
#[derive(FromForm)]
struct UserAddForm<'r> {
name: &'r str,
is_guest: bool,
}
#[post("/user/new", data = "<data>")]
async fn create(
db: &State<SqlitePool>,
data: Form<UserAddForm<'_>>,
_admin: AdminUser,
) -> Flash<Redirect> {
if User::create(db, data.name, data.is_guest).await {
Flash::success(Redirect::to("/admin/user"), "Successfully created user")
} else {
Flash::error(
Redirect::to("/admin/user"),
format!("User {} already exists", data.name),
)
}
}
pub fn routes() -> Vec<Route> {
routes![index, resetpw, update, create, delete]
}

View File

@ -1,122 +0,0 @@
use rocket::{
form::Form,
get,
http::{Cookie, CookieJar},
post,
request::FlashMessage,
response::{Flash, Redirect},
routes,
time::{Duration, OffsetDateTime},
FromForm, Route, State,
};
use rocket_dyn_templates::{context, tera, Template};
use serde_json::json;
use sqlx::SqlitePool;
use crate::model::{
log::Log,
user::{LoginError, User},
};
#[get("/")]
fn index(flash: Option<FlashMessage<'_>>) -> Template {
let mut context = tera::Context::new();
if let Some(msg) = flash {
context.insert("flash", &msg.into_inner());
}
Template::render("auth/login", context.into_json())
}
#[derive(FromForm)]
struct LoginForm<'r> {
name: &'r str,
password: &'r str,
}
#[post("/", data = "<login>")]
async fn login(
login: Form<LoginForm<'_>>,
db: &State<SqlitePool>,
cookies: &CookieJar<'_>,
) -> Flash<Redirect> {
let user = User::login(db, login.name, login.password).await;
let user = match user {
Ok(user) => user,
Err(LoginError::NoPasswordSet(user)) => {
return Flash::warning(
Redirect::to(format!("/auth/set-pw/{}", user.id)),
"Setze ein neues Passwort",
);
}
Err(_) => {
return Flash::error(Redirect::to("/auth"), "Falscher Benutzername/Passwort");
}
};
let user_json: String = format!("{}", json!(user));
cookies.add_private(Cookie::new("loggedin_user", user_json));
Flash::success(Redirect::to("/"), "Login erfolgreich")
}
#[get("/set-pw/<userid>")]
fn setpw(userid: i32) -> Template {
Template::render("auth/set-pw", context!(userid))
}
#[derive(FromForm)]
struct UpdatePw<'r> {
userid: i32,
password: &'r str,
password_confirm: &'r str,
}
#[post("/set-pw", data = "<updatepw>")]
async fn updatepw(
db: &State<SqlitePool>,
updatepw: Form<UpdatePw<'_>>,
cookies: &CookieJar<'_>,
) -> Flash<Redirect> {
let user = User::find_by_id(db, updatepw.userid).await;
let Some(user) = user else{
return Flash::error(
Redirect::to("/auth"),
format!("User with ID {} does not exist!", updatepw.userid),
)
};
if updatepw.password != updatepw.password_confirm {
return Flash::error(
Redirect::to(format!("/auth/set-pw/{}", updatepw.userid)),
"Passwörter stimmen nicht überein! Bitte probiere es nochmal",
);
}
user.update_pw(db, updatepw.password).await;
let user_json: String = format!("{}", json!(user));
let mut cookie = Cookie::new("loggedin_user", user_json);
cookie.set_expires(OffsetDateTime::now_utc() + Duration::weeks(12));
cookies.add_private(cookie);
Log::create(db, format!("User {} set her password.", user.name)).await;
Flash::success(
Redirect::to("/"),
"Passwort erfolgreich gesetzt. Du bist nun eingeloggt.",
)
}
#[get("/logout")]
fn logout(cookies: &CookieJar<'_>, _user: User) -> Flash<Redirect> {
cookies.remove_private(Cookie::named("loggedin_user"));
Flash::success(Redirect::to("/auth"), "Logout erfolgreich")
}
pub fn routes() -> Vec<Route> {
routes![index, login, logout, setpw, updatepw]
}

View File

@ -1,161 +0,0 @@
use rocket::{
form::Form,
get, post,
response::{Flash, Redirect},
routes, FromForm, Route, State,
};
use sqlx::SqlitePool;
use crate::model::{
log::Log,
planned_event::PlannedEvent,
trip::{CoxHelpError, Trip, TripDeleteError, TripUpdateError},
tripdetails::TripDetails,
user::CoxUser,
};
#[derive(FromForm)]
struct AddTripForm<'r> {
day: String,
//TODO: properly parse `planned_starting_time`
planned_starting_time: &'r str,
#[field(validate = range(1..))]
max_people: i32,
notes: Option<&'r str>,
trip_type: Option<i64>,
allow_guests: bool,
}
#[post("/trip", data = "<data>")]
async fn create(
db: &State<SqlitePool>,
data: Form<AddTripForm<'_>>,
cox: CoxUser,
) -> Flash<Redirect> {
let trip_details_id = TripDetails::create(
db,
data.planned_starting_time,
data.max_people,
&data.day,
data.notes,
data.allow_guests,
data.trip_type,
)
.await;
let trip_details = TripDetails::find_by_id(db, trip_details_id).await.unwrap(); //Okay, bc just
//created
Trip::new_own(db, &cox, trip_details).await;
Log::create(
db,
format!(
"Cox {} created trip on {} @ {} for {} rower",
cox.name, data.day, data.planned_starting_time, data.max_people,
),
)
.await;
Flash::success(Redirect::to("/"), "Ausfahrt erfolgreich erstellt.")
}
#[derive(FromForm)]
struct EditTripForm<'r> {
max_people: i32,
notes: Option<&'r str>,
trip_type: Option<i64>,
}
#[post("/trip/<trip_id>", data = "<data>")]
async fn update(
db: &State<SqlitePool>,
data: Form<EditTripForm<'_>>,
trip_id: i64,
cox: CoxUser,
) -> Flash<Redirect> {
if let Some(trip) = Trip::find_by_id(db, trip_id).await {
match Trip::update_own(db, &cox, &trip, data.max_people, data.notes, data.trip_type).await {
Ok(_) => Flash::success(Redirect::to("/"), "Ausfahrt erfolgreich aktualisiert."),
Err(TripUpdateError::NotYourTrip) => {
Flash::error(Redirect::to("/"), "Nicht deine Ausfahrt!")
}
Err(TripUpdateError::TripDetailsDoesNotExist) => {
Flash::error(Redirect::to("/"), "Ausfahrt gibt's nicht")
}
}
} else {
Flash::error(Redirect::to("/"), "Ausfahrt gibt's nicht")
}
}
#[get("/join/<planned_event_id>")]
async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
if let Some(planned_event) = PlannedEvent::find_by_id(db, planned_event_id).await {
match Trip::new_join(db, &cox, &planned_event).await {
Ok(_) => {
Log::create(
db,
format!(
"Cox {} helps at planned_event.id={}",
cox.name, planned_event_id,
),
)
.await;
Flash::success(Redirect::to("/"), "Danke für's helfen!")
}
Err(CoxHelpError::AlreadyRegisteredAsCox) => {
Flash::error(Redirect::to("/"), "Du hilfst bereits aus!")
}
Err(CoxHelpError::AlreadyRegisteredAsRower) => Flash::error(
Redirect::to("/"),
"Du hast dich bereits als Ruderer angemeldet!",
),
}
} else {
Flash::error(Redirect::to("/"), "Event gibt's nicht")
}
}
#[get("/remove/trip/<trip_id>")]
async fn remove_trip(db: &State<SqlitePool>, trip_id: i64, cox: CoxUser) -> Flash<Redirect> {
let trip = Trip::find_by_id(db, trip_id).await;
match trip {
None => Flash::error(Redirect::to("/"), "Trip gibt's nicht!"),
Some(trip) => match trip.delete(db, &cox).await {
Ok(_) => {
Log::create(db, format!("Cox {} deleted trip.id={}", cox.name, trip_id)).await;
Flash::success(Redirect::to("/"), "Erfolgreich gelöscht!")
}
Err(TripDeleteError::SomebodyAlreadyRegistered) => Flash::error(
Redirect::to("/"),
"Ausfahrt kann nicht gelöscht werden, da bereits jemand registriert ist!",
),
Err(TripDeleteError::NotYourTrip) => {
Flash::error(Redirect::to("/"), "Nicht deine Ausfahrt!")
}
},
}
}
#[get("/remove/<planned_event_id>")]
async fn remove(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
if let Some(planned_event) = PlannedEvent::find_by_id(db, planned_event_id).await {
Trip::delete_by_planned_event(db, &cox, &planned_event).await;
Log::create(
db,
format!(
"Cox {} deleted registration for planned_event.id={}",
cox.name, planned_event_id
),
)
.await;
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
} else {
Flash::error(Redirect::to("/"), "Planned_event does not exist.")
}
}
pub fn routes() -> Vec<Route> {
routes![create, join, remove, remove_trip, update]
}

View File

@ -1,19 +0,0 @@
use rocket::{get, http::ContentType, 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<SqlitePool>) -> (ContentType, String) {
(ContentType::Calendar, PlannedEvent::get_ics_feed(db).await)
}
pub fn routes() -> Vec<Route> {
routes![faq, cal]
}

View File

@ -1,150 +0,0 @@
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<SqlitePool>, user: User, flash: Option<FlashMessage<'_>>) -> 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/<trip_details_id>")]
async fn join(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
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/<trip_details_id>")]
async fn remove(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
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 start(db: SqlitePool) -> Rocket<Build> {
rocket::build()
.manage(db)
.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::<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");
// }
//}