54 lines
1.6 KiB
Rust
54 lines
1.6 KiB
Rust
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<User>,
|
|
pool: web::Data<PgPool>,
|
|
path: web::Path<IdPath>,
|
|
form: web::Form<AvailabillityForm>,
|
|
) -> 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")
|
|
}
|