use actix_web::{http::header::LOCATION, web, HttpResponse, Responder}; use sqlx::PgPool; use crate::{ endpoints::{availability::post_new::AvailabillityForm, IdPath}, models::{Availabillity, User}, }; #[actix_web::post("/availabillity/edit/{id}")] pub async fn post( user: web::ReqData, pool: web::Data, path: web::Path, form: web::Form, ) -> impl Responder { if let Ok(mut availabillity) = Availabillity::read_by_id(pool.get_ref(), path.id).await { if availabillity.user_id == user.id { let mut has_changed = false; if availabillity.start_time != form.from { availabillity.start_time = form.from; has_changed = true; } if availabillity.end_time != form.till { availabillity.end_time = form.till; has_changed = true; } if availabillity.comment != form.comment { availabillity.comment = form.comment.clone(); has_changed = true; } if has_changed { if let Ok(_) = Availabillity::update(pool.get_ref(), path.id, &availabillity).await { return HttpResponse::Found() .insert_header((LOCATION, "/")) .finish(); } } if !has_changed { return HttpResponse::Found() .insert_header((LOCATION, "/")) .finish(); } } } HttpResponse::BadRequest().body("Fehler beim erstellen") }