Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editoast: move sql requests from views::rolling_stock to modelsv2::rolling_stock #7470

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions editoast/src/modelsv2/rolling_stock_model.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod power_restrictions;
mod rolling_stock_usage;

use std::collections::HashMap;

use diesel::ExpressionMethods;
Expand All @@ -13,6 +16,8 @@ use editoast_schemas::rolling_stock::RollingResistance;
use editoast_schemas::rolling_stock::RollingStock;
use editoast_schemas::rolling_stock::RollingStockMetadata;
use editoast_schemas::rolling_stock::RollingStockSupportedSignalingSystems;
use power_restrictions::PowerRestriction;
pub use rolling_stock_usage::TrainScheduleScenarioStudyProject;
use serde::Deserialize;
use serde::Serialize;
use std::sync::Arc;
Expand All @@ -29,6 +34,8 @@ use crate::views::rolling_stocks::RollingStockWithLiveries;

editoast_common::schemas! {
RollingStockModel,
PowerRestriction,
TrainScheduleScenarioStudyProject,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, ModelV2, ToSchema)]
Expand Down
29 changes: 29 additions & 0 deletions editoast/src/modelsv2/rolling_stock_model/power_restrictions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use diesel::sql_query;
use diesel::sql_types::Text;
use diesel_async::RunQueryDsl;
use serde::Deserialize;
use serde::Serialize;
use utoipa::ToSchema;

use super::RollingStockModel;
use crate::error::Result;
use crate::modelsv2::DbConnection;

editoast_common::schemas! {
PowerRestriction,
}

#[derive(QueryableByName, Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct PowerRestriction {
#[diesel(sql_type = Text)]
pub power_restriction: String,
}

impl RollingStockModel {
pub async fn get_power_restrictions(conn: &mut DbConnection) -> Result<Vec<PowerRestriction>> {
let power_restrictions = sql_query(include_str!("sql/get_power_restrictions.sql"))
.load::<PowerRestriction>(conn)
.await?;
Ok(power_restrictions)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use diesel::sql_query;
use diesel::sql_types::BigInt;
use diesel::sql_types::Text;
use diesel_async::RunQueryDsl;
use serde::Deserialize;
use serde::Serialize;
use utoipa::ToSchema;

use super::RollingStockModel;
use crate::error::Result;
use crate::modelsv2::DbConnection;

editoast_common::schemas! {
TrainScheduleScenarioStudyProject,
}

#[derive(Debug, QueryableByName, Serialize, Deserialize, PartialEq, ToSchema)]
pub struct TrainScheduleScenarioStudyProject {
#[diesel(sql_type = BigInt)]
pub train_schedule_id: i64,
#[diesel(sql_type = Text)]
pub train_name: String,
#[diesel(sql_type = BigInt)]
pub project_id: i64,
#[diesel(sql_type = Text)]
pub project_name: String,
#[diesel(sql_type = BigInt)]
pub study_id: i64,
#[diesel(sql_type = Text)]
pub study_name: String,
#[diesel(sql_type = BigInt)]
pub scenario_id: i64,
#[diesel(sql_type = Text)]
pub scenario_name: String,
}

impl RollingStockModel {
pub async fn get_rolling_stock_usage(
&self,
conn: &mut DbConnection,
) -> Result<Vec<TrainScheduleScenarioStudyProject>> {
let result = sql_query(include_str!("sql/get_train_schedules_with_scenario.sql"))
.bind::<BigInt, _>(self.id)
.load::<TrainScheduleScenarioStudyProject>(conn)
.await?;
Ok(result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT DISTINCT jsonb_object_keys(power_restrictions) AS power_restriction
FROM rolling_stock
58 changes: 12 additions & 46 deletions editoast/src/views/rolling_stocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ use actix_web::web::Json;
use actix_web::web::Path;
use actix_web::web::Query;
use actix_web::HttpResponse;
use diesel::sql_query;
use diesel::sql_types::BigInt;
use diesel::sql_types::Text as SqlText;
use diesel_async::RunQueryDsl;
use editoast_derive::EditoastError;
use editoast_schemas::rolling_stock::RollingStockLivery;
use editoast_schemas::rolling_stock::RollingStockLiveryMetadata;
Expand All @@ -43,6 +39,7 @@ use crate::error::InternalError;
use crate::error::Result;
use crate::modelsv2::prelude::*;
use crate::modelsv2::rolling_stock_livery::RollingStockLiveryModel;
use crate::modelsv2::rolling_stock_model::TrainScheduleScenarioStudyProject;
use crate::modelsv2::DbConnectionPool;
use crate::modelsv2::Document;
use crate::modelsv2::RollingStockModel;
Expand Down Expand Up @@ -76,10 +73,8 @@ editoast_common::schemas! {
DeleteRollingStockQueryParams,
RollingStockLockedUpdateForm,
RollingStockLiveryCreateForm,
PowerRestriction,
RollingStockError,
RollingStockKey,
TrainScheduleScenarioStudyProject,
RollingStockWithLiveries,
light_rolling_stock::schemas(),
}
Expand Down Expand Up @@ -194,12 +189,6 @@ async fn get_by_name(
Ok(Json(rolling_stock_with_liveries))
}

#[derive(QueryableByName, Debug, Clone, Serialize, Deserialize, ToSchema)]
struct PowerRestriction {
#[diesel(sql_type = SqlText)]
power_restriction: String,
}

/// Returns the set of power restrictions for all rolling_stocks modes.
#[utoipa::path(tag = "rolling_stock",
responses(
Expand All @@ -208,13 +197,8 @@ struct PowerRestriction {
)]
#[get("")]
async fn get_power_restrictions(db_pool: Data<DbConnectionPool>) -> Result<Json<Vec<String>>> {
let mut conn = db_pool.get().await?;
let power_restrictions: Vec<PowerRestriction> = sql_query(
"SELECT DISTINCT jsonb_object_keys(power_restrictions) AS power_restriction
FROM rolling_stock",
)
.load(&mut conn)
.await?;
let conn = &mut db_pool.get().await?;
let power_restrictions = RollingStockModel::get_power_restrictions(conn).await?;
Ok(Json(
power_restrictions
.into_iter()
Expand Down Expand Up @@ -409,38 +393,20 @@ struct RollingStockLiveryCreateForm {
pub images: Vec<TempFile>,
}

#[derive(Debug, QueryableByName, Serialize, Deserialize, PartialEq, ToSchema)]
pub struct TrainScheduleScenarioStudyProject {
#[diesel(sql_type = BigInt)]
pub train_schedule_id: i64,
#[diesel(sql_type = SqlText)]
pub train_name: String,
#[diesel(sql_type = BigInt)]
pub project_id: i64,
#[diesel(sql_type = SqlText)]
pub project_name: String,
#[diesel(sql_type = BigInt)]
pub study_id: i64,
#[diesel(sql_type = SqlText)]
pub study_name: String,
#[diesel(sql_type = BigInt)]
pub scenario_id: i64,
#[diesel(sql_type = SqlText)]
pub scenario_name: String,
}

async fn get_rolling_stock_usage(
db_pool: Data<DbConnectionPool>,
rolling_stock_id: i64,
) -> Result<Vec<TrainScheduleScenarioStudyProject>> {
let mut db_conn = db_pool.get().await?;
RollingStockModel::exists(&mut db_conn, rolling_stock_id).await?;
let conn = &mut db_pool.get().await?;

sql_query(include_str!("sql/get_train_schedules_with_scenario.sql"))
.bind::<BigInt, _>(rolling_stock_id)
.load(&mut db_conn)
.await
.map_err(|e| e.into())
let rolling_stock = RollingStockModel::retrieve_or_fail(conn, rolling_stock_id, || {
RollingStockError::KeyNotFound {
rolling_stock_key: RollingStockKey::Id(rolling_stock_id),
}
})
.await?;

rolling_stock.get_rolling_stock_usage(conn).await
}

/// Create a rolling stock livery
Expand Down
Loading