use actix_identity::Identity; use actix_web::{http::header::LOCATION, web, HttpResponse, Responder}; use chrono::{NaiveDate, NaiveTime}; use serde::Deserialize; use sqlx::PgPool; use crate::models::{Event, Role, User}; #[derive(Deserialize)] pub struct NewEventForm { name: String, date: NaiveDate, from: NaiveTime, till: NaiveTime, location: i32, voluntarywachhabender: Option, amount: i16, clothing: String, } #[actix_web::post("/events/new")] pub async fn post( user: Identity, pool: web::Data, form: web::Form, ) -> impl Responder { let current_user = User::read_by_id(pool.get_ref(), user.id().unwrap().parse().unwrap()) .await .unwrap(); if current_user.role != Role::Admin && current_user.role != Role::AreaManager { return HttpResponse::Unauthorized().finish(); } let return_location = format!("/?data={}", form.date); match Event::create( pool.get_ref(), &form.date, &form.from, &form.till, &form.name, form.location, form.voluntarywachhabender.is_some() && form.voluntarywachhabender.unwrap(), form.amount, &form.clothing, ) .await { Ok(_) => HttpResponse::Found().insert_header((LOCATION, return_location)).finish(), Err(_) => HttpResponse::BadRequest().body("Fehler beim Erstellen") } }