From c339b262ed2f41ac90d5916740cd0e7b9838b8c1 Mon Sep 17 00:00:00 2001 From: Leo Valais Date: Tue, 3 Dec 2024 16:58:09 +0100 Subject: [PATCH 1/4] editoast: remove DbPool V1 support Signed-off-by: Leo Valais --- .../editoast_models/src/db_connection_pool.rs | 13 ++++----- editoast/editoast_models/src/lib.rs | 8 +---- editoast/src/models/fixtures.rs | 2 +- editoast/src/views/infra/attached.rs | 2 +- editoast/src/views/infra/auto_fixes/mod.rs | 2 +- editoast/src/views/infra/delimited_area.rs | 2 +- editoast/src/views/infra/edition.rs | 4 +-- editoast/src/views/infra/lines.rs | 2 +- editoast/src/views/infra/mod.rs | 12 ++++---- editoast/src/views/infra/pathfinding.rs | 2 +- editoast/src/views/infra/railjson.rs | 2 +- editoast/src/views/infra/routes.rs | 4 +-- editoast/src/views/layers.rs | 2 +- editoast/src/views/mod.rs | 23 +++++++-------- editoast/src/views/path/pathfinding.rs | 2 +- editoast/src/views/path/properties.rs | 2 +- .../src/views/stdcm_search_environment.rs | 4 +-- editoast/src/views/test_app.rs | 29 ++++--------------- editoast/src/views/timetable.rs | 2 +- editoast/src/views/timetable/stdcm.rs | 2 +- editoast/src/views/train_schedule.rs | 12 ++++---- .../src/views/train_schedule/projection.rs | 2 +- 22 files changed, 52 insertions(+), 83 deletions(-) diff --git a/editoast/editoast_models/src/db_connection_pool.rs b/editoast/editoast_models/src/db_connection_pool.rs index 44760eed997..ae651012313 100644 --- a/editoast/editoast_models/src/db_connection_pool.rs +++ b/editoast/editoast_models/src/db_connection_pool.rs @@ -27,9 +27,6 @@ use url::Url; use tokio::sync::OwnedRwLockWriteGuard; use tokio::sync::RwLock; -use super::DbConnectionPool; -use super::DieselConnection; - pub type DbConnectionConfig = AsyncDieselConnectionManager; #[derive(Clone)] @@ -110,7 +107,7 @@ impl DerefMut for WriteHandle { /// /// # Testing pool /// -/// In test mode, the [DbConnectionPool::get] function will always return the same connection that has +/// In test mode, the [Pool::::get] function will always return the same connection that has /// been setup to drop all modification once the test ends. /// Since this connection will not commit any changes to the database, we ensure the isolation of each test. /// @@ -423,17 +420,17 @@ pub async fn ping_database(conn: &mut DbConnection) -> Result<(), PingError> { Ok(()) } -pub fn create_connection_pool( +fn create_connection_pool( url: Url, max_size: usize, -) -> Result { +) -> Result, DatabasePoolBuildError> { let mut manager_config = ManagerConfig::default(); manager_config.custom_setup = Box::new(establish_connection); let manager = DbConnectionConfig::new_with_config(url, manager_config); Ok(Pool::builder(manager).max_size(max_size).build()?) } -fn establish_connection(config: &str) -> BoxFuture> { +fn establish_connection(config: &str) -> BoxFuture> { let fut = async { let mut connector_builder = SslConnector::builder(SslMethod::tls()).unwrap(); connector_builder.set_verify(SslVerifyMode::NONE); @@ -448,7 +445,7 @@ fn establish_connection(config: &str) -> BoxFuture; - /// Generic error type to forward errors from the database /// /// Useful for functions which only points of failure are the DB calls. #[derive(Debug, thiserror::Error)] -#[error("an error occured while querying the database: {0}")] +#[error("an error occurred while querying the database: {0}")] pub struct DatabaseError(#[from] diesel::result::Error); diff --git a/editoast/src/models/fixtures.rs b/editoast/src/models/fixtures.rs index 2f32e11255d..5a93f662aa3 100644 --- a/editoast/src/models/fixtures.rs +++ b/editoast/src/models/fixtures.rs @@ -4,7 +4,7 @@ use std::ops::DerefMut; use chrono::Utc; use editoast_models::DbConnection; -use editoast_models::DbConnectionPool; + use editoast_models::DbConnectionPoolV2; use editoast_schemas::infra::Direction; use editoast_schemas::infra::DirectionalTrackRange; diff --git a/editoast/src/views/infra/attached.rs b/editoast/src/views/infra/attached.rs index 6595a55bc4a..ae0184eb32c 100644 --- a/editoast/src/views/infra/attached.rs +++ b/editoast/src/views/infra/attached.rs @@ -67,7 +67,7 @@ async fn attached( Path(InfraAttachedParams { infra_id, track_id }): Path, State(AppState { infra_caches, - db_pool_v2: db_pool, + db_pool, .. }): State, Extension(auth): AuthenticationExt, diff --git a/editoast/src/views/infra/auto_fixes/mod.rs b/editoast/src/views/infra/auto_fixes/mod.rs index b62802b87e7..cb9cd2649ab 100644 --- a/editoast/src/views/infra/auto_fixes/mod.rs +++ b/editoast/src/views/infra/auto_fixes/mod.rs @@ -87,7 +87,7 @@ async fn list_auto_fixes( Path(infra_id): Path, State(AppState { infra_caches, - db_pool_v2: db_pool, + db_pool, .. }): State, Extension(auth): AuthenticationExt, diff --git a/editoast/src/views/infra/delimited_area.rs b/editoast/src/views/infra/delimited_area.rs index 0ac51ae9b87..7ef6104afc7 100644 --- a/editoast/src/views/infra/delimited_area.rs +++ b/editoast/src/views/infra/delimited_area.rs @@ -129,7 +129,7 @@ async fn delimited_area( Extension(auth): AuthenticationExt, State(AppState { infra_caches, - db_pool_v2: db_pool, + db_pool, .. }): State, Path(InfraIdParam { infra_id }): Path, diff --git a/editoast/src/views/infra/edition.rs b/editoast/src/views/infra/edition.rs index fc58345f4bf..241ce66917b 100644 --- a/editoast/src/views/infra/edition.rs +++ b/editoast/src/views/infra/edition.rs @@ -71,7 +71,7 @@ crate::routes! { async fn edit<'a>( Path(InfraIdParam { infra_id }): Path, State(AppState { - db_pool_v2: db_pool, + db_pool, infra_caches, valkey, map_layers, @@ -126,7 +126,7 @@ async fn edit<'a>( pub async fn split_track_section<'a>( Path(InfraIdParam { infra_id }): Path, State(AppState { - db_pool_v2: db_pool, + db_pool, infra_caches, valkey, map_layers, diff --git a/editoast/src/views/infra/lines.rs b/editoast/src/views/infra/lines.rs index ecf9ce840a4..5f5d89f57c3 100644 --- a/editoast/src/views/infra/lines.rs +++ b/editoast/src/views/infra/lines.rs @@ -45,7 +45,7 @@ async fn get_line_bbox( Path((infra_id, line_code)): Path<(i64, i64)>, State(AppState { infra_caches, - db_pool_v2: db_pool, + db_pool, .. }): State, Extension(auth): AuthenticationExt, diff --git a/editoast/src/views/infra/mod.rs b/editoast/src/views/infra/mod.rs index 442886c72c6..03f9ec8244c 100644 --- a/editoast/src/views/infra/mod.rs +++ b/editoast/src/views/infra/mod.rs @@ -135,7 +135,7 @@ async fn refresh( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let valkey_client = app_state.valkey.clone(); let infra_caches = app_state.infra_caches.clone(); let map_layers = app_state.map_layers.clone(); @@ -212,7 +212,7 @@ async fn list( if !authorized { return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let osrdyne_client = app_state.osrdyne_client.clone(); let settings = pagination_params @@ -307,7 +307,7 @@ async fn get( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let osrdyne_client = app_state.osrdyne_client.clone(); let infra_id = infra.infra_id; @@ -433,7 +433,7 @@ async fn delete( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let infra_caches = app_state.infra_caches.clone(); let infra_id = infra.infra_id; if Infra::fast_delete_static(db_pool.get().await?, infra_id).await? { @@ -513,7 +513,7 @@ async fn get_switch_types( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; let infra_caches = app_state.infra_caches.clone(); @@ -724,7 +724,7 @@ async fn load( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let core_client = app_state.core_client.clone(); let infra_id = path.infra_id; diff --git a/editoast/src/views/infra/pathfinding.rs b/editoast/src/views/infra/pathfinding.rs index f727541d07c..d152f5fdac9 100644 --- a/editoast/src/views/infra/pathfinding.rs +++ b/editoast/src/views/infra/pathfinding.rs @@ -110,7 +110,7 @@ async fn pathfinding_view( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let infra_caches = app_state.infra_caches.clone(); // Parse and check input diff --git a/editoast/src/views/infra/railjson.rs b/editoast/src/views/infra/railjson.rs index 607c2bd7d55..2b62631590d 100644 --- a/editoast/src/views/infra/railjson.rs +++ b/editoast/src/views/infra/railjson.rs @@ -181,7 +181,7 @@ async fn post_railjson( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let infra_caches = app_state.infra_caches.clone(); if railjson.version != RAILJSON_VERSION { return Err(ListErrorsRailjson::WrongRailjsonVersionProvided.into()); diff --git a/editoast/src/views/infra/routes.rs b/editoast/src/views/infra/routes.rs index 91769ed92fd..6e52e1566b1 100644 --- a/editoast/src/views/infra/routes.rs +++ b/editoast/src/views/infra/routes.rs @@ -158,7 +158,7 @@ async fn get_routes_track_ranges( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let infra_caches = app_state.infra_caches.clone(); let infra_id = infra; let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, infra_id, || { @@ -219,7 +219,7 @@ async fn get_routes_nodes( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let infra_caches = app_state.infra_caches.clone(); let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, params.infra_id, || { diff --git a/editoast/src/views/layers.rs b/editoast/src/views/layers.rs index 78a031822a7..5b9bcf88ed4 100644 --- a/editoast/src/views/layers.rs +++ b/editoast/src/views/layers.rs @@ -179,7 +179,7 @@ struct TileParams { async fn cache_and_get_mvt_tile( State(AppState { map_layers, - db_pool_v2: db_pool, + db_pool, valkey, .. }): State, diff --git a/editoast/src/views/mod.rs b/editoast/src/views/mod.rs index 522c23f9e71..ed34758915d 100644 --- a/editoast/src/views/mod.rs +++ b/editoast/src/views/mod.rs @@ -41,7 +41,7 @@ use dashmap::DashMap; use editoast_authz::authorizer::Authorizer; use editoast_authz::authorizer::UserInfo; use editoast_authz::BuiltinRole; -use editoast_models::DbConnectionPool; + use editoast_osrdyne_client::OsrdyneClient; use futures::TryFutureExt; pub use openapi::OpenApiRoot; @@ -225,7 +225,7 @@ async fn authenticate( async fn authentication_middleware( State(AppState { - db_pool_v2: db_pool, + db_pool, disable_authorization, .. }): State, @@ -278,7 +278,7 @@ pub enum AppHealthError { )] async fn health( State(AppState { - db_pool_v2: db_pool, + db_pool, valkey, health_check_timeout, core_client, @@ -384,8 +384,7 @@ pub struct Server { pub struct AppState { pub config: Arc, - pub db_pool_v1: Arc, - pub db_pool_v2: Arc, + pub db_pool: Arc, pub valkey: Arc, pub infra_caches: Arc>, pub map_layers: Arc, @@ -398,7 +397,7 @@ pub struct AppState { impl FromRef for DbConnectionPoolV2 { fn from_ref(input: &AppState) -> Self { - (*input.db_pool_v2).clone() + (*input.db_pool).clone() } } @@ -409,16 +408,15 @@ impl AppState { // Config database let valkey = ValkeyClient::new(config.valkey_config.clone())?.into(); - // Create both database pools - let db_pool_v2 = { + // Create database pool + let db_pool = { let PostgresConfig { database_url, pool_size, } = config.postgres_config.clone(); - DbConnectionPoolV2::try_initialize(database_url, pool_size).await? + let pool = DbConnectionPoolV2::try_initialize(database_url, pool_size).await?; + Arc::new(pool) }; - let db_pool_v1 = db_pool_v2.pool_v1(); - let db_pool_v2 = Arc::new(db_pool_v2); // Setup infra cache map let infra_caches = DashMap::::default().into(); @@ -449,8 +447,7 @@ impl AppState { Ok(Self { valkey, - db_pool_v1, - db_pool_v2, + db_pool, infra_caches, core_client, osrdyne_client, diff --git a/editoast/src/views/path/pathfinding.rs b/editoast/src/views/path/pathfinding.rs index 7ecb6f5417e..220566072ef 100644 --- a/editoast/src/views/path/pathfinding.rs +++ b/editoast/src/views/path/pathfinding.rs @@ -158,7 +158,7 @@ pub enum PathfindingFailure { )] async fn post( State(AppState { - db_pool_v2: db_pool, + db_pool, valkey, core_client, .. diff --git a/editoast/src/views/path/properties.rs b/editoast/src/views/path/properties.rs index 4a32b467231..ca5b0aaff1c 100644 --- a/editoast/src/views/path/properties.rs +++ b/editoast/src/views/path/properties.rs @@ -164,7 +164,7 @@ type Properties = EnumSet; )] async fn post( State(AppState { - db_pool_v2: db_pool, + db_pool, valkey, core_client, .. diff --git a/editoast/src/views/stdcm_search_environment.rs b/editoast/src/views/stdcm_search_environment.rs index cfd94a4d604..3610295620e 100644 --- a/editoast/src/views/stdcm_search_environment.rs +++ b/editoast/src/views/stdcm_search_environment.rs @@ -118,7 +118,7 @@ async fn overwrite( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; let changeset: Changeset = form.into(); @@ -146,7 +146,7 @@ async fn retrieve_latest( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; let search_env = StdcmSearchEnvironment::retrieve_latest(conn).await; diff --git a/editoast/src/views/test_app.rs b/editoast/src/views/test_app.rs index fb1f0372810..9ab47d93081 100644 --- a/editoast/src/views/test_app.rs +++ b/editoast/src/views/test_app.rs @@ -7,7 +7,6 @@ use std::sync::Arc; use axum::Router; use axum_tracing_opentelemetry::middleware::OtelAxumLayer; use dashmap::DashMap; -use editoast_models::db_connection_pool::create_connection_pool; use editoast_models::DbConnectionPoolV2; use editoast_osrdyne_client::OsrdyneClient; use serde::de::DeserializeOwned; @@ -40,7 +39,6 @@ pub(crate) struct TestAppBuilder { db_pool: Option, core_client: Option, osrdyne_client: Option, - db_pool_v1: bool, } impl TestAppBuilder { @@ -49,13 +47,11 @@ impl TestAppBuilder { db_pool: None, core_client: None, osrdyne_client: None, - db_pool_v1: false, } } pub fn db_pool(mut self, db_pool: DbConnectionPoolV2) -> Self { assert!(self.db_pool.is_none()); - assert!(!self.db_pool_v1); self.db_pool = Some(db_pool); self } @@ -126,24 +122,10 @@ impl TestAppBuilder { .expect("Could not build Valkey client") .into(); - // Create both database pools - let (db_pool_v2, db_pool_v1) = if self.db_pool_v1 { - let PostgresConfig { - database_url, - pool_size, - } = config.postgres_config.clone(); - let pool = create_connection_pool(database_url, pool_size) - .expect("could not create connection pool for tests"); - let v1 = Arc::new(pool); - let v2 = futures::executor::block_on(DbConnectionPoolV2::from_pool(v1.clone())); - (Arc::new(v2), v1) - } else { - let db_pool_v2 = self.db_pool.expect( - "No database pool provided to TestAppBuilder, use Default or provide a database pool" - ); - let db_pool_v1 = db_pool_v2.pool_v1(); - (Arc::new(db_pool_v2), db_pool_v1) - }; + // Create database pool + let db_pool_v2 = Arc::new(self.db_pool.expect( + "No database pool provided to TestAppBuilder, use Default or provide a database pool", + )); // Setup infra cache map let infra_caches = DashMap::::default().into(); @@ -163,8 +145,7 @@ impl TestAppBuilder { let osrdyne_client = Arc::new(osrdyne_client); let app_state = AppState { - db_pool_v1, - db_pool_v2: db_pool_v2.clone(), + db_pool: db_pool_v2.clone(), core_client: core_client.clone(), osrdyne_client, valkey, diff --git a/editoast/src/views/timetable.rs b/editoast/src/views/timetable.rs index 8b278f6111d..e8595eaf56a 100644 --- a/editoast/src/views/timetable.rs +++ b/editoast/src/views/timetable.rs @@ -285,7 +285,7 @@ async fn conflicts( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let valkey_client = app_state.valkey.clone(); let core_client = app_state.core_client.clone(); diff --git a/editoast/src/views/timetable/stdcm.rs b/editoast/src/views/timetable/stdcm.rs index 34fe53fb625..2b0a32de357 100644 --- a/editoast/src/views/timetable/stdcm.rs +++ b/editoast/src/views/timetable/stdcm.rs @@ -134,7 +134,7 @@ async fn stdcm( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; let valkey_client = app_state.valkey.clone(); diff --git a/editoast/src/views/train_schedule.rs b/editoast/src/views/train_schedule.rs index d537e715985..2a2c8dabc5c 100644 --- a/editoast/src/views/train_schedule.rs +++ b/editoast/src/views/train_schedule.rs @@ -179,7 +179,7 @@ async fn get( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let train_schedule_id = train_schedule_id.id; let conn = &mut db_pool.get().await?; @@ -217,7 +217,7 @@ async fn get_batch( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; let train_schedules: Vec = TrainSchedule::retrieve_batch_or_fail(conn, train_ids, |missing| { @@ -251,7 +251,7 @@ async fn delete( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); use crate::models::DeleteBatch; let conn = &mut db_pool.get().await?; @@ -339,7 +339,7 @@ async fn simulation( let valkey_client = app_state.valkey.clone(); let core_client = app_state.core_client.clone(); - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let infra_id = infra_id_query.infra_id; let electrical_profile_set_id = electrical_profile_set_id_query.electrical_profile_set_id; @@ -673,7 +673,7 @@ async fn simulation_summary( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; let valkey_client = app_state.valkey.clone(); let core = app_state.core_client.clone(); @@ -774,7 +774,7 @@ async fn get_path( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let valkey_client = app_state.valkey.clone(); let core = app_state.core_client.clone(); diff --git a/editoast/src/views/train_schedule/projection.rs b/editoast/src/views/train_schedule/projection.rs index 46590b1f151..ff40379c62a 100644 --- a/editoast/src/views/train_schedule/projection.rs +++ b/editoast/src/views/train_schedule/projection.rs @@ -150,7 +150,7 @@ async fn project_path( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool_v2.clone(); + let db_pool = app_state.db_pool.clone(); let valkey_client = app_state.valkey.clone(); let core_client = app_state.core_client.clone(); From 5b797433531c4007f514c1a62fba831b3df642f6 Mon Sep 17 00:00:00 2001 From: Leo Valais Date: Tue, 3 Dec 2024 17:24:27 +0100 Subject: [PATCH 2/4] editoast: destructure AppState early in handlers for consistency Signed-off-by: Leo Valais --- editoast/src/views/infra/mod.rs | 68 +++++++++++-------- editoast/src/views/infra/pathfinding.rs | 9 +-- editoast/src/views/infra/railjson.rs | 10 +-- editoast/src/views/infra/routes.rs | 21 +++--- editoast/src/views/mod.rs | 7 +- .../src/views/stdcm_search_environment.rs | 11 +-- editoast/src/views/timetable.rs | 19 +++--- editoast/src/views/timetable/stdcm.rs | 10 +-- editoast/src/views/train_schedule.rs | 44 ++++++------ .../src/views/train_schedule/projection.rs | 11 +-- 10 files changed, 114 insertions(+), 96 deletions(-) diff --git a/editoast/src/views/infra/mod.rs b/editoast/src/views/infra/mod.rs index 03f9ec8244c..992025a108a 100644 --- a/editoast/src/views/infra/mod.rs +++ b/editoast/src/views/infra/mod.rs @@ -123,7 +123,13 @@ struct RefreshResponse { ) )] async fn refresh( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + infra_caches, + map_layers, + .. + }): State, Extension(auth): AuthenticationExt, Query(query_params): Query, ) -> Result> { @@ -135,11 +141,6 @@ async fn refresh( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let valkey_client = app_state.valkey.clone(); - let infra_caches = app_state.infra_caches.clone(); - let map_layers = app_state.map_layers.clone(); - // Use a transaction to give scope to infra list lock let RefreshQueryParams { force, @@ -160,7 +161,6 @@ async fn refresh( }; // Refresh each infras - let db_pool = db_pool; let mut infra_refreshed = vec![]; for mut infra in infras_list { @@ -201,7 +201,11 @@ struct InfraListResponse { ), )] async fn list( - app_state: State, + State(AppState { + db_pool, + osrdyne_client, + .. + }): State, Extension(auth): AuthenticationExt, pagination_params: Query, ) -> Result> { @@ -212,8 +216,6 @@ async fn list( if !authorized { return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let osrdyne_client = app_state.osrdyne_client.clone(); let settings = pagination_params .validate(1000)? @@ -295,7 +297,11 @@ struct InfraIdParam { ), )] async fn get( - app_state: State, + State(AppState { + db_pool, + osrdyne_client, + .. + }): State, Extension(auth): AuthenticationExt, Path(infra): Path, ) -> Result> { @@ -307,9 +313,6 @@ async fn get( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let osrdyne_client = app_state.osrdyne_client.clone(); - let infra_id = infra.infra_id; let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, infra_id, || { InfraApiError::NotFound { infra_id } @@ -344,7 +347,7 @@ impl From for Changeset { ), )] async fn create( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Json(infra_form): Json, ) -> Result { @@ -381,7 +384,7 @@ struct CloneQuery { async fn clone( Extension(auth): AuthenticationExt, Path(params): Path, - db_pool: State, + State(db_pool): State, Query(CloneQuery { name }): Query, ) -> Result> { let authorized = auth @@ -421,7 +424,11 @@ async fn clone( ), )] async fn delete( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, infra: Path, ) -> Result { @@ -433,8 +440,6 @@ async fn delete( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let infra_caches = app_state.infra_caches.clone(); let infra_id = infra.infra_id; if Infra::fast_delete_static(db_pool.get().await?, infra_id).await? { infra_caches.remove(&infra_id); @@ -468,7 +473,7 @@ impl From for Changeset { ), )] async fn put( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Path(infra): Path, Json(patch): Json, @@ -501,7 +506,11 @@ async fn put( ) )] async fn get_switch_types( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, Path(infra): Path, ) -> Result>> { @@ -513,9 +522,7 @@ async fn get_switch_types( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; - let infra_caches = app_state.infra_caches.clone(); let infra = Infra::retrieve_or_fail(conn, infra.infra_id, || InfraApiError::NotFound { infra_id: infra.infra_id, @@ -546,7 +553,7 @@ async fn get_switch_types( async fn get_speed_limit_tags( Extension(auth): AuthenticationExt, Path(infra): Path, - db_pool: State, + State(db_pool): State, ) -> Result>> { let authorized = auth .check_roles([BuiltinRole::InfraRead].into()) @@ -590,7 +597,7 @@ async fn get_voltages( Extension(auth): AuthenticationExt, Path(infra): Path, Query(param): Query, - db_pool: State, + State(db_pool): State, ) -> Result>> { let authorized = auth .check_roles([BuiltinRole::InfraRead].into()) @@ -623,7 +630,7 @@ async fn get_voltages( ) )] async fn get_all_voltages( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, ) -> Result>> { let authorized = auth @@ -712,7 +719,11 @@ async fn unlock( ) )] async fn load( - app_state: State, + State(AppState { + db_pool, + core_client, + .. + }): State, Extension(auth): AuthenticationExt, Path(path): Path, ) -> Result { @@ -724,9 +735,6 @@ async fn load( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let core_client = app_state.core_client.clone(); - let infra_id = path.infra_id; let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, infra_id, || { InfraApiError::NotFound { infra_id } diff --git a/editoast/src/views/infra/pathfinding.rs b/editoast/src/views/infra/pathfinding.rs index d152f5fdac9..b4a77b80a0d 100644 --- a/editoast/src/views/infra/pathfinding.rs +++ b/editoast/src/views/infra/pathfinding.rs @@ -96,7 +96,11 @@ struct QueryParam { ) )] async fn pathfinding_view( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, Path(infra): Path, Query(params): Query, @@ -110,9 +114,6 @@ async fn pathfinding_view( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let infra_caches = app_state.infra_caches.clone(); - // Parse and check input let infra_id = infra.infra_id; let number = params.number.unwrap_or(DEFAULT_NUMBER_OF_PATHS); diff --git a/editoast/src/views/infra/railjson.rs b/editoast/src/views/infra/railjson.rs index 2b62631590d..ad859718765 100644 --- a/editoast/src/views/infra/railjson.rs +++ b/editoast/src/views/infra/railjson.rs @@ -55,7 +55,7 @@ enum ListErrorsRailjson { )] async fn get_railjson( Path(infra): Path, - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, ) -> Result { let authorized = auth @@ -168,7 +168,11 @@ struct PostRailjsonResponse { ) )] async fn post_railjson( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, Query(params): Query, Json(railjson): Json, @@ -181,8 +185,6 @@ async fn post_railjson( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let infra_caches = app_state.infra_caches.clone(); if railjson.version != RAILJSON_VERSION { return Err(ListErrorsRailjson::WrongRailjsonVersionProvided.into()); } diff --git a/editoast/src/views/infra/routes.rs b/editoast/src/views/infra/routes.rs index 6e52e1566b1..22c7b52ac2c 100644 --- a/editoast/src/views/infra/routes.rs +++ b/editoast/src/views/infra/routes.rs @@ -67,7 +67,7 @@ struct RoutesResponse { )] async fn get_routes_from_waypoint( Path(path): Path, - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, ) -> Result> { let authorized = auth @@ -145,7 +145,11 @@ struct RoutesFromNodesPositions { ), )] async fn get_routes_track_ranges( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, Path(infra): Path, Query(params): Query, @@ -158,8 +162,8 @@ async fn get_routes_track_ranges( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let infra_caches = app_state.infra_caches.clone(); + let db_pool = db_pool.clone(); + let infra_caches = infra_caches.clone(); let infra_id = infra; let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, infra_id, || { InfraApiError::NotFound { infra_id } @@ -206,7 +210,11 @@ async fn get_routes_track_ranges( ), )] async fn get_routes_nodes( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, Path(params): Path, Json(node_states): Json>>, @@ -219,9 +227,6 @@ async fn get_routes_nodes( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let infra_caches = app_state.infra_caches.clone(); - let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, params.infra_id, || { InfraApiError::NotFound { infra_id: params.infra_id, diff --git a/editoast/src/views/mod.rs b/editoast/src/views/mod.rs index ed34758915d..3b79ed76f56 100644 --- a/editoast/src/views/mod.rs +++ b/editoast/src/views/mod.rs @@ -334,8 +334,11 @@ async fn version() -> Json { (status = 200, description = "Return the core service version", body = Version), ), )] -async fn core_version(app_state: State) -> Json { - let core = app_state.core_client.clone(); +async fn core_version( + State(AppState { + core_client: core, .. + }): State, +) -> Json { let response = CoreVersionRequest {}.fetch(&core).await; let response = response.unwrap_or(Version { git_describe: None }); Json(response) diff --git a/editoast/src/views/stdcm_search_environment.rs b/editoast/src/views/stdcm_search_environment.rs index 3610295620e..e4d6db80f34 100644 --- a/editoast/src/views/stdcm_search_environment.rs +++ b/editoast/src/views/stdcm_search_environment.rs @@ -6,6 +6,7 @@ use axum::response::Response; use axum::Extension; use chrono::NaiveDateTime; use editoast_authz::BuiltinRole; +use editoast_models::DbConnectionPoolV2; use serde::de::Error as SerdeError; use serde::Deserialize; use std::result::Result as StdResult; @@ -19,7 +20,6 @@ use crate::models::stdcm_search_environment::StdcmSearchEnvironment; use crate::models::Changeset; use crate::views::AuthenticationExt; use crate::views::AuthorizationError; -use crate::AppState; use crate::Model; crate::routes! { @@ -106,7 +106,7 @@ impl From for Changeset, + State(db_pool): State, Extension(auth): AuthenticationExt, Json(form): Json, ) -> Result { @@ -118,11 +118,8 @@ async fn overwrite( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; - let changeset: Changeset = form.into(); - Ok((StatusCode::CREATED, Json(changeset.overwrite(conn).await?))) } @@ -135,7 +132,7 @@ async fn overwrite( ) )] async fn retrieve_latest( - State(app_state): State, + State(db_pool): State, Extension(auth): AuthenticationExt, ) -> Result { let authorized = auth @@ -146,9 +143,7 @@ async fn retrieve_latest( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; - let search_env = StdcmSearchEnvironment::retrieve_latest(conn).await; if let Some(search_env) = search_env { Ok(Json(search_env).into_response()) diff --git a/editoast/src/views/timetable.rs b/editoast/src/views/timetable.rs index e8595eaf56a..a78b13f0846 100644 --- a/editoast/src/views/timetable.rs +++ b/editoast/src/views/timetable.rs @@ -120,7 +120,7 @@ struct TimetableIdParam { ), )] async fn get( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Path(timetable_id): Path, ) -> Result> { @@ -154,7 +154,7 @@ async fn get( ), )] async fn post( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, ) -> Result> { let authorized = auth @@ -183,7 +183,7 @@ async fn post( ), )] async fn delete( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, timetable_id: Path, ) -> Result { @@ -215,7 +215,7 @@ async fn delete( ) )] async fn train_schedule( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Path(timetable_id): Path, Json(train_schedules): Json>, @@ -271,7 +271,12 @@ pub struct ElectricalProfileSetIdQueryParam { ), )] async fn conflicts( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + core_client, + .. + }): State, Extension(auth): AuthenticationExt, Path(timetable_id): Path, Query(infra_id_query): Query, @@ -285,10 +290,6 @@ async fn conflicts( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let valkey_client = app_state.valkey.clone(); - let core_client = app_state.core_client.clone(); - let timetable_id = timetable_id.id; let infra_id = infra_id_query.infra_id; let electrical_profile_set_id = electrical_profile_set_id_query.electrical_profile_set_id; diff --git a/editoast/src/views/timetable/stdcm.rs b/editoast/src/views/timetable/stdcm.rs index 2b0a32de357..1abf7ef1ae3 100644 --- a/editoast/src/views/timetable/stdcm.rs +++ b/editoast/src/views/timetable/stdcm.rs @@ -120,7 +120,12 @@ struct InfraIdQueryParam { ) )] async fn stdcm( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + core_client, + .. + }): State, Extension(auth): AuthenticationExt, Path(id): Path, Query(query): Query, @@ -134,11 +139,8 @@ async fn stdcm( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; - let valkey_client = app_state.valkey.clone(); - let core_client = app_state.core_client.clone(); let timetable_id = id; let infra_id = query.infra; diff --git a/editoast/src/views/train_schedule.rs b/editoast/src/views/train_schedule.rs index 2a2c8dabc5c..80fd72681bb 100644 --- a/editoast/src/views/train_schedule.rs +++ b/editoast/src/views/train_schedule.rs @@ -167,7 +167,7 @@ impl From for TrainScheduleChangeset { ) )] async fn get( - app_state: State, + State(db_pool): State, Extension(auth): AuthenticationExt, train_schedule_id: Path, ) -> Result> { @@ -179,7 +179,6 @@ async fn get( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let train_schedule_id = train_schedule_id.id; let conn = &mut db_pool.get().await?; @@ -205,7 +204,7 @@ struct BatchRequest { ) )] async fn get_batch( - app_state: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Json(BatchRequest { ids: train_ids }): Json, ) -> Result>> { @@ -217,7 +216,6 @@ async fn get_batch( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; let train_schedules: Vec = TrainSchedule::retrieve_batch_or_fail(conn, train_ids, |missing| { @@ -239,7 +237,7 @@ async fn get_batch( ) )] async fn delete( - app_state: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Json(BatchRequest { ids: train_ids }): Json, ) -> Result { @@ -251,8 +249,6 @@ async fn delete( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - use crate::models::DeleteBatch; let conn = &mut db_pool.get().await?; TrainSchedule::delete_batch_or_fail(conn, train_ids, |number| { @@ -274,7 +270,7 @@ async fn delete( ) )] async fn put( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, train_schedule_id: Path, Json(train_schedule_form): Json, @@ -323,7 +319,12 @@ pub struct ElectricalProfileSetIdQueryParam { ), )] async fn simulation( - app_state: State, + State(AppState { + valkey: valkey_client, + core_client, + db_pool, + .. + }): State, Extension(auth): AuthenticationExt, Path(train_schedule_id): Path, Query(infra_id_query): Query, @@ -337,10 +338,6 @@ async fn simulation( return Err(AuthorizationError::Unauthorized.into()); } - let valkey_client = app_state.valkey.clone(); - let core_client = app_state.core_client.clone(); - let db_pool = app_state.db_pool.clone(); - let infra_id = infra_id_query.infra_id; let electrical_profile_set_id = electrical_profile_set_id_query.electrical_profile_set_id; let train_schedule_id = train_schedule_id.id; @@ -657,7 +654,12 @@ enum SimulationSummaryResult { ), )] async fn simulation_summary( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + core_client: core, + .. + }): State, Extension(auth): AuthenticationExt, Json(SimulationBatchForm { infra_id, @@ -673,10 +675,7 @@ async fn simulation_summary( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; - let valkey_client = app_state.valkey.clone(); - let core = app_state.core_client.clone(); let infra = Infra::retrieve_or_fail(conn, infra_id, || TrainScheduleError::InfraNotFound { infra_id, @@ -759,7 +758,12 @@ async fn simulation_summary( ) )] async fn get_path( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + core_client: core, + .. + }): State, Extension(auth): AuthenticationExt, Path(TrainScheduleIdParam { id: train_schedule_id, @@ -774,10 +778,6 @@ async fn get_path( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let valkey_client = app_state.valkey.clone(); - let core = app_state.core_client.clone(); - let conn = &mut db_pool.get().await?; let mut valkey_conn = valkey_client.get_connection().await?; diff --git a/editoast/src/views/train_schedule/projection.rs b/editoast/src/views/train_schedule/projection.rs index ff40379c62a..6c06a73c29a 100644 --- a/editoast/src/views/train_schedule/projection.rs +++ b/editoast/src/views/train_schedule/projection.rs @@ -126,7 +126,12 @@ struct CachedProjectPathTrainResult { ), )] async fn project_path( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + core_client, + .. + }): State, Extension(auth): AuthenticationExt, Json(ProjectPathForm { infra_id, @@ -150,10 +155,6 @@ async fn project_path( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let valkey_client = app_state.valkey.clone(); - let core_client = app_state.core_client.clone(); - let ProjectPathInput { track_section_ranges: path_track_ranges, routes: path_routes, From 6968585670931e0cb03bb27ed147424df3fde1df Mon Sep 17 00:00:00 2001 From: Leo Valais Date: Tue, 3 Dec 2024 17:31:28 +0100 Subject: [PATCH 3/4] editoast: fix minor destructuration inconsistencies Signed-off-by: Leo Valais --- editoast/openapi.yaml | 4 +-- editoast/src/views/infra/mod.rs | 5 ++- editoast/src/views/timetable.rs | 24 +++++--------- editoast/src/views/train_schedule.rs | 31 +++++++++---------- .../src/views/train_schedule/projection.rs | 4 +-- 5 files changed, 29 insertions(+), 39 deletions(-) diff --git a/editoast/openapi.yaml b/editoast/openapi.yaml index 53f213dafad..29362fc945f 100644 --- a/editoast/openapi.yaml +++ b/editoast/openapi.yaml @@ -10072,7 +10072,7 @@ components: enum: - pathfinding_not_found - type: object - description: An error has occured during pathfinding + description: An error has occurred during pathfinding required: - core_error - status @@ -10084,7 +10084,7 @@ components: enum: - pathfinding_failure - type: object - description: An error has occured during computing + description: An error has occurred during computing required: - error_type - status diff --git a/editoast/src/views/infra/mod.rs b/editoast/src/views/infra/mod.rs index 992025a108a..dc64f075337 100644 --- a/editoast/src/views/infra/mod.rs +++ b/editoast/src/views/infra/mod.rs @@ -207,7 +207,7 @@ async fn list( .. }): State, Extension(auth): AuthenticationExt, - pagination_params: Query, + Query(pagination_params): Query, ) -> Result> { let authorized = auth .check_roles([BuiltinRole::InfraRead].into()) @@ -430,7 +430,7 @@ async fn delete( .. }): State, Extension(auth): AuthenticationExt, - infra: Path, + Path(InfraIdParam { infra_id }): Path, ) -> Result { let authorized = auth .check_roles([BuiltinRole::InfraWrite].into()) @@ -440,7 +440,6 @@ async fn delete( return Err(AuthorizationError::Unauthorized.into()); } - let infra_id = infra.infra_id; if Infra::fast_delete_static(db_pool.get().await?, infra_id).await? { infra_caches.remove(&infra_id); Ok(StatusCode::NO_CONTENT) diff --git a/editoast/src/views/timetable.rs b/editoast/src/views/timetable.rs index a78b13f0846..3b888627a9e 100644 --- a/editoast/src/views/timetable.rs +++ b/editoast/src/views/timetable.rs @@ -122,7 +122,7 @@ struct TimetableIdParam { async fn get( State(db_pool): State, Extension(auth): AuthenticationExt, - Path(timetable_id): Path, + Path(TimetableIdParam { id: timetable_id }): Path, ) -> Result> { let authorized = auth .check_roles([BuiltinRole::TimetableRead].into()) @@ -132,15 +132,11 @@ async fn get( return Err(AuthorizationError::Unauthorized.into()); } - let timetable_id = timetable_id.id; - // Return the timetable - let conn = &mut db_pool.get().await?; let timetable = TimetableWithTrains::retrieve_or_fail(conn, timetable_id, || { TimetableError::NotFound { timetable_id } }) .await?; - Ok(Json(timetable.into())) } @@ -185,7 +181,7 @@ async fn post( async fn delete( State(db_pool): State, Extension(auth): AuthenticationExt, - timetable_id: Path, + Path(TimetableIdParam { id: timetable_id }): Path, ) -> Result { let authorized = auth .check_roles([BuiltinRole::TimetableWrite].into()) @@ -195,7 +191,6 @@ async fn delete( return Err(AuthorizationError::Unauthorized.into()); } - let timetable_id = timetable_id.id; let conn = &mut db_pool.get().await?; Timetable::delete_static_or_fail(conn, timetable_id, || TimetableError::NotFound { timetable_id, @@ -217,7 +212,7 @@ async fn delete( async fn train_schedule( State(db_pool): State, Extension(auth): AuthenticationExt, - Path(timetable_id): Path, + Path(TimetableIdParam { id: timetable_id }): Path, Json(train_schedules): Json>, ) -> Result>> { let authorized = auth @@ -230,7 +225,6 @@ async fn train_schedule( let conn = &mut db_pool.get().await?; - let timetable_id = timetable_id.id; TimetableWithTrains::retrieve_or_fail(conn, timetable_id, || TimetableError::NotFound { timetable_id, }) @@ -278,9 +272,11 @@ async fn conflicts( .. }): State, Extension(auth): AuthenticationExt, - Path(timetable_id): Path, - Query(infra_id_query): Query, - Query(electrical_profile_set_id_query): Query, + Path(TimetableIdParam { id: timetable_id }): Path, + Query(InfraIdQueryParam { infra_id }): Query, + Query(ElectricalProfileSetIdQueryParam { + electrical_profile_set_id, + }): Query, ) -> Result>> { let authorized = auth .check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableRead].into()) @@ -290,10 +286,6 @@ async fn conflicts( return Err(AuthorizationError::Unauthorized.into()); } - let timetable_id = timetable_id.id; - let infra_id = infra_id_query.infra_id; - let electrical_profile_set_id = electrical_profile_set_id_query.electrical_profile_set_id; - // 1. Retrieve Timetable / Infra / Trains / Simultion let timetable_trains = TimetableWithTrains::retrieve_or_fail(&mut db_pool.get().await?, timetable_id, || { diff --git a/editoast/src/views/train_schedule.rs b/editoast/src/views/train_schedule.rs index 80fd72681bb..b6aa27a3ab3 100644 --- a/editoast/src/views/train_schedule.rs +++ b/editoast/src/views/train_schedule.rs @@ -169,7 +169,9 @@ impl From for TrainScheduleChangeset { async fn get( State(db_pool): State, Extension(auth): AuthenticationExt, - train_schedule_id: Path, + Path(TrainScheduleIdParam { + id: train_schedule_id, + }): Path, ) -> Result> { let authorized = auth .check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableRead].into()) @@ -179,9 +181,7 @@ async fn get( return Err(AuthorizationError::Unauthorized.into()); } - let train_schedule_id = train_schedule_id.id; let conn = &mut db_pool.get().await?; - let train_schedule = TrainSchedule::retrieve_or_fail(conn, train_schedule_id, || { TrainScheduleError::NotFound { train_schedule_id } }) @@ -272,7 +272,9 @@ async fn delete( async fn put( State(db_pool): State, Extension(auth): AuthenticationExt, - train_schedule_id: Path, + Path(TrainScheduleIdParam { + id: train_schedule_id, + }): Path, Json(train_schedule_form): Json, ) -> Result> { let authorized = auth @@ -284,10 +286,7 @@ async fn put( } let conn = &mut db_pool.get().await?; - - let train_schedule_id = train_schedule_id.id; let ts_changeset: TrainScheduleChangeset = train_schedule_form.into(); - let ts_result = ts_changeset .update_or_fail(conn, train_schedule_id, || TrainScheduleError::NotFound { train_schedule_id, @@ -326,9 +325,13 @@ async fn simulation( .. }): State, Extension(auth): AuthenticationExt, - Path(train_schedule_id): Path, - Query(infra_id_query): Query, - Query(electrical_profile_set_id_query): Query, + Path(TrainScheduleIdParam { + id: train_schedule_id, + }): Path, + Query(InfraIdQueryParam { infra_id }): Query, + Query(ElectricalProfileSetIdQueryParam { + electrical_profile_set_id, + }): Query, ) -> Result> { let authorized = auth .check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableRead].into()) @@ -338,10 +341,6 @@ async fn simulation( return Err(AuthorizationError::Unauthorized.into()); } - let infra_id = infra_id_query.infra_id; - let electrical_profile_set_id = electrical_profile_set_id_query.electrical_profile_set_id; - let train_schedule_id = train_schedule_id.id; - // Retrieve infra or fail let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, infra_id, || { TrainScheduleError::InfraNotFound { infra_id } @@ -635,9 +634,9 @@ enum SimulationSummaryResult { }, /// Pathfinding not found PathfindingNotFound(PathfindingNotFound), - /// An error has occured during pathfinding + /// An error has occurred during pathfinding PathfindingFailure { core_error: InternalError }, - /// An error has occured during computing + /// An error has occurred during computing SimulationFailed { error_type: String }, /// InputError PathfindingInputError(PathfindingInputError), diff --git a/editoast/src/views/train_schedule/projection.rs b/editoast/src/views/train_schedule/projection.rs index 6c06a73c29a..8bfa58abdae 100644 --- a/editoast/src/views/train_schedule/projection.rs +++ b/editoast/src/views/train_schedule/projection.rs @@ -288,11 +288,11 @@ async fn project_path( let cached_value = CachedProjectPathTrainResult { space_time_curves: space_time_curves .get(id) - .expect("Space time curves not availabe for train") + .expect("Space time curves not available for train") .clone(), signal_updates: signal_updates .get(id) - .expect("Signal update not availabe for train") + .expect("Signal update not available for train") .clone(), }; hit_cache.insert(*id, cached_value.clone()); From fd2a3d951594ed73d69fe6b39c411790d5638640 Mon Sep 17 00:00:00 2001 From: Leo Valais Date: Wed, 4 Dec 2024 15:11:28 +0100 Subject: [PATCH 4/4] editoast: implement FromRef for CoreClient for easy access using State Signed-off-by: Leo Valais --- editoast/src/views/mod.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/editoast/src/views/mod.rs b/editoast/src/views/mod.rs index 3b79ed76f56..6f68bcefc5a 100644 --- a/editoast/src/views/mod.rs +++ b/editoast/src/views/mod.rs @@ -334,11 +334,7 @@ async fn version() -> Json { (status = 200, description = "Return the core service version", body = Version), ), )] -async fn core_version( - State(AppState { - core_client: core, .. - }): State, -) -> Json { +async fn core_version(State(core): State>) -> Json { let response = CoreVersionRequest {}.fetch(&core).await; let response = response.unwrap_or(Version { git_describe: None }); Json(response) @@ -404,6 +400,12 @@ impl FromRef for DbConnectionPoolV2 { } } +impl FromRef for Arc { + fn from_ref(input: &AppState) -> Self { + input.core_client.clone() + } +} + impl AppState { async fn init(config: ServerConfig) -> Result { info!("Building application state...");