Skip to content

Commit d815eeb

Browse files
committed
editoast: add train import command for train schedule v2
1 parent a65a503 commit d815eeb

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

editoast/src/client/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ pub enum Commands {
5050
Search(SearchCommands),
5151
#[command(subcommand, about, long_about = "Infrastructure related commands")]
5252
Infra(InfraCommands),
53+
#[command(subcommand, about, long_about = "Trains related commands")]
54+
Trains(TrainsCommands),
55+
}
56+
57+
#[derive(Subcommand, Debug)]
58+
pub enum TrainsCommands {
59+
Import(ImportTrainArgs),
60+
}
61+
62+
#[derive(Args, Debug, Derivative)]
63+
#[derivative(Default)]
64+
#[command(about, long_about = "Import a train given a file")]
65+
pub struct ImportTrainArgs {
66+
#[arg(long)]
67+
pub timetable: Option<String>,
68+
pub path: String,
5369
}
5470

5571
#[derive(Subcommand, Debug)]

editoast/src/main.rs

+53-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ use crate::core::CoreClient;
2121
use crate::error::InternalError;
2222
use crate::map::redis_utils::RedisClient;
2323
use crate::models::{Create, Delete, Infra};
24+
use crate::modelsv2::timetable::Timetable;
25+
use crate::modelsv2::Retrieve as RetrieveV2;
26+
use crate::modelsv2::{Create as CreateV2, Model};
2427
use crate::schema::electrical_profiles::ElectricalProfileSetData;
28+
use crate::schema::v2::trainschedule::TrainScheduleBase;
2529
use crate::schema::RailJson;
2630
use crate::views::infra::InfraForm;
2731
use crate::views::OpenApiRoot;
@@ -34,10 +38,13 @@ use chashmap::CHashMap;
3438
use clap::Parser;
3539
use client::{
3640
ClearArgs, Client, Color, Commands, DeleteProfileSetArgs, ElectricalProfilesCommands,
37-
GenerateArgs, ImportProfileSetArgs, ImportRailjsonArgs, ImportRollingStockArgs, InfraCloneArgs,
38-
InfraCommands, ListProfileSetArgs, MakeMigrationArgs, RedisConfig, RefreshArgs, RunserverArgs,
39-
SearchCommands,
41+
GenerateArgs, ImportProfileSetArgs, ImportRailjsonArgs, ImportRollingStockArgs,
42+
ImportTrainArgs, InfraCloneArgs, InfraCommands, ListProfileSetArgs, MakeMigrationArgs,
43+
RedisConfig, RefreshArgs, RunserverArgs, SearchCommands, TrainsCommands,
4044
};
45+
use modelsv2::train_schedule::TrainScheduleChangeset;
46+
use views::v2::train_schedule::TrainScheduleForm;
47+
4148
use colored::*;
4249
use diesel::{sql_query, ConnectionError, ConnectionResult};
4350
use diesel_async::pooled_connection::deadpool::Pool;
@@ -184,7 +191,50 @@ async fn run() -> Result<(), Box<dyn Error + Send + Sync>> {
184191
}
185192
InfraCommands::ImportRailjson(args) => import_railjson(args, create_db_pool()?).await,
186193
},
194+
Commands::Trains(subcommand) => match subcommand {
195+
TrainsCommands::Import(args) => trains_import(args, create_db_pool()?).await,
196+
},
197+
}
198+
}
199+
200+
async fn trains_import(
201+
args: ImportTrainArgs,
202+
db_pool: Data<DbPool>,
203+
) -> Result<(), Box<dyn Error + Send + Sync>> {
204+
let conn = &mut db_pool.get().await?;
205+
let timetable = match args.timetable {
206+
Some(timetable) => match Timetable::retrieve(conn, timetable.parse::<i64>()?).await? {
207+
Some(timetable) => timetable,
208+
None => {
209+
let error = CliError::new(1, format!("❌ Timetable not found, Name: {timetable}"));
210+
return Err(Box::new(error));
211+
}
212+
},
213+
None => {
214+
let changeset = Timetable::changeset();
215+
changeset.create(conn).await?
216+
}
217+
};
218+
219+
let train_file = File::open(args.path)?;
220+
let train_schedules: Vec<TrainScheduleBase> =
221+
serde_json::from_reader(BufReader::new(train_file))?;
222+
let ts_length = train_schedules.len();
223+
224+
for train_schedule in train_schedules {
225+
let form = TrainScheduleForm {
226+
timetable_id: timetable.id,
227+
train_schedule,
228+
};
229+
let changeset: TrainScheduleChangeset = form.into();
230+
changeset.create(conn).await?;
187231
}
232+
233+
println!(
234+
"✅ {} train schedules created for timetable with id {}", ts_length, timetable.id
235+
);
236+
237+
Ok(())
188238
}
189239

190240
fn init_sentry(args: &RunserverArgs) -> Option<ClientInitGuard> {

0 commit comments

Comments
 (0)