diff --git a/src/endpoints/availability/get_overview.rs b/src/endpoints/availability/get_overview.rs index 67312eee..cf366b18 100644 --- a/src/endpoints/availability/get_overview.rs +++ b/src/endpoints/availability/get_overview.rs @@ -1,4 +1,5 @@ -use actix_web::{web, Responder}; +use crate::filters; +use actix_web::{web, HttpResponse, Responder}; use askama::Template; use askama_actix::TemplateToResponse; use chrono::{NaiveDate, Utc}; @@ -10,6 +11,7 @@ use crate::models::{Area, Availabillity, Event, Function, Role, User}; #[derive(Deserialize)] pub struct CalendarQuery { date: Option, + area: Option, } #[derive(Template)] @@ -17,6 +19,7 @@ pub struct CalendarQuery { struct CalendarTemplate { user: User, date: NaiveDate, + selected_area: Option, areas: Vec, events: Vec, availabillities: Vec, @@ -32,19 +35,40 @@ async fn get( Some(given_date) => given_date, None => Utc::now().date_naive(), }; + let areas = Area::read_all(pool.get_ref()).await.unwrap(); - let events = Event::read_by_date_including_location(pool.get_ref(), date) - .await - .unwrap(); + let selected_area = match query.area { + Some(id) => { + if !areas.iter().any(|a| a.id == id) { + return HttpResponse::BadRequest().finish(); + } - let availabillities = Availabillity::read_by_date_including_user(pool.get_ref(), date) - .await - .unwrap(); + Some(id) + } + None => None, + }; + + let events = Event::read_by_date_and_area_including_location( + pool.get_ref(), + date, + query.area.unwrap_or(user.area_id), + ) + .await + .unwrap(); + + let availabillities = Availabillity::read_by_date_and_area_including_user( + pool.get_ref(), + date, + query.area.unwrap_or(user.area_id), + ) + .await + .unwrap(); let template = CalendarTemplate { user: user.into_inner(), date, + selected_area, areas, events, availabillities, diff --git a/src/endpoints/user/patch.rs b/src/endpoints/user/patch.rs index 5586d363..19d14525 100644 --- a/src/endpoints/user/patch.rs +++ b/src/endpoints/user/patch.rs @@ -1,4 +1,3 @@ -use actix_identity::Identity; use actix_web::{web, HttpResponse, Responder}; use serde::Deserialize; use serde_json::value::Value; diff --git a/src/filters.rs b/src/filters.rs new file mode 100644 index 00000000..a98cce0e --- /dev/null +++ b/src/filters.rs @@ -0,0 +1,7 @@ +pub fn show_area_query(a: &Option) -> ::askama::Result { + if let Some(a) = a { + return Ok(format!("&area={}", a)); + } else { + return Ok(String::new()); + } +} diff --git a/src/main.rs b/src/main.rs index 081a9412..49d7bb51 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,8 +19,10 @@ mod endpoints; mod models; mod middleware; +mod filters; mod postgres_session_store; + pub enum Command { Migrate, CreateAdmin, diff --git a/src/models/availabillity.rs b/src/models/availabillity.rs index 049381ff..32c9d0f6 100644 --- a/src/models/availabillity.rs +++ b/src/models/availabillity.rs @@ -33,33 +33,10 @@ impl Availabillity { Ok(result) } - pub async fn read_by_date( - pool: &PgPool, - date: NaiveDate, - ) -> anyhow::Result> { - let records = query!("SELECT * FROM availabillity WHERE date = $1", date) - .fetch_all(pool) - .await?; - - let availabillities = records - .iter() - .map(|a| Availabillity { - id: a.id, - user_id: a.userid, - user: None, - date: a.date, - start_time: a.starttime, - end_time: a.endtime, - comment: a.comment.clone(), - }) - .collect(); - - Ok(availabillities) - } - - pub async fn read_by_date_including_user( + pub async fn read_by_date_and_area_including_user( pool: &PgPool, date: NaiveDate, + area_id: i32 ) -> anyhow::Result> { let records = query!( r##" @@ -69,7 +46,7 @@ impl Availabillity { availabillity.date, availabillity.startTime, availabillity.endTime, - availabillity.comment, + availabillity.comment, user_.name, user_.email, user_.password, @@ -82,9 +59,11 @@ impl Availabillity { user_.receiveNotifications FROM availabillity JOIN user_ ON availabillity.userId = user_.id - WHERE availabillity.date = $1; + WHERE availabillity.date = $1 + AND user_.areaId = $2; "##, - date + date, + area_id ) .fetch_all(pool) .await?; @@ -130,7 +109,7 @@ impl Availabillity { availabillity.date, availabillity.startTime, availabillity.endTime, - availabillity.comment, + availabillity.comment, user_.name, user_.email, user_.password, @@ -211,7 +190,7 @@ impl Availabillity { availabillity.date, availabillity.startTime, availabillity.endTime, - availabillity.comment, + availabillity.comment, user_.name, user_.email, user_.password, diff --git a/src/models/event.rs b/src/models/event.rs index 66407a63..cbf5c9ba 100644 --- a/src/models/event.rs +++ b/src/models/event.rs @@ -34,34 +34,10 @@ impl Event { Ok(result.id) } - pub async fn read_by_date(pool: &PgPool, date: NaiveDate) -> anyhow::Result> { - let records = query!("SELECT * FROM event WHERE date = $1;", date) - .fetch_all(pool) - .await?; - - let events = records - .iter() - .map(|record| Event { - id: record.id, - date: record.date, - start_time: record.starttime, - end_time: record.endtime, - name: record.name.to_string(), - location_id: record.locationid, - location: None, - voluntary_wachhabender: record.voluntarywachhabender, - amount_of_posten: record.amountofposten, - clothing: record.clothing.to_string(), - canceled: record.canceled, - }) - .collect(); - - Ok(events) - } - - pub async fn read_by_date_including_location( + pub async fn read_by_date_and_area_including_location( pool: &PgPool, date: NaiveDate, + area_id: i32 ) -> anyhow::Result> { let records = query!( r#" @@ -81,9 +57,11 @@ impl Event { location.areaId AS locationAreaId FROM event JOIN location ON event.locationId = location.id - WHERE date = $1; + WHERE date = $1 + AND location.areaId = $2; "#, - date + date, + area_id ) .fetch_all(pool) .await?; diff --git a/templates/index.html b/templates/index.html index 7eaa450f..1dc17a3f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,14 +5,45 @@
+
+ {% if user.role == Role::Admin %} +
+ +
+ {% endif %}
+ +
@@ -25,14 +56,17 @@ Events 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) %} + {% endif %} {% if events.len() == 0 %}
-
keine Events geplant
+
keine Events geplant
{% else %} {% for event in events %} @@ -67,14 +101,17 @@ Verfügbarkeiten am {{ date.format("%d.%m.%Y") }} + {% if (user.role == Role::Admin || user.role == Role::AreaManager) && selected_area.is_some() && + selected_area.unwrap() == user.area_id %} + {% endif %} {% if availabillities.len() == 0 %}
-
keine Verfügbarkeiten eingetragen
+
keine Verfügbarkeiten eingetragen
{% else %}