62 lines
1.4 KiB
Rust
62 lines
1.4 KiB
Rust
use rocket::{
|
|
form::Form,
|
|
get, post,
|
|
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 {
|
|
day: String,
|
|
name: String,
|
|
planned_amount_cox: i32,
|
|
allow_guests: bool,
|
|
planned_starting_time: String,
|
|
max_people: i32,
|
|
notes: Option<String>,
|
|
}
|
|
|
|
#[post("/planned-event", data = "<data>")]
|
|
async fn create(
|
|
db: &State<SqlitePool>,
|
|
data: Form<AddPlannedEventForm>,
|
|
_admin: AdminUser,
|
|
) -> Flash<Redirect> {
|
|
//TODO: fix clones()
|
|
let trip_details_id = TripDetails::new(
|
|
db,
|
|
data.planned_starting_time.clone(),
|
|
data.max_people,
|
|
data.day.clone(),
|
|
data.notes.clone(),
|
|
)
|
|
.await;
|
|
|
|
//TODO: fix clone()
|
|
PlannedEvent::new(
|
|
db,
|
|
data.name.clone(),
|
|
data.planned_amount_cox,
|
|
data.allow_guests,
|
|
trip_details_id,
|
|
)
|
|
.await;
|
|
|
|
Flash::success(Redirect::to("/"), "Successfully planned the event")
|
|
}
|
|
|
|
#[get("/planned-event/<id>/delete")]
|
|
async fn delete(db: &State<SqlitePool>, id: i64, _admin: AdminUser) -> Flash<Redirect> {
|
|
PlannedEvent::delete(db, id).await;
|
|
|
|
Flash::success(Redirect::to("/"), "Successfully deleted the event")
|
|
}
|
|
|
|
pub fn routes() -> Vec<Route> {
|
|
routes![create, delete]
|
|
}
|