162 lines
5.2 KiB
Rust
Raw Normal View History

2023-04-04 15:16:21 +02:00
use rocket::{
form::Form,
get, post,
response::{Flash, Redirect},
routes, FromForm, Route, State,
};
use sqlx::SqlitePool;
use crate::model::{
2023-04-18 12:10:11 +02:00
log::Log,
2023-04-26 16:54:53 +02:00
planned_event::PlannedEvent,
2023-04-07 11:54:56 +02:00
trip::{CoxHelpError, Trip, TripDeleteError, TripUpdateError},
tripdetails::TripDetails,
user::CoxUser,
};
2023-04-04 15:16:21 +02:00
#[derive(FromForm)]
struct AddTripForm {
day: String,
2023-05-03 14:39:53 +02:00
//TODO: properly parse `planned_starting_time`
2023-04-04 15:16:21 +02:00
planned_starting_time: String,
2023-05-03 14:39:53 +02:00
#[field(validate = range(1..))]
2023-04-04 15:16:21 +02:00
max_people: i32,
notes: Option<String>,
2023-04-28 21:19:51 +02:00
trip_type: Option<i64>,
allow_guests: bool,
2023-04-04 15:16:21 +02:00
}
#[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(),
data.allow_guests,
2023-04-28 21:19:51 +02:00
data.trip_type,
2023-04-04 15:16:21 +02:00
)
.await;
2023-04-26 16:54:53 +02:00
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;
2023-04-04 15:16:21 +02:00
//TODO: fix clone()
2023-04-18 12:10:11 +02:00
Log::create(
db,
format!(
"Cox {} created trip on {} @ {} for {} rower",
cox.name,
data.day.clone(),
data.planned_starting_time.clone(),
data.max_people,
),
)
.await;
2023-04-07 11:54:56 +02:00
Flash::success(Redirect::to("/"), "Ausfahrt erfolgreich erstellt.")
}
#[derive(FromForm)]
struct EditTripForm {
max_people: i32,
notes: Option<String>,
}
#[post("/trip/<trip_id>", data = "<data>")]
async fn update(
db: &State<SqlitePool>,
data: Form<EditTripForm>,
trip_id: i64,
cox: CoxUser,
) -> Flash<Redirect> {
2023-04-26 16:54:53 +02:00
if let Some(trip) = Trip::find_by_id(db, trip_id).await {
match Trip::update_own(db, &cox, &trip, data.max_people, data.notes.clone()).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")
}
2023-04-07 11:54:56 +02:00
}
2023-04-26 16:54:53 +02:00
} else {
Flash::error(Redirect::to("/"), "Ausfahrt gibt's nicht")
2023-04-07 11:54:56 +02:00
}
2023-04-04 15:16:21 +02:00
}
#[get("/join/<planned_event_id>")]
async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
2023-04-26 16:54:53 +02:00
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!",
),
2023-04-18 12:10:11 +02:00
}
2023-04-26 16:54:53 +02:00
} else {
Flash::error(Redirect::to("/"), "Event gibt's nicht")
2023-04-04 15:16:21 +02:00
}
}
#[get("/remove/trip/<trip_id>")]
async fn remove_trip(db: &State<SqlitePool>, trip_id: i64, cox: CoxUser) -> Flash<Redirect> {
2023-04-26 16:54:53 +02:00
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!")
}
},
}
}
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-26 16:54:53 +02:00
if let Some(planned_event) = PlannedEvent::find_by_id(db, planned_event_id).await {
Trip::delete_by_planned_event(db, &cox, &planned_event).await;
2023-04-04 15:16:21 +02:00
2023-04-26 16:54:53 +02:00
Log::create(
db,
format!(
"Cox {} deleted registration for planned_event.id={}",
cox.name, planned_event_id
),
)
.await;
2023-04-18 12:10:11 +02:00
2023-04-26 16:54:53 +02:00
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
} else {
Flash::error(Redirect::to("/"), "Planned_event does not exist.")
}
2023-04-04 15:16:21 +02:00
}
pub fn routes() -> Vec<Route> {
2023-04-07 11:54:56 +02:00
routes![create, join, remove, remove_trip, update]
2023-04-04 15:16:21 +02:00
}