rowt/src/rest/admin/planned_event.rs

72 lines
2.0 KiB
Rust
Raw Normal View History

2023-04-04 12:19:56 +02:00
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>,
2023-04-28 21:19:51 +02:00
trip_type: Option<i64>,
2023-04-04 12:19:56 +02:00
}
#[post("/planned-event", data = "<data>")]
async fn create(
db: &State<SqlitePool>,
data: Form<AddPlannedEventForm>,
_admin: AdminUser,
) -> Flash<Redirect> {
//TODO: fix clones()
2023-04-04 19:49:27 +02:00
let trip_details_id = TripDetails::create(
2023-04-04 12:19:56 +02:00
db,
data.planned_starting_time.clone(),
data.max_people,
data.day.clone(),
data.notes.clone(),
2023-04-28 21:19:51 +02:00
data.trip_type,
2023-04-04 12:19:56 +02:00
)
.await;
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-04-04 12:19:56 +02:00
//TODO: fix clone()
2023-04-04 19:49:27 +02:00
PlannedEvent::create(
2023-04-04 12:19:56 +02:00
db,
data.name.clone(),
data.planned_amount_cox,
data.allow_guests,
2023-04-26 17:02:47 +02:00
trip_details,
2023-04-04 12:19:56 +02:00
)
.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> {
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> {
routes![create, delete]
}