use actix_web::{http::header::LOCATION, web, HttpResponse, Responder}; use chrono::{NaiveDate, NaiveTime}; use garde::Validate; use serde::Deserialize; use sqlx::PgPool; use crate::{ models::{Event, EventChangeset, Location, Role, User}, utils::{self, ApplicationError}, }; #[derive(Deserialize)] pub struct NewEventForm { name: String, date: NaiveDate, from: NaiveTime, till: NaiveTime, location: i32, voluntarywachhabender: Option, voluntaryfuehrungsassistent: Option, amount: i16, clothing: String, note: Option, } #[actix_web::post("/events/new")] pub async fn post( user: web::ReqData, pool: web::Data, form: web::Form, ) -> Result { if user.role != Role::Admin && user.role != Role::AreaManager { return Err(ApplicationError::Unauthorized); } let Some(location) = Location::read_by_id(pool.get_ref(), form.location).await? else { return Ok(HttpResponse::BadRequest().body("Location does not exist")); }; if user.role != Role::Admin && user.area_id != location.area_id { return Ok(HttpResponse::BadRequest().body("Can't use location outside of your area")); } let changeset = EventChangeset { date: form.date, amount_of_posten: form.amount, clothing: form.clothing.clone(), location_id: form.location, time: (form.from, form.till), name: form.name.clone(), note: form .note .clone() .and_then(|n| if !n.is_empty() { Some(n) } else { None }), voluntary_wachhabender: form.voluntarywachhabender.unwrap_or(false), voluntary_fuehrungsassistent: form.voluntaryfuehrungsassistent.unwrap_or(false), }; if let Err(e) = changeset.validate_with(&None) { return Ok(HttpResponse::BadRequest().body(e.to_string())); }; Event::create(pool.get_ref(), changeset).await?; let url = utils::get_return_url_for_date(&form.date); println!("redirecto to {url}"); Ok(HttpResponse::Found() .insert_header((LOCATION, url.clone())) .insert_header(("HX-LOCATION", url)) .finish()) }