|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use database::DbConnection; |
1 | 4 | use editoast_derive::Model; |
| 5 | +use itertools::Itertools; |
2 | 6 | use schemas::paced_train::PacedTrainException; |
3 | 7 | use schemas::train_schedule_exception::TrainScheduleExceptionChangeGroups; |
4 | 8 |
|
5 | 9 | use crate as editoast_models; |
| 10 | +use crate::prelude::List; |
| 11 | +use crate::prelude::SelectionSettings; |
6 | 12 |
|
7 | 13 | #[derive(Debug, Clone, Model)] |
8 | 14 | #[cfg_attr(test, derive(serde::Deserialize))] |
@@ -33,6 +39,35 @@ impl From<TrainScheduleException> for schemas::TrainScheduleException { |
33 | 39 | } |
34 | 40 | } |
35 | 41 |
|
| 42 | +impl TrainScheduleException { |
| 43 | + pub async fn retrieve_exceptions_by_train_schedules( |
| 44 | + conn: &mut DbConnection, |
| 45 | + timetable_id: i64, |
| 46 | + train_schedules_ids: Vec<i64>, |
| 47 | + ) -> Result<Vec<TrainScheduleException>, editoast_models::Error> { |
| 48 | + let train_schedules_ids_for_settings = train_schedules_ids.clone(); |
| 49 | + let exceptions_settings = SelectionSettings::new() |
| 50 | + .filter(move || editoast_models::TrainScheduleException::TIMETABLE_ID.eq(timetable_id)) |
| 51 | + .filter(move || { |
| 52 | + editoast_models::TrainScheduleException::TRAIN_SCHEDULE_ID |
| 53 | + .eq_any(train_schedules_ids_for_settings.clone()) |
| 54 | + }); |
| 55 | + |
| 56 | + let mut exceptions_map: HashMap<i64, Vec<TrainScheduleException>> = |
| 57 | + Self::list(conn, exceptions_settings) |
| 58 | + .await? |
| 59 | + .into_iter() |
| 60 | + .into_group_map_by(|e| e.train_schedule_id); |
| 61 | + |
| 62 | + let exceptions = train_schedules_ids |
| 63 | + .into_iter() |
| 64 | + .flat_map(|id| exceptions_map.remove(&id).unwrap_or_default()) |
| 65 | + .collect(); |
| 66 | + |
| 67 | + Ok(exceptions) |
| 68 | + } |
| 69 | +} |
| 70 | + |
36 | 71 | impl From<TrainScheduleException> for PacedTrainException { |
37 | 72 | fn from(train_schedule_exception: TrainScheduleException) -> Self { |
38 | 73 | let exception_type = match train_schedule_exception.occurrence_index { |
|
0 commit comments