Skip to content

Commit d1b3faf

Browse files
committed
editoast: move get_rolling_stock_usage sql request from views to modelsv2
1 parent 5b08119 commit d1b3faf

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

editoast/src/modelsv2/rolling_stock_model.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
mod power_restriction;
2+
pub mod train_schedule_scenario_study_project;
23

34
use std::collections::HashMap;
45

56
use diesel::sql_query;
7+
use diesel::sql_types::BigInt;
68
use diesel::ExpressionMethods;
79
use diesel::QueryDsl;
810
use diesel::SelectableHelper;
@@ -20,6 +22,7 @@ use power_restriction::PowerRestriction;
2022
use serde::Deserialize;
2123
use serde::Serialize;
2224
use std::sync::Arc;
25+
use train_schedule_scenario_study_project::TrainScheduleScenarioStudyProject;
2326
use utoipa::ToSchema;
2427
use validator::Validate;
2528
use validator::ValidationError;
@@ -35,6 +38,7 @@ use crate::views::rolling_stocks::RollingStockWithLiveries;
3538
editoast_common::schemas! {
3639
RollingStockModel,
3740
PowerRestriction,
41+
TrainScheduleScenarioStudyProject,
3842
}
3943

4044
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, ModelV2, ToSchema)]
@@ -123,6 +127,19 @@ impl RollingStockModel {
123127
.await?;
124128
Ok(power_restrictions)
125129
}
130+
131+
pub async fn get_rolling_stock_usage(
132+
&self,
133+
conn: &mut DbConnection,
134+
) -> Result<Vec<TrainScheduleScenarioStudyProject>> {
135+
let result = sql_query(include_str!(
136+
"rolling_stock_model/sql/get_train_schedules_with_scenario.sql"
137+
))
138+
.bind::<BigInt, _>(self.id)
139+
.load::<TrainScheduleScenarioStudyProject>(conn)
140+
.await?;
141+
Ok(result)
142+
}
126143
}
127144

128145
impl RollingStockModelChangeset {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use diesel::sql_types::BigInt;
2+
use diesel::sql_types::Text;
3+
use serde::Deserialize;
4+
use serde::Serialize;
5+
use utoipa::ToSchema;
6+
7+
editoast_common::schemas! {
8+
TrainScheduleScenarioStudyProject,
9+
}
10+
11+
#[derive(Debug, QueryableByName, Serialize, Deserialize, PartialEq, ToSchema)]
12+
pub struct TrainScheduleScenarioStudyProject {
13+
#[diesel(sql_type = BigInt)]
14+
pub train_schedule_id: i64,
15+
#[diesel(sql_type = Text)]
16+
pub train_name: String,
17+
#[diesel(sql_type = BigInt)]
18+
pub project_id: i64,
19+
#[diesel(sql_type = Text)]
20+
pub project_name: String,
21+
#[diesel(sql_type = BigInt)]
22+
pub study_id: i64,
23+
#[diesel(sql_type = Text)]
24+
pub study_name: String,
25+
#[diesel(sql_type = BigInt)]
26+
pub scenario_id: i64,
27+
#[diesel(sql_type = Text)]
28+
pub scenario_name: String,
29+
}

editoast/src/views/rolling_stocks/mod.rs

+11-32
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ use actix_web::web::Json;
1818
use actix_web::web::Path;
1919
use actix_web::web::Query;
2020
use actix_web::HttpResponse;
21-
use diesel::sql_query;
22-
use diesel::sql_types::BigInt;
23-
use diesel::sql_types::Text as SqlText;
24-
use diesel_async::RunQueryDsl;
2521
use editoast_derive::EditoastError;
2622
use editoast_schemas::rolling_stock::RollingStockLivery;
2723
use editoast_schemas::rolling_stock::RollingStockLiveryMetadata;
@@ -43,6 +39,7 @@ use crate::error::InternalError;
4339
use crate::error::Result;
4440
use crate::modelsv2::prelude::*;
4541
use crate::modelsv2::rolling_stock_livery::RollingStockLiveryModel;
42+
use crate::modelsv2::rolling_stock_model::train_schedule_scenario_study_project::TrainScheduleScenarioStudyProject;
4643
use crate::modelsv2::DbConnectionPool;
4744
use crate::modelsv2::Document;
4845
use crate::modelsv2::RollingStockModel;
@@ -78,7 +75,6 @@ editoast_common::schemas! {
7875
RollingStockLiveryCreateForm,
7976
RollingStockError,
8077
RollingStockKey,
81-
TrainScheduleScenarioStudyProject,
8278
RollingStockWithLiveries,
8379
light_rolling_stock::schemas(),
8480
}
@@ -397,38 +393,21 @@ struct RollingStockLiveryCreateForm {
397393
pub images: Vec<TempFile>,
398394
}
399395

400-
#[derive(Debug, QueryableByName, Serialize, Deserialize, PartialEq, ToSchema)]
401-
pub struct TrainScheduleScenarioStudyProject {
402-
#[diesel(sql_type = BigInt)]
403-
pub train_schedule_id: i64,
404-
#[diesel(sql_type = SqlText)]
405-
pub train_name: String,
406-
#[diesel(sql_type = BigInt)]
407-
pub project_id: i64,
408-
#[diesel(sql_type = SqlText)]
409-
pub project_name: String,
410-
#[diesel(sql_type = BigInt)]
411-
pub study_id: i64,
412-
#[diesel(sql_type = SqlText)]
413-
pub study_name: String,
414-
#[diesel(sql_type = BigInt)]
415-
pub scenario_id: i64,
416-
#[diesel(sql_type = SqlText)]
417-
pub scenario_name: String,
418-
}
419-
420396
async fn get_rolling_stock_usage(
421397
db_pool: Data<DbConnectionPool>,
422398
rolling_stock_id: i64,
423399
) -> Result<Vec<TrainScheduleScenarioStudyProject>> {
424-
let mut db_conn = db_pool.get().await?;
425-
RollingStockModel::exists(&mut db_conn, rolling_stock_id).await?;
400+
let conn = &mut db_pool.get().await?;
401+
// RollingStockModel::exists(&mut db_conn, rolling_stock_id).await?;
426402

427-
sql_query(include_str!("sql/get_train_schedules_with_scenario.sql"))
428-
.bind::<BigInt, _>(rolling_stock_id)
429-
.load(&mut db_conn)
430-
.await
431-
.map_err(|e| e.into())
403+
let rolling_stock = RollingStockModel::retrieve_or_fail(conn, rolling_stock_id, || {
404+
RollingStockError::KeyNotFound {
405+
rolling_stock_key: RollingStockKey::Id(rolling_stock_id),
406+
}
407+
})
408+
.await?;
409+
410+
rolling_stock.get_rolling_stock_usage(conn).await
432411
}
433412

434413
/// Create a rolling stock livery

0 commit comments

Comments
 (0)