use rocket::{ form::Form, get, post, put, response::{Flash, Redirect}, routes, FromForm, Route, State, }; use sqlx::SqlitePool; use crate::model::{planned_event::PlannedEvent, tripdetails::TripDetails, user::AdminUser}; //TODO: add constraints (e.g. planned_amount_cox > 0) #[derive(FromForm)] struct AddPlannedEventForm<'r> { day: &'r str, name: &'r str, planned_amount_cox: i32, allow_guests: bool, planned_starting_time: &'r str, max_people: i32, notes: Option<&'r str>, trip_type: Option, } #[post("/planned-event", data = "")] async fn create( db: &State, data: Form>, _admin: AdminUser, ) -> Flash { let trip_details_id = TripDetails::create( db, data.planned_starting_time, data.max_people, data.day, data.notes, data.allow_guests, data.trip_type, ) .await; let trip_details = TripDetails::find_by_id(db, trip_details_id).await.unwrap(); //Okay, bc. we //just created //the object PlannedEvent::create(db, data.name, data.planned_amount_cox, trip_details).await; Flash::success(Redirect::to("/"), "Successfully planned the event") } //TODO: add constraints (e.g. planned_amount_cox > 0) #[derive(FromForm)] struct UpdatePlannedEventForm<'r> { id: i64, planned_amount_cox: i32, max_people: i32, notes: Option<&'r str>, } #[put("/planned-event", data = "")] async fn update( db: &State, data: Form>, _admin: AdminUser, ) -> Flash { match PlannedEvent::find_by_id(db, data.id).await { Some(planned_event) => { planned_event .update(db, data.planned_amount_cox, data.max_people, data.notes) .await; Flash::success(Redirect::to("/"), "Successfully edited the event") } None => Flash::error(Redirect::to("/"), "Planned event id not found"), } } #[get("/planned-event//delete")] async fn delete(db: &State, id: i64, _admin: AdminUser) -> Flash { match PlannedEvent::find_by_id(db, id).await { Some(planned_event) => { planned_event.delete(db).await; Flash::success(Redirect::to("/"), "Successfully deleted the event") } None => Flash::error(Redirect::to("/"), "PlannedEvent does not exist"), } } pub fn routes() -> Vec { routes![create, delete, update] }