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, } #[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("/"), "Successfully planned the event") } #[get("/join/")] async fn join(db: &State, planned_event_id: i64, cox: CoxUser) -> Flash { 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/")] async fn remove(db: &State, planned_event_id: i64, cox: CoxUser) -> Flash { //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 { routes![create, join, remove] }