diff --git a/editoast/src/views/timetable/import.rs b/editoast/src/views/timetable/import.rs index a0a7631a66d..68459838202 100644 --- a/editoast/src/views/timetable/import.rs +++ b/editoast/src/views/timetable/import.rs @@ -1,3 +1,4 @@ +use crate::schema::OperationalPointPart; use crate::views::timetable::TimetableError; use crate::{core::CoreClient, views::timetable::Path, DbPool}; use actix_web::{post, web::Data}; @@ -114,17 +115,18 @@ pub async fn post_timetable( return Err(TimetableError::InfraNotLoaded { infra_id }.into()); } - let pathfinding_request = PathfindingRequest::new(infra_id, infra_version.clone()); for import_item in data.into_inner() { // PATHFINDING - let mut pf_request = pathfinding_request.clone(); + let mut pf_request = PathfindingRequest::new(infra_id, infra_version.clone()); let Some(rolling_stock_model) = RollingStockModel::retrieve_by_name(&mut conn, import_item.rolling_stock.clone()) .await? else { continue; }; - let rolling_stock: RollingStock = rolling_stock_model.clone().into(); + let rolling_stock_id = rolling_stock_model.id.unwrap(); + let rollingstock_version = rolling_stock_model.version; + let rolling_stock: RollingStock = rolling_stock_model.into(); pf_request.with_rolling_stocks(&mut vec![rolling_stock.clone()]); // List operational points uic needed for this import let mut ops_uic = import_item @@ -153,22 +155,9 @@ pub async fn post_timetable( continue; } // Create waypoints - let mut waypoints = PathfindingWaypoints::new(); - for step in import_item.path.iter() { - waypoints.push(match &step.location { - TimetableImportPathLocation::TrackOffsetLocation { - track_section, - offset, - } => Vec::from(Waypoint::bidirectional(track_section, *offset)), - TimetableImportPathLocation::OperationalPointLocation { uic } => op_id_to_parts - .get(uic) - .unwrap() - .iter() - .flat_map(|op_part| Waypoint::bidirectional(&op_part.track, op_part.position)) - .collect(), - }); - } + let mut waypoints = waypoints_from_steps(&import_item.path, &op_id_to_parts); pf_request.with_waypoints(&mut waypoints); + // Run pathfinding // TODO: Stops duration should be associated to trains not path let steps_duration = vec![0.; pf_request.nb_waypoints()]; @@ -280,10 +269,10 @@ pub async fn post_timetable( departure_time: import_train.departure_time.num_seconds_from_midnight() as f64, scheduled_points: Default::default(), // TODO change path_id, - rolling_stock_id: rolling_stock_model.id.unwrap(), + rolling_stock_id, timetable_id, infra_version: Some(infra_version.clone()), - rollingstock_version: rolling_stock_model.version, + rollingstock_version, ..Default::default() } .create_conn(&mut conn) @@ -297,3 +286,25 @@ pub async fn post_timetable( Ok(HttpResponse::NoContent().finish()) } + +fn waypoints_from_steps( + path: &Vec, + op_id_to_parts: &HashMap>, +) -> Vec> { + let mut res = PathfindingWaypoints::new(); + for step in path { + res.push(match &step.location { + TimetableImportPathLocation::TrackOffsetLocation { + track_section, + offset, + } => Vec::from(Waypoint::bidirectional(track_section, *offset)), + TimetableImportPathLocation::OperationalPointLocation { uic } => op_id_to_parts + .get(uic) + .unwrap() + .iter() + .flat_map(|op_part| Waypoint::bidirectional(&op_part.track, op_part.position)) + .collect(), + }); + } + res +}