add planned_trip functionality
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
use rocket::Route;
|
||||
|
||||
pub mod planned_event;
|
||||
pub mod user;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
let mut ret = Vec::new();
|
||||
ret.append(&mut user::routes());
|
||||
ret.append(&mut planned_event::routes());
|
||||
ret
|
||||
}
|
||||
|
61
src/rest/admin/planned_event.rs
Normal file
61
src/rest/admin/planned_event.rs
Normal file
@ -0,0 +1,61 @@
|
||||
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]
|
||||
}
|
@ -38,7 +38,11 @@ struct UserEditForm {
|
||||
}
|
||||
|
||||
#[post("/user", data = "<data>")]
|
||||
async fn update(db: &State<SqlitePool>, data: Form<UserEditForm>) -> Flash<Redirect> {
|
||||
async fn update(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<UserEditForm>,
|
||||
_admin: AdminUser,
|
||||
) -> Flash<Redirect> {
|
||||
let user = User::find_by_id(db, data.id).await;
|
||||
let user = match user {
|
||||
Ok(user) => user,
|
||||
|
Reference in New Issue
Block a user