rowt/src/rest/admin/planned_event.rs

66 lines
1.6 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>,
}
#[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(),
)
.await;
//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,
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> {
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]
}