This commit is contained in:
2023-04-04 15:16:21 +02:00
parent 0bdd073d7f
commit cedaba5709
10 changed files with 453 additions and 28 deletions

58
src/rest/cox.rs Normal file
View File

@ -0,0 +1,58 @@
use rocket::{
form::Form,
get, post,
response::{Flash, Redirect},
routes, FromForm, Route, State,
};
use sqlx::SqlitePool;
use crate::model::{trip::Trip, tripdetails::TripDetails, user::CoxUser};
//TODO: add constraints (e.g. planned_amount_cox > 0)
#[derive(FromForm)]
struct AddTripForm {
day: String,
planned_starting_time: String,
max_people: i32,
notes: Option<String>,
}
#[post("/trip", data = "<data>")]
async fn create(db: &State<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) -> Flash<Redirect> {
//TODO: fix clones()
let trip_details_id = TripDetails::new(
db,
data.planned_starting_time.clone(),
data.max_people,
data.day.clone(),
data.notes.clone(),
)
.await;
//TODO: fix clone()
Trip::new_own(db, cox.id, trip_details_id).await;
Flash::success(Redirect::to("/"), "Successfully planned the event")
}
#[get("/join/<planned_event_id>")]
async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
if Trip::new_join(db, cox.id, planned_event_id).await {
Flash::success(Redirect::to("/"), "Danke für's helfen!")
} else {
Flash::error(Redirect::to("/"), "Du nimmst bereits teil!")
}
}
#[get("/remove/<planned_event_id>")]
async fn remove(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
//TODO: Check if > 2 hrs to event
Trip::delete(db, cox.id, planned_event_id).await;
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
}
pub fn routes() -> Vec<Route> {
routes![create, join, remove]
}

View File

@ -1,21 +1,57 @@
use chrono::{Duration, Local, NaiveDate};
use rocket::{catch, catchers, get, response::Redirect, routes, Build, Rocket, State};
use rocket_dyn_templates::{context, Template};
use chrono::{Duration, Local};
use rocket::{
catch, catchers, get,
request::FlashMessage,
response::{Flash, Redirect},
routes, Build, Rocket, State,
};
use rocket_dyn_templates::{context, tera::Context, Template};
use sqlx::SqlitePool;
use crate::model::{user::User, Day};
use crate::model::{planned_event::PlannedEvent, user::User, usertrip::UserTrip, Day};
mod admin;
mod auth;
mod cox;
#[get("/")]
async fn index(db: &State<SqlitePool>, user: User) -> Template {
async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_>>) -> Template {
let mut days = Vec::new();
for i in 0..6 {
let date = (Local::now() + Duration::days(i)).date_naive();
days.push(Day::new(db, date).await);
}
Template::render("index", context! {loggedin_user: user, days})
let mut context = Context::new();
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> {
if !PlannedEvent::rower_can_register(db, trip_details_id).await {
return Flash::error(Redirect::to("/"), "Bereits ausgebucht!");
}
if UserTrip::new(db, user.id, trip_details_id).await {
Flash::success(Redirect::to("/"), "Erfolgreich angemeldet!")
} else {
Flash::error(Redirect::to("/"), "Du nimmst bereits teil!")
}
}
#[get("/remove/<trip_details_id>")]
async fn remove(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
//TODO: Check if > 2 hrs to event
UserTrip::delete(db, user.id, trip_details_id).await;
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
}
#[catch(401)] //unauthorized
@ -26,8 +62,9 @@ fn unauthorized_error() -> Redirect {
pub fn start(db: SqlitePool) -> Rocket<Build> {
rocket::build()
.manage(db)
.mount("/", routes![index])
.mount("/", routes![index, join, remove])
.mount("/auth", auth::routes())
.mount("/cox", cox::routes())
.mount("/admin", admin::routes())
.register("/", catchers![unauthorized_error])
.attach(Template::fairing())