use rocket::{ form::Form, get, post, response::{Flash, Redirect}, routes, FromForm, Route, State, }; use sqlx::SqlitePool; use crate::model::{ trip::{CoxHelpError, Trip, TripDeleteError, TripUpdateError}, 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, } #[post("/trip", data = "")] async fn create(db: &State, data: Form, cox: CoxUser) -> Flash { //TODO: fix clones() let trip_details_id = TripDetails::create( 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("/"), "Ausfahrt erfolgreich erstellt.") } #[derive(FromForm)] struct EditTripForm { max_people: i32, notes: Option, } #[post("/trip/", data = "")] async fn update( db: &State, data: Form, trip_id: i64, cox: CoxUser, ) -> Flash { match Trip::update_own(db, cox.id, trip_id, 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::TripDoesNotExist) => { Flash::error(Redirect::to("/"), "Ausfahrt gibt's nicht") } } } #[get("/join/")] async fn join(db: &State, planned_event_id: i64, cox: CoxUser) -> Flash { 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!", ), } } #[get("/remove/trip/")] async fn remove_trip(db: &State, trip_id: i64, cox: CoxUser) -> Flash { 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!") } } } #[get("/remove/")] async fn remove(db: &State, planned_event_id: i64, cox: CoxUser) -> Flash { Trip::delete_by_planned_event_id(db, cox.id, planned_event_id).await; Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!") } pub fn routes() -> Vec { routes![create, join, remove, remove_trip, update] }