diff --git a/src/endpoints/availability/delete.rs b/src/endpoints/availability/delete.rs index eeebed6f..e6f4aaa8 100644 --- a/src/endpoints/availability/delete.rs +++ b/src/endpoints/availability/delete.rs @@ -15,7 +15,7 @@ pub async fn delete( if let Ok(availabillity_in_db) = Availabillity::read_by_id(pool.get_ref(), path.id).await { if availabillity_in_db.user_id == user.id { if let Ok(_) = Availabillity::delete(pool.get_ref(), availabillity_in_db.id).await { - return HttpResponse::NoContent().finish(); + return HttpResponse::Ok().finish(); } } } diff --git a/src/endpoints/availability/get_new.rs b/src/endpoints/availability/get_new.rs index 82530a1b..51efd313 100644 --- a/src/endpoints/availability/get_new.rs +++ b/src/endpoints/availability/get_new.rs @@ -1,25 +1,31 @@ use actix_web::{web, Responder}; -use askama::Template; use askama_actix::TemplateToResponse; use chrono::NaiveDate; +use serde::Deserialize; -use crate::{endpoints::NaiveDateQuery, models::{Role, User}}; +use crate::endpoints::availability::NewOrEditAvailabilityTemplate; +use crate::models::User; -#[derive(Template)] -#[template(path = "availability/new.html")] -struct AvailabillityNewTemplate { - user: User, +#[derive(Deserialize)] +struct AvailabilityNewQuery { + #[serde(rename(deserialize = "wholeday"))] + whole_day: Option, date: NaiveDate, } #[actix_web::get("/availabillity/new")] pub async fn get( user: web::ReqData, - query: web::Query, + query: web::Query, ) -> impl Responder { - let template = AvailabillityNewTemplate { + let template = NewOrEditAvailabilityTemplate { user: user.into_inner(), date: query.date, + whole_day: query.whole_day.unwrap_or(true), + id: None, + start_time: None, + end_time: None, + comment: None, }; template.to_response() diff --git a/src/endpoints/availability/get_update.rs b/src/endpoints/availability/get_update.rs index d7a90568..b6e1c229 100644 --- a/src/endpoints/availability/get_update.rs +++ b/src/endpoints/availability/get_update.rs @@ -1,45 +1,46 @@ use actix_web::{web, HttpResponse, Responder}; use askama_actix::TemplateToResponse; -use chrono::NaiveTime; +use serde::Deserialize; use sqlx::PgPool; use crate::{ - endpoints::{availability::AvailabillityEditTemplate, IdPath}, + endpoints::{availability::NewOrEditAvailabilityTemplate, IdPath}, models::{Availabillity, User}, }; +#[derive(Deserialize)] +struct EditAvailabilityQuery { + #[serde(rename(deserialize = "wholeday"))] + whole_day: Option, +} + #[actix_web::get("/availabillity/edit/{id}")] pub async fn get( user: web::ReqData, pool: web::Data, path: web::Path, + query: web::Query, ) -> impl Responder { if let Ok(availabillity) = Availabillity::read_by_id(pool.get_ref(), path.id).await { if availabillity.user_id == user.id { let start_time = availabillity .start_time - .unwrap_or(NaiveTime::from_hms_opt(0, 0, 0).unwrap()) - .format("%R") - .to_string(); + .and_then(|d| Some(d.format("%R").to_string())); let end_time = availabillity .end_time - .unwrap_or(NaiveTime::from_hms_opt(23, 59, 0).unwrap()) - .format("%R") - .to_string(); + .and_then(|d| Some(d.format("%R").to_string())); let has_time = availabillity.start_time.is_some() && availabillity.end_time.is_some(); - let comment = availabillity.comment.unwrap_or(String::new()); - - let template = AvailabillityEditTemplate { + let template = NewOrEditAvailabilityTemplate { user: user.into_inner(), date: availabillity.date, - id: path.id, - start_time, - end_time, - has_time, - comment, + whole_day: query.whole_day.unwrap_or(!has_time), + id: Some(path.id), + start_time: start_time.as_deref(), + end_time: end_time.as_deref(), + comment: availabillity.comment.as_deref(), }; return template.to_response(); diff --git a/src/endpoints/availability/mod.rs b/src/endpoints/availability/mod.rs index c41b4dd9..16b21b7b 100644 --- a/src/endpoints/availability/mod.rs +++ b/src/endpoints/availability/mod.rs @@ -1,23 +1,24 @@ use askama::Template; use chrono::NaiveDate; -use crate::models::{User,Role}; +use crate::filters; +use crate::models::{Role, User}; pub mod delete; pub mod get_new; -pub mod get_update; pub mod get_overview; +pub mod get_update; pub mod post_new; pub mod post_update; #[derive(Template)] -#[template(path = "availability/edit.html")] -struct AvailabillityEditTemplate { +#[template(path = "availability/new_or_edit.html")] +struct NewOrEditAvailabilityTemplate<'a> { user: User, date: NaiveDate, - id: i32, - start_time: String, - end_time: String, - has_time: bool, - comment: String, + whole_day: bool, + id: Option, + start_time: Option<&'a str>, + end_time: Option<&'a str>, + comment: Option<&'a str>, } diff --git a/src/endpoints/availability/post_update.rs b/src/endpoints/availability/post_update.rs index b5d53020..7c79409a 100644 --- a/src/endpoints/availability/post_update.rs +++ b/src/endpoints/availability/post_update.rs @@ -40,6 +40,12 @@ pub async fn post( .finish(); } } + + if !has_changed { + return HttpResponse::Found() + .insert_header((LOCATION, "/")) + .finish(); + } } } diff --git a/src/filters.rs b/src/filters.rs index 3ff50fef..0a30cd70 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -11,3 +11,15 @@ pub fn show_area_query(a: &Option, first: bool) -> ::askama::Result return Ok(String::new()); } } + +pub fn cond_show(show: &bool, text: &str) -> askama::Result { + return if *show { + Ok(String::from(text)) + } else { + Ok(String::new()) + } +} + +pub fn invert(b: &bool) -> askama::Result { + return Ok(!b); +} diff --git a/templates/availability/edit.html b/templates/availability/edit.html deleted file mode 100644 index c0a32cbb..00000000 --- a/templates/availability/edit.html +++ /dev/null @@ -1,117 +0,0 @@ -{% extends "nav.html" %} - -{% block content %} -
-
-
-

Bearbeite Vefügbarkeit für den {{ date.format("%d.%m.%Y") }}

- - - -
-
- -
-
-
-
-
-
-
-
- -
-
- -
-
-
- -
-
- -
-
-
- -
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
-
- -
-
- Zurück -
-
-
-
- -
-
-
- - -{% endblock %} diff --git a/templates/availability/new.html b/templates/availability/new.html deleted file mode 100644 index dc62dc9c..00000000 --- a/templates/availability/new.html +++ /dev/null @@ -1,109 +0,0 @@ -{% extends "nav.html" %} - -{% block content %} -
-
-
-

Neue Vefügbarkeit für den {{ date.format("%d.%m.%Y") }}

- - - -
-
- -
-
-
-
-
-
-
-
- -
-
- -
-
-
- -
-
- -
-
-
- -
-
- -
-
-
-
- -
-
-
-
- -
-
-
-
-
- -
-
- Zurück -
-
-
-
- -
-
-
- - -{% endblock %} diff --git a/templates/availability/new_or_edit.html b/templates/availability/new_or_edit.html new file mode 100644 index 00000000..c1575da6 --- /dev/null +++ b/templates/availability/new_or_edit.html @@ -0,0 +1,98 @@ +{% extends "nav.html" %} + +{% block content %} +
+
+ {% if id.is_some() %} +
+

Bearbeite Vefügbarkeit für den {{ date.format("%d.%m.%Y") }}

+ {% else %} + +

Neue Vefügbarkeit für den {{ date.format("%d.%m.%Y") }}

+ {% endif %} + + + +
+
+ +
+
+
+
+
+
+
+
+ +
+
+ +
+
+
+ +
+
+ +
+
+
+ +
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+ {% if id.is_some() %} + + {% else %} + + {% endif %} +
+
+ Zurück +
+
+
+
+ +
+
+
+{% endblock %} diff --git a/templates/events/new.html b/templates/events/new.html index 20e3a5a0..43fdf1bd 100644 --- a/templates/events/new.html +++ b/templates/events/new.html @@ -9,110 +9,108 @@
-
- -
-
-
-
- -
-
-
+
+ +
+
+
+
+ +
+
+
-
- -
-
-
- -
-
- -
-
+
+ +
+
+
+ +
+
+ +
+
-
- -
-
-
-
-
- -
-
-
-
+
+ +
+
+
+
+
+ +
+
+
+
-
- -
-
-
-
- -
-
-
+
+ +
+
+
+
+ +
+
+
-
- -
-
-
-
- -
-
-
+
+ +
+
+
+
+ +
+
+
-
- -
-
-
-
- -
-
-
+
+ +
+
+
+
+ +
+
+
-
-
-
-
- -
-
- Zurück -
-
-
+
+
+
+
+ +
+
+ Zurück +
+
+
- - {% endblock %} diff --git a/templates/index.html b/templates/index.html index f90ba4a7..3cef9daa 100644 --- a/templates/index.html +++ b/templates/index.html @@ -63,7 +63,14 @@ {% if (user.role == Role::Admin || user.role == Role::AreaManager) && (selected_area.is_none() || selected_area.unwrap() == user.area_id) %} {% endif %} @@ -80,7 +87,8 @@
{{ event.name }}
{% if user.role == Role::AreaManager || user.role == Role::Admin %} - Planen + Planen bearbeiten als abgesagt markieren {% endif %} @@ -105,10 +113,16 @@ Verfügbarkeiten am {{ date.format("%d.%m.%Y") }} - {% if (user.role == Role::Admin || user.role == Role::AreaManager) && (selected_area.is_none() || - selected_area.unwrap() == user.area_id) %} + {% if selected_area.is_none() || selected_area.unwrap() == user.area_id %} {% endif %} @@ -132,7 +146,7 @@ {% for availabillity in availabillities %} {% let u = availabillity.user.as_ref().unwrap() %} - + {{ u.name }} {% match u.function %} @@ -157,8 +171,10 @@ {% if availabillity.user_id == user.id %}
- Bearbeiten - + Bearbeiten +
{% endif %} @@ -172,22 +188,4 @@ - - {% endblock %}