From 09821c4b8d3353ce4e89b46ade3b8be9903e26a0 Mon Sep 17 00:00:00 2001 From: Max Hohlfeld Date: Sun, 26 Jan 2025 19:28:39 +0100 Subject: [PATCH] refactor: rename availability --- web/src/endpoints/assignment/mod.rs | 4 +-- web/src/endpoints/assignment/post_new.rs | 4 +-- web/src/endpoints/availability/delete.rs | 6 ++-- web/src/endpoints/availability/get_new.rs | 6 ++-- .../endpoints/availability/get_overview.rs | 8 ++--- web/src/endpoints/availability/get_update.rs | 8 ++--- web/src/endpoints/availability/mod.rs | 4 +-- web/src/endpoints/availability/post_new.rs | 4 +-- web/src/endpoints/availability/post_update.rs | 6 ++-- web/src/endpoints/events/get_plan.rs | 4 +-- web/src/endpoints/events/post_edit.rs | 4 +-- .../endpoints/export/get_availability_data.rs | 4 +-- web/src/models/assignment_changeset.rs | 4 +-- web/src/models/availability_changeset.rs | 3 ++ web/src/models/availabillity.rs | 34 ++++++------------- .../models/availabillity_assignment_state.rs | 14 ++++++++ web/src/models/mod.rs | 5 ++- web/src/utils/event_planning_template.rs | 6 ++-- 18 files changed, 67 insertions(+), 61 deletions(-) create mode 100644 web/src/models/availability_changeset.rs create mode 100644 web/src/models/availabillity_assignment_state.rs diff --git a/web/src/endpoints/assignment/mod.rs b/web/src/endpoints/assignment/mod.rs index 36667edb..c370661f 100644 --- a/web/src/endpoints/assignment/mod.rs +++ b/web/src/endpoints/assignment/mod.rs @@ -2,7 +2,7 @@ use rinja::Template; use crate::{ filters, - models::{Availabillity, AvailabillityAssignmentState, Event, Function}, + models::{Availability, AvailabillityAssignmentState, Event, Function}, }; pub mod delete; @@ -12,7 +12,7 @@ pub mod post_new; #[template(path = "events/plan_personal_table.html")] pub struct PlanEventPersonalTablePartialTemplate { event: Event, - availabillities: Vec<(Availabillity, AvailabillityAssignmentState)>, + availabillities: Vec<(Availability, AvailabillityAssignmentState)>, further_posten_required: bool, further_fuehrungsassistent_required: bool, further_wachhabender_required: bool, diff --git a/web/src/endpoints/assignment/post_new.rs b/web/src/endpoints/assignment/post_new.rs index 3083f1bf..d02244b1 100644 --- a/web/src/endpoints/assignment/post_new.rs +++ b/web/src/endpoints/assignment/post_new.rs @@ -7,7 +7,7 @@ use sqlx::PgPool; use crate::{ endpoints::assignment::PlanEventPersonalTablePartialTemplate, models::{ - Assignment, AssignmentChangeset, AssignmentContext, Availabillity, Event, Function, Role, + Assignment, AssignmentChangeset, AssignmentContext, Availability, Event, Function, Role, User, }, utils::{ @@ -45,7 +45,7 @@ pub async fn post( } let Some(availabillity) = - Availabillity::read_by_id_including_user(pool.get_ref(), query.availabillity).await? + Availability::read_by_id_including_user(pool.get_ref(), query.availabillity).await? else { return Ok(HttpResponse::NotFound().finish()); }; diff --git a/web/src/endpoints/availability/delete.rs b/web/src/endpoints/availability/delete.rs index 6fd6c753..d1c9c86b 100644 --- a/web/src/endpoints/availability/delete.rs +++ b/web/src/endpoints/availability/delete.rs @@ -3,7 +3,7 @@ use sqlx::PgPool; use crate::{ endpoints::IdPath, - models::{Availabillity, User}, utils::ApplicationError, + models::{Availability, User}, utils::ApplicationError, }; #[actix_web::delete("/availabillity/delete/{id}")] @@ -12,7 +12,7 @@ pub async fn delete( pool: web::Data, path: web::Path, ) -> Result { - let Some(availabillity) = Availabillity::read_by_id(pool.get_ref(), path.id).await? else { + let Some(availabillity) = Availability::read_by_id(pool.get_ref(), path.id).await? else { return Ok(HttpResponse::NotFound().finish()); }; @@ -20,7 +20,7 @@ pub async fn delete( return Err(ApplicationError::Unauthorized); } - Availabillity::delete(pool.get_ref(), availabillity.id).await?; + Availability::delete(pool.get_ref(), availabillity.id).await?; Ok(HttpResponse::Ok().finish()) } diff --git a/web/src/endpoints/availability/get_new.rs b/web/src/endpoints/availability/get_new.rs index fca70341..04ec1ad9 100644 --- a/web/src/endpoints/availability/get_new.rs +++ b/web/src/endpoints/availability/get_new.rs @@ -5,7 +5,7 @@ use serde::Deserialize; use sqlx::PgPool; use crate::endpoints::availability::{calc_free_slots_cor, NewOrEditAvailabilityTemplate}; -use crate::models::{Availabillity, User}; +use crate::models::{Availability, User}; use crate::utils::ApplicationError; #[derive(Deserialize)] @@ -21,14 +21,14 @@ pub async fn get( pool: web::Data, query: web::Query, ) -> Result { - let availabillities = Availabillity::read_by_date_and_area_including_user( + let availabillities = Availability::read_by_date_and_area_including_user( pool.get_ref(), query.date, user.area_id, ) .await?; - let availabilities_from_user: Vec<&Availabillity> = availabillities + let availabilities_from_user: Vec<&Availability> = availabillities .iter() .filter(|a| a.user_id == user.id) .collect(); diff --git a/web/src/endpoints/availability/get_overview.rs b/web/src/endpoints/availability/get_overview.rs index 0519aca0..dc257fcd 100644 --- a/web/src/endpoints/availability/get_overview.rs +++ b/web/src/endpoints/availability/get_overview.rs @@ -7,7 +7,7 @@ use rinja::Template; use serde::Deserialize; use sqlx::PgPool; -use crate::models::{Area, Availabillity, Event, Role, User}; +use crate::models::{Area, Availability, Event, Role, User}; #[derive(Deserialize)] pub struct CalendarQuery { @@ -30,7 +30,7 @@ struct CalendarTemplate { Option, Vec, )>, - availabillities: Vec, + availabillities: Vec, } #[actix_web::get("/")] @@ -57,14 +57,14 @@ async fn get( None => None, }; - let availabillities = Availabillity::read_by_date_and_area_including_user( + let availabillities = Availability::read_by_date_and_area_including_user( pool.get_ref(), date, query.area.unwrap_or(user.area_id), ) .await?; - let availabilities_from_user: Vec<&Availabillity> = availabillities + let availabilities_from_user: Vec<&Availability> = availabillities .iter() .filter(|a| a.user_id == user.id) .collect(); diff --git a/web/src/endpoints/availability/get_update.rs b/web/src/endpoints/availability/get_update.rs index dd191c41..0924d270 100644 --- a/web/src/endpoints/availability/get_update.rs +++ b/web/src/endpoints/availability/get_update.rs @@ -8,7 +8,7 @@ use crate::{ availability::{calc_free_slots_cor, NewOrEditAvailabilityTemplate}, IdPath, }, - models::{Availabillity, User}, + models::{Availability, User}, utils::ApplicationError, }; @@ -25,7 +25,7 @@ pub async fn get( path: web::Path, query: web::Query, ) -> Result { - let Some(availabillity) = Availabillity::read_by_id(pool.get_ref(), path.id).await? else { + let Some(availabillity) = Availability::read_by_id(pool.get_ref(), path.id).await? else { return Ok(HttpResponse::NotFound().finish()); }; @@ -44,14 +44,14 @@ pub async fn get( let has_time = availabillity.start_time.is_some() && availabillity.end_time.is_some(); let suggestions = if has_time { - let availabillities = Availabillity::read_by_date_and_area_including_user( + let availabillities = Availability::read_by_date_and_area_including_user( pool.get_ref(), availabillity.date, user.area_id, ) .await?; - let availabilities_from_user: Vec<&Availabillity> = availabillities + let availabilities_from_user: Vec<&Availability> = availabillities .iter() .filter(|a| a.user_id == user.id) .collect(); diff --git a/web/src/endpoints/availability/mod.rs b/web/src/endpoints/availability/mod.rs index 5affa8ee..d9afa84b 100644 --- a/web/src/endpoints/availability/mod.rs +++ b/web/src/endpoints/availability/mod.rs @@ -2,7 +2,7 @@ use chrono::{NaiveDate, NaiveTime}; use rinja::Template; use crate::filters; -use crate::models::{Availabillity, Role, User}; +use crate::models::{Availability, Role, User}; pub mod delete; pub mod get_new; @@ -24,7 +24,7 @@ struct NewOrEditAvailabilityTemplate<'a> { slot_suggestions: Vec<(NaiveTime, NaiveTime)>, } -fn calc_free_slots_cor(availabilities: &Vec<&Availabillity>) -> Vec<(NaiveTime, NaiveTime)> { +fn calc_free_slots_cor(availabilities: &Vec<&Availability>) -> Vec<(NaiveTime, NaiveTime)> { let mut times = Vec::new(); for a in availabilities { diff --git a/web/src/endpoints/availability/post_new.rs b/web/src/endpoints/availability/post_new.rs index bd732957..9eea1b65 100644 --- a/web/src/endpoints/availability/post_new.rs +++ b/web/src/endpoints/availability/post_new.rs @@ -4,7 +4,7 @@ use serde::Deserialize; use sqlx::PgPool; use crate::{ - models::{Availabillity, User}, + models::{Availability, User}, utils::{self, ApplicationError}, }; @@ -22,7 +22,7 @@ pub async fn post( pool: web::Data, form: web::Form, ) -> Result { - Availabillity::create( + Availability::create( pool.get_ref(), user.id, form.date, diff --git a/web/src/endpoints/availability/post_update.rs b/web/src/endpoints/availability/post_update.rs index b20f377d..64fd27e3 100644 --- a/web/src/endpoints/availability/post_update.rs +++ b/web/src/endpoints/availability/post_update.rs @@ -3,7 +3,7 @@ use sqlx::PgPool; use crate::{ endpoints::{availability::post_new::AvailabillityForm, IdPath}, - models::{Availabillity, User}, + models::{Availability, User}, utils::{self, ApplicationError}, }; @@ -14,7 +14,7 @@ pub async fn post( path: web::Path, form: web::Form, ) -> Result { - let Some(availabillity) = Availabillity::read_by_id(pool.get_ref(), path.id).await? else { + let Some(availabillity) = Availability::read_by_id(pool.get_ref(), path.id).await? else { return Ok(HttpResponse::NotFound().finish()); }; @@ -26,7 +26,7 @@ pub async fn post( || availabillity.end_time != form.till || availabillity.comment != form.comment { - Availabillity::update( + Availability::update( pool.get_ref(), availabillity.id, form.from, diff --git a/web/src/endpoints/events/get_plan.rs b/web/src/endpoints/events/get_plan.rs index 94347d1f..9ff8d960 100644 --- a/web/src/endpoints/events/get_plan.rs +++ b/web/src/endpoints/events/get_plan.rs @@ -5,7 +5,7 @@ use sqlx::PgPool; use crate::{ endpoints::IdPath, filters, - models::{Availabillity, AvailabillityAssignmentState, Event, Function, Role, User, Vehicle}, + models::{Availability, AvailabillityAssignmentState, Event, Function, Role, User, Vehicle}, utils::{ event_planning_template::{ generate_availabillity_assignment_list, generate_status_whether_staff_is_required, @@ -20,7 +20,7 @@ use crate::{ pub struct PlanEventTemplate { user: User, event: Event, - availabillities: Vec<(Availabillity, AvailabillityAssignmentState)>, + availabillities: Vec<(Availability, AvailabillityAssignmentState)>, further_posten_required: bool, further_fuehrungsassistent_required: bool, further_wachhabender_required: bool, diff --git a/web/src/endpoints/events/post_edit.rs b/web/src/endpoints/events/post_edit.rs index 43154f9a..9d3b5e7b 100644 --- a/web/src/endpoints/events/post_edit.rs +++ b/web/src/endpoints/events/post_edit.rs @@ -7,7 +7,7 @@ use sqlx::PgPool; use crate::{ endpoints::IdPath, models::{ - Assignment, AssignmentChangeset, Availabillity, Event, EventChangeset, EventContext, + Assignment, AssignmentChangeset, Availability, Event, EventChangeset, EventContext, Function, Location, Role, User, }, utils::{self, ApplicationError}, @@ -79,7 +79,7 @@ pub async fn post( NaiveTime::parse_from_str("23:59", "%R").unwrap(), ); for assignment in &assignments_for_event { - let availability = Availabillity::read_by_id(pool.get_ref(), assignment.availabillity_id) + let availability = Availability::read_by_id(pool.get_ref(), assignment.availabillity_id) .await? .unwrap(); let all_assignments = diff --git a/web/src/endpoints/export/get_availability_data.rs b/web/src/endpoints/export/get_availability_data.rs index 8729cbc2..0f008e4d 100644 --- a/web/src/endpoints/export/get_availability_data.rs +++ b/web/src/endpoints/export/get_availability_data.rs @@ -4,7 +4,7 @@ use chrono::{Months, NaiveDate, NaiveTime, Utc}; use serde::{Deserialize, Serialize}; use sqlx::PgPool; -use crate::models::{Availabillity, Function, Role, User}; +use crate::models::{Availability, Function, Role, User}; #[derive(Deserialize)] struct ExportQuery { @@ -64,7 +64,7 @@ pub async fn get( }; let availabillities = - Availabillity::read_for_export(pool.get_ref(), (start_date, end_date), area_id) + Availability::read_for_export(pool.get_ref(), (start_date, end_date), area_id) .await .unwrap(); diff --git a/web/src/models/assignment_changeset.rs b/web/src/models/assignment_changeset.rs index 2de850e4..029baf91 100644 --- a/web/src/models/assignment_changeset.rs +++ b/web/src/models/assignment_changeset.rs @@ -1,7 +1,7 @@ use chrono::NaiveTime; use garde::Validate; -use super::{start_time_lies_before_end_time, Assignment, Availabillity, Event, Function}; +use super::{start_time_lies_before_end_time, Assignment, Availability, Event, Function}; #[derive(Validate)] #[garde(allow_unvalidated)] @@ -23,7 +23,7 @@ pub struct AssignmentChangeset { pub struct AssignmentContext { pub event: Event, - pub availabillity: Availabillity, + pub availabillity: Availability, pub user_function: Function, pub assignments_for_event: Vec, pub assignments_for_availabillity: Vec, diff --git a/web/src/models/availability_changeset.rs b/web/src/models/availability_changeset.rs new file mode 100644 index 00000000..9bdda2af --- /dev/null +++ b/web/src/models/availability_changeset.rs @@ -0,0 +1,3 @@ +pub struct AvailabilityChangeset { + +} diff --git a/web/src/models/availabillity.rs b/web/src/models/availabillity.rs index d28bafdd..0d38e8ba 100644 --- a/web/src/models/availabillity.rs +++ b/web/src/models/availabillity.rs @@ -4,7 +4,7 @@ use sqlx::{query, PgPool}; use super::{Area, Function, Result, Role, User}; #[derive(Clone, Debug)] -pub struct Availabillity { +pub struct Availability { pub id: i32, pub user_id: i32, pub user: Option, @@ -14,21 +14,7 @@ pub struct Availabillity { pub comment: Option, } -#[derive(PartialEq, Eq, Debug)] -pub enum AvailabillityAssignmentState { - // availabillity is not assigned at all or at least not timely conflicting - Unassigned, - // availabillity is assigned for another event that is timely conflicting - Conflicting, - // availabillity is assigned to this event as Posten - AssignedPosten(i32), - // availabillity is assigned to this event as Führungsassistent - AssignedFührungsassistent(i32), - // availabillity is assigned to this event as Wachhabender - AssignedWachhabender(i32), -} - -impl Availabillity { +impl Availability { pub async fn create( pool: &PgPool, user_id: i32, @@ -58,7 +44,7 @@ impl Availabillity { pool: &PgPool, date: NaiveDate, area_id: i32, - ) -> Result> { + ) -> Result> { let records = query!( r##" SELECT @@ -91,7 +77,7 @@ impl Availabillity { let availabillities = records .iter() - .map(|r| Availabillity { + .map(|r| Availability { id: r.id, user_id: r.userid, user: Some(User { @@ -121,7 +107,7 @@ impl Availabillity { pub async fn read_by_id_including_user( pool: &PgPool, id: i32, - ) -> Result> { + ) -> Result> { let record = query!( r##" SELECT @@ -152,7 +138,7 @@ impl Availabillity { .await?; let availabillity = record.and_then(|r| { - Some(Availabillity { + Some(Availability { id: r.id, user_id: r.userid, user: Some(User { @@ -179,13 +165,13 @@ impl Availabillity { Ok(availabillity) } - pub async fn read_by_id(pool: &PgPool, id: i32) -> Result> { + pub async fn read_by_id(pool: &PgPool, id: i32) -> Result> { let record = query!("SELECT * FROM availabillity WHERE id = $1", id) .fetch_optional(pool) .await?; let availabillity = record.and_then(|record| { - Some(Availabillity { + Some(Availability { id: record.id, user_id: record.userid, user: None, @@ -203,7 +189,7 @@ impl Availabillity { pool: &PgPool, date_range: (NaiveDate, NaiveDate), area_id: i32, - ) -> Result> { + ) -> Result> { let records = query!( r##" SELECT @@ -240,7 +226,7 @@ impl Availabillity { let availabillities = records .iter() - .map(|r| Availabillity { + .map(|r| Availability { id: r.id, user_id: r.userid, user: Some(User { diff --git a/web/src/models/availabillity_assignment_state.rs b/web/src/models/availabillity_assignment_state.rs new file mode 100644 index 00000000..f8050b9f --- /dev/null +++ b/web/src/models/availabillity_assignment_state.rs @@ -0,0 +1,14 @@ +#[derive(PartialEq, Eq, Debug)] +pub enum AvailabillityAssignmentState { + // availabillity is not assigned at all or at least not timely conflicting + Unassigned, + // availabillity is assigned for another event that is timely conflicting + Conflicting, + // availabillity is assigned to this event as Posten + AssignedPosten(i32), + // availabillity is assigned to this event as Führungsassistent + AssignedFührungsassistent(i32), + // availabillity is assigned to this event as Wachhabender + AssignedWachhabender(i32), +} + diff --git a/web/src/models/mod.rs b/web/src/models/mod.rs index 3e0d1e5c..3440e23a 100644 --- a/web/src/models/mod.rs +++ b/web/src/models/mod.rs @@ -2,6 +2,7 @@ mod area; mod assignement; mod assignment_changeset; mod availabillity; +mod availabillity_assignment_state; mod event; mod event_changeset; mod function; @@ -12,11 +13,13 @@ mod role; mod user; mod vehicle; mod vehicle_assignement; +mod availability_changeset; pub use area::Area; pub use assignement::Assignment; pub use assignment_changeset::{AssignmentChangeset, AssignmentContext}; -pub use availabillity::{Availabillity, AvailabillityAssignmentState}; +pub use availabillity::Availability; +pub use availabillity_assignment_state::AvailabillityAssignmentState; use chrono::NaiveTime; pub use event::Event; pub use event_changeset::{EventChangeset, EventContext}; diff --git a/web/src/utils/event_planning_template.rs b/web/src/utils/event_planning_template.rs index 2725b107..29f7acbf 100644 --- a/web/src/utils/event_planning_template.rs +++ b/web/src/utils/event_planning_template.rs @@ -1,7 +1,7 @@ use sqlx::PgPool; use crate::models::{ - Assignment, Availabillity, AvailabillityAssignmentState, Event, Function, Vehicle, VehicleAssignement, + Assignment, Availability, AvailabillityAssignmentState, Event, Function, Vehicle, VehicleAssignement, }; use super::ApplicationError; @@ -9,8 +9,8 @@ use super::ApplicationError; pub async fn generate_availabillity_assignment_list( pool: &PgPool, event: &Event, -) -> Result, ApplicationError> { - let availabillities_in_db = Availabillity::read_by_date_and_area_including_user( +) -> Result, ApplicationError> { + let availabillities_in_db = Availability::read_by_date_and_area_including_user( pool, event.date, event.location.as_ref().unwrap().area_id,