2023-04-04 12:19:56 +02:00
|
|
|
use rocket::{
|
|
|
|
form::Form,
|
2023-05-03 22:40:22 +02:00
|
|
|
get, post, put,
|
2023-04-04 12:19:56 +02:00
|
|
|
response::{Flash, Redirect},
|
|
|
|
routes, FromForm, Route, State,
|
|
|
|
};
|
2023-07-25 13:22:11 +02:00
|
|
|
use serde::Serialize;
|
2023-04-04 12:19:56 +02:00
|
|
|
use sqlx::SqlitePool;
|
|
|
|
|
2023-07-25 13:22:11 +02:00
|
|
|
use crate::model::{
|
|
|
|
planned_event::PlannedEvent,
|
|
|
|
tripdetails::{TripDetails, TripDetailsToAdd},
|
|
|
|
user::AdminUser,
|
|
|
|
};
|
2023-04-04 12:19:56 +02:00
|
|
|
|
|
|
|
//TODO: add constraints (e.g. planned_amount_cox > 0)
|
2023-07-25 13:22:11 +02:00
|
|
|
#[derive(FromForm, Serialize)]
|
2023-05-24 12:11:55 +02:00
|
|
|
struct AddPlannedEventForm<'r> {
|
|
|
|
name: &'r str,
|
2023-04-04 12:19:56 +02:00
|
|
|
planned_amount_cox: i32,
|
2023-07-25 13:22:11 +02:00
|
|
|
tripdetails: TripDetailsToAdd<'r>,
|
2023-04-04 12:19:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/planned-event", data = "<data>")]
|
|
|
|
async fn create(
|
|
|
|
db: &State<SqlitePool>,
|
2023-05-24 12:11:55 +02:00
|
|
|
data: Form<AddPlannedEventForm<'_>>,
|
2023-04-04 12:19:56 +02:00
|
|
|
_admin: AdminUser,
|
|
|
|
) -> Flash<Redirect> {
|
2023-07-25 13:22:11 +02:00
|
|
|
let data = data.into_inner();
|
|
|
|
|
|
|
|
let trip_details_id = TripDetails::create(db, data.tripdetails).await;
|
2023-04-04 12:19:56 +02:00
|
|
|
|
2023-04-26 17:02:47 +02:00
|
|
|
let trip_details = TripDetails::find_by_id(db, trip_details_id).await.unwrap(); //Okay, bc. we
|
|
|
|
//just created
|
|
|
|
//the object
|
|
|
|
|
2023-07-23 19:45:48 +02:00
|
|
|
PlannedEvent::create(db, data.name, data.planned_amount_cox, trip_details).await;
|
2023-04-04 12:19:56 +02:00
|
|
|
|
|
|
|
Flash::success(Redirect::to("/"), "Successfully planned the event")
|
|
|
|
}
|
|
|
|
|
2023-05-03 22:40:22 +02:00
|
|
|
//TODO: add constraints (e.g. planned_amount_cox > 0)
|
|
|
|
#[derive(FromForm)]
|
2023-05-24 12:11:55 +02:00
|
|
|
struct UpdatePlannedEventForm<'r> {
|
2023-05-03 22:40:22 +02:00
|
|
|
id: i64,
|
|
|
|
planned_amount_cox: i32,
|
|
|
|
max_people: i32,
|
2023-05-24 12:11:55 +02:00
|
|
|
notes: Option<&'r str>,
|
2023-06-08 11:28:25 +02:00
|
|
|
always_show: bool,
|
2023-05-03 22:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/planned-event", data = "<data>")]
|
|
|
|
async fn update(
|
|
|
|
db: &State<SqlitePool>,
|
2023-05-24 12:11:55 +02:00
|
|
|
data: Form<UpdatePlannedEventForm<'_>>,
|
2023-05-03 22:40:22 +02:00
|
|
|
_admin: AdminUser,
|
|
|
|
) -> Flash<Redirect> {
|
|
|
|
match PlannedEvent::find_by_id(db, data.id).await {
|
|
|
|
Some(planned_event) => {
|
|
|
|
planned_event
|
2023-06-08 11:28:25 +02:00
|
|
|
.update(
|
|
|
|
db,
|
|
|
|
data.planned_amount_cox,
|
|
|
|
data.max_people,
|
|
|
|
data.notes,
|
|
|
|
data.always_show,
|
|
|
|
)
|
2023-05-03 22:40:22 +02:00
|
|
|
.await;
|
|
|
|
Flash::success(Redirect::to("/"), "Successfully edited the event")
|
|
|
|
}
|
|
|
|
None => Flash::error(Redirect::to("/"), "Planned event id not found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 12:19:56 +02:00
|
|
|
#[get("/planned-event/<id>/delete")]
|
|
|
|
async fn delete(db: &State<SqlitePool>, id: i64, _admin: AdminUser) -> Flash<Redirect> {
|
2023-04-26 12:21:30 +02:00
|
|
|
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"),
|
|
|
|
}
|
2023-04-04 12:19:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn routes() -> Vec<Route> {
|
2023-05-03 22:40:22 +02:00
|
|
|
routes![create, delete, update]
|
2023-04-04 12:19:56 +02:00
|
|
|
}
|