Skip to content

Commit 933395f

Browse files
Wadjetztheocrsb
authored andcommitted
editoast: retrieve exceptions by ids and timetable utility function (#15797)
This PR add a small utility function to retrieve exceptions by ids and timetable useful for multiple endpoints Signed-off-by: Egor <egor@berezify.fr>
1 parent ec7ad50 commit 933395f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

editoast/editoast_models/src/train_schedule_exception.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
use std::collections::HashMap;
2+
3+
use database::DbConnection;
14
use editoast_derive::Model;
5+
use itertools::Itertools;
26
use schemas::paced_train::PacedTrainException;
37
use schemas::train_schedule_exception::TrainScheduleExceptionChangeGroups;
48

59
use crate as editoast_models;
10+
use crate::prelude::List;
11+
use crate::prelude::SelectionSettings;
612

713
#[derive(Debug, Clone, Model)]
814
#[cfg_attr(test, derive(serde::Deserialize))]
@@ -33,6 +39,35 @@ impl From<TrainScheduleException> for schemas::TrainScheduleException {
3339
}
3440
}
3541

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+
3671
impl From<TrainScheduleException> for PacedTrainException {
3772
fn from(train_schedule_exception: TrainScheduleException) -> Self {
3873
let exception_type = match train_schedule_exception.occurrence_index {

0 commit comments

Comments
 (0)