2023-04-04 15:16:21 +02:00
|
|
|
use rocket::{
|
|
|
|
form::Form,
|
|
|
|
get, post,
|
|
|
|
response::{Flash, Redirect},
|
|
|
|
routes, FromForm, Route, State,
|
|
|
|
};
|
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
2023-04-05 21:49:48 +02:00
|
|
|
use crate::model::{
|
2023-04-07 11:16:39 +02:00
|
|
|
trip::{CoxHelpError, Trip, TripDeleteError},
|
2023-04-05 21:49:48 +02:00
|
|
|
tripdetails::TripDetails,
|
|
|
|
user::CoxUser,
|
|
|
|
};
|
2023-04-04 15:16:21 +02:00
|
|
|
|
|
|
|
//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()
|
2023-04-04 19:49:27 +02:00
|
|
|
let trip_details_id = TripDetails::create(
|
2023-04-04 15:16:21 +02:00
|
|
|
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> {
|
2023-04-05 21:49:48 +02:00
|
|
|
match Trip::new_join(db, cox.id, planned_event_id).await {
|
|
|
|
Ok(_) => 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!",
|
|
|
|
),
|
2023-04-04 15:16:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:16:39 +02:00
|
|
|
#[get("/remove/trip/<trip_id>")]
|
|
|
|
async fn remove_trip(db: &State<SqlitePool>, trip_id: i64, cox: CoxUser) -> Flash<Redirect> {
|
|
|
|
match Trip::delete(db, cox.id, trip_id).await {
|
|
|
|
Ok(_) => Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!"),
|
|
|
|
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!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 15:16:21 +02:00
|
|
|
#[get("/remove/<planned_event_id>")]
|
|
|
|
async fn remove(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
|
2023-04-07 11:16:39 +02:00
|
|
|
Trip::delete_by_planned_event_id(db, cox.id, planned_event_id).await;
|
2023-04-04 15:16:21 +02:00
|
|
|
|
|
|
|
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn routes() -> Vec<Route> {
|
2023-04-07 11:16:39 +02:00
|
|
|
routes![create, join, remove, remove_trip]
|
2023-04-04 15:16:21 +02:00
|
|
|
}
|