rowt/src/rest/cox.rs

59 lines
1.6 KiB
Rust
Raw Normal View History

2023-04-04 15:16:21 +02:00
use rocket::{
form::Form,
get, post,
response::{Flash, Redirect},
routes, FromForm, Route, State,
};
use sqlx::SqlitePool;
use crate::model::{trip::Trip, tripdetails::TripDetails, user::CoxUser};
//TODO: add constraints (e.g. planned_amount_cox > 0)
#[derive(FromForm)]
struct AddTripForm {
day: String,
planned_starting_time: String,
max_people: i32,
notes: Option<String>,
}
#[post("/trip", data = "<data>")]
async fn create(db: &State<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) -> Flash<Redirect> {
//TODO: fix clones()
2023-04-04 19:49:27 +02:00
let trip_details_id = TripDetails::create(
2023-04-04 15:16:21 +02:00
db,
data.planned_starting_time.clone(),
data.max_people,
data.day.clone(),
data.notes.clone(),
)
.await;
//TODO: fix clone()
Trip::new_own(db, cox.id, trip_details_id).await;
Flash::success(Redirect::to("/"), "Successfully planned the event")
}
#[get("/join/<planned_event_id>")]
async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
if Trip::new_join(db, cox.id, planned_event_id).await {
Flash::success(Redirect::to("/"), "Danke für's helfen!")
} else {
Flash::error(Redirect::to("/"), "Du nimmst bereits teil!")
}
}
#[get("/remove/<planned_event_id>")]
async fn remove(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
//TODO: Check if > 2 hrs to event
Trip::delete(db, cox.id, planned_event_id).await;
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
}
pub fn routes() -> Vec<Route> {
routes![create, join, remove]
}