2023-03-23 10:26:18 +01:00

44 lines
1.2 KiB
Rust

use rocket::{form::Form, response::Redirect, Route, State};
use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set};
use crate::models::day;
use super::NaiveDateForm;
#[derive(FromForm, Debug)]
struct DayForm {
day: NaiveDateForm,
#[field(validate = range(0..20))]
planned_amount_cox: i32,
planned_starting_time: Option<String>,
open_registration: bool,
}
#[put("/", data = "<day>")]
async fn create(db: &State<DatabaseConnection>, day: Form<DayForm>) -> Redirect {
let new_day = day::ActiveModel {
day: Set(*day.day),
planned_amount_cox: Set(day.planned_amount_cox),
planned_starting_time: Set(day.planned_starting_time.clone()),
open_registration: Set(day.open_registration),
};
let day: Option<day::Model> = day::Entity::find_by_id(*day.day)
.one(db.inner())
.await
.unwrap();
if let Some(day) = day {
log::info!("{:?} got updated to {:?}", day, new_day);
new_day.update(db.inner()).await.unwrap(); //TODO: fixme
} else {
log::info!("{:?} got inserted", new_day);
new_day.insert(db.inner()).await.unwrap(); //TODO: fixme
}
Redirect::to("/")
}
pub fn routes() -> Vec<Route> {
routes![create]
}