-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtrain_schedule.rs
105 lines (100 loc) · 3.24 KB
/
train_schedule.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use chrono::DateTime;
use chrono::Utc;
use editoast_derive::Model;
use editoast_models::DbConnection;
use crate::error::Result;
use editoast_schemas::train_schedule::Comfort;
use editoast_schemas::train_schedule::Distribution;
use editoast_schemas::train_schedule::Margins;
use editoast_schemas::train_schedule::PathItem;
use editoast_schemas::train_schedule::PowerRestrictionItem;
use editoast_schemas::train_schedule::ScheduleItem;
use editoast_schemas::train_schedule::TrainScheduleBase;
use editoast_schemas::train_schedule::TrainScheduleOptions;
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use futures_util::stream::TryStreamExt;
use std::ops::DerefMut;
use super::Model as _;
use crate::diesel::ExpressionMethods;
use crate::Row;
#[derive(Debug, Default, Clone, Model)]
#[model(table = editoast_models::tables::train_schedule)]
#[model(gen(ops = crud, batch_ops = crd, list))]
pub struct TrainSchedule {
pub id: i64,
pub train_name: String,
pub labels: Vec<Option<String>>,
pub rolling_stock_name: String,
pub timetable_id: i64,
pub start_time: DateTime<Utc>,
#[model(json)]
pub schedule: Vec<ScheduleItem>,
#[model(json)]
pub margins: Margins,
pub initial_speed: f64,
#[model(to_enum)]
pub comfort: Comfort,
#[model(json)]
pub path: Vec<PathItem>,
#[model(to_enum)]
pub constraint_distribution: Distribution,
pub speed_limit_tag: Option<String>,
#[model(json)]
pub power_restrictions: Vec<PowerRestrictionItem>,
#[model(json)]
pub options: TrainScheduleOptions,
}
impl From<TrainScheduleBase> for TrainScheduleChangeset {
fn from(
TrainScheduleBase {
train_name,
labels,
rolling_stock_name,
start_time,
path,
schedule,
margins,
initial_speed,
comfort,
constraint_distribution,
speed_limit_tag,
power_restrictions,
options,
}: TrainScheduleBase,
) -> Self {
TrainSchedule::changeset()
.comfort(comfort)
.constraint_distribution(constraint_distribution)
.initial_speed(initial_speed)
.labels(labels.into_iter().map(Some).collect())
.margins(margins)
.path(path)
.power_restrictions(power_restrictions)
.rolling_stock_name(rolling_stock_name)
.schedule(schedule)
.speed_limit_tag(speed_limit_tag.map(|s| s.0))
.start_time(start_time)
.train_name(train_name)
.options(options)
}
}
pub async fn filter_train_by_start_time_and_timetable(
conn: &mut DbConnection,
time: DateTime<Utc>,
timetable_id: i64,
) -> Result<Vec<TrainSchedule>> {
use editoast_models::tables::train_schedule::dsl;
let train_schedules = dsl::train_schedule
.filter(dsl::start_time.le(time))
.filter(dsl::timetable_id.eq(timetable_id))
.load_stream::<Row<TrainSchedule>>(conn.write().await.deref_mut())
.await?
.map_ok(|ts| ts.into())
.try_collect::<Vec<TrainSchedule>>()
.await;
match train_schedules {
Ok(train_schedules) => Ok(train_schedules),
Err(err) => Err(err.into()),
}
}