From eb869450083c03e93dcfe34205c37fd20a7c7cdc Mon Sep 17 00:00:00 2001 From: Max Hohlfeld Date: Thu, 19 Dec 2024 23:36:34 +0100 Subject: [PATCH] refactor: restructure code --- web/src/auth/mod.rs | 2 -- web/src/endpoints/user/mod.rs | 14 ++++++++----- web/src/endpoints/user/post_login.rs | 2 +- .../endpoints/vehicle_assignment/post_new.rs | 12 +++-------- web/src/main.rs | 1 - web/src/models/vehicle.rs | 20 ------------------- web/src/{auth/utils.rs => utils/auth.rs} | 1 + web/src/utils/manage_commands.rs | 2 +- web/src/utils/mod.rs | 5 +++-- 9 files changed, 18 insertions(+), 41 deletions(-) delete mode 100644 web/src/auth/mod.rs rename web/src/{auth/utils.rs => utils/auth.rs} (99%) diff --git a/web/src/auth/mod.rs b/web/src/auth/mod.rs deleted file mode 100644 index 12990fda..00000000 --- a/web/src/auth/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod utils; - diff --git a/web/src/endpoints/user/mod.rs b/web/src/endpoints/user/mod.rs index d9e9a8ab..ca42cbbc 100644 --- a/web/src/endpoints/user/mod.rs +++ b/web/src/endpoints/user/mod.rs @@ -1,7 +1,11 @@ -use crate::auth::utils::hash_plain_password_with_salt; -use crate::models::{Area, Role, Token, User}; -use crate::utils::{password_help, ApplicationError}; -use crate::{auth, filters}; +use crate::{ + filters, + models::{Area, Role, Token, User}, + utils::{ + auth::{generate_salt_and_hash_plain_password, hash_plain_password_with_salt}, + password_help, ApplicationError, + }, +}; use actix_web::HttpResponse; use rinja::Template; use sqlx::PgPool; @@ -117,7 +121,7 @@ async fn handle_password_change_request( return Ok(HttpResponse::BadRequest().body("Passwörter stimmen nicht überein.")); } - let (hash, salt) = auth::utils::generate_salt_and_hash_plain_password(password).unwrap(); + let (hash, salt) = generate_salt_and_hash_plain_password(password).unwrap(); User::update( pool, diff --git a/web/src/endpoints/user/post_login.rs b/web/src/endpoints/user/post_login.rs index 8a6ae7cc..a1d88ac2 100644 --- a/web/src/endpoints/user/post_login.rs +++ b/web/src/endpoints/user/post_login.rs @@ -3,7 +3,7 @@ use actix_web::{web, HttpMessage, HttpRequest, HttpResponse, Responder}; use serde::{Deserialize, Serialize}; use sqlx::PgPool; -use crate::{auth::utils::hash_plain_password_with_salt, models::User}; +use crate::{models::User, utils::auth::hash_plain_password_with_salt}; #[derive(Deserialize, Serialize)] pub struct LoginForm { diff --git a/web/src/endpoints/vehicle_assignment/post_new.rs b/web/src/endpoints/vehicle_assignment/post_new.rs index 2b540086..1d9f9fa5 100644 --- a/web/src/endpoints/vehicle_assignment/post_new.rs +++ b/web/src/endpoints/vehicle_assignment/post_new.rs @@ -4,15 +4,9 @@ use serde::Deserialize; use sqlx::PgPool; use crate::{ - endpoints::{assignment::PlanEventPersonalTablePartialTemplate, vehicle_assignment::PlanVehiclesPartialTemplate}, - models::{Assignment, Availabillity, Event, Function, Role, User, Vehicle, VehicleAssignement}, - utils::{ - event_planning_template::{ - generate_availabillity_assignment_list, generate_status_whether_staff_is_required, - generate_vehicles_assigned_and_available, - }, - ApplicationError, - }, + endpoints::vehicle_assignment::PlanVehiclesPartialTemplate, + models::{Event, Role, User, Vehicle, VehicleAssignement}, + utils::{event_planning_template::generate_vehicles_assigned_and_available, ApplicationError}, }; #[derive(Deserialize)] diff --git a/web/src/main.rs b/web/src/main.rs index 9ca10275..6e2db15e 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -16,7 +16,6 @@ use sqlx::{Pool, Postgres}; use crate::postgres_session_store::SqlxPostgresqlSessionStore; use crate::utils::manage_commands::{handle_command, parse_args}; -mod auth; mod endpoints; mod middleware; mod models; diff --git a/web/src/models/vehicle.rs b/web/src/models/vehicle.rs index 518fef3c..dafd0581 100644 --- a/web/src/models/vehicle.rs +++ b/web/src/models/vehicle.rs @@ -36,26 +36,6 @@ impl Vehicle { Ok(vehicles) } - pub async fn read_all_assignable_for_event(pool: &PgPool, event_id: i32) -> Result> { - let records = query!(r#" - SELECT * - FROM vehicle - - ;"#).fetch_all(pool).await?; - - let vehicles = records - .into_iter() - .map(|v| Vehicle { - id: v.id, - radio_call_name: v.radiocallname, - station: v.station, - }) - .collect(); - - Ok(vehicles) - } - - pub async fn read(pool: &PgPool, id: i32) -> Result> { let record = query!("SELECT * FROM vehicle WHERE id = $1;", id) .fetch_optional(pool) diff --git a/web/src/auth/utils.rs b/web/src/utils/auth.rs similarity index 99% rename from web/src/auth/utils.rs rename to web/src/utils/auth.rs index 0b35e79f..dcf489f5 100644 --- a/web/src/auth/utils.rs +++ b/web/src/utils/auth.rs @@ -23,3 +23,4 @@ pub fn hash_plain_password_with_salt(plain: &str, salt_string: &str) -> Result