|
| 1 | +use crate::error::Result; |
| 2 | +use crate::models::List; |
| 3 | +use crate::models::Ordering; |
| 4 | +use crate::modelsv2::Model; |
| 5 | +use crate::modelsv2::Row; |
| 6 | +use crate::views::pagination::Paginate; |
| 7 | +use crate::views::pagination::PaginatedResponse; |
| 8 | +use async_trait::async_trait; |
| 9 | +use chrono::NaiveDateTime; |
| 10 | +use diesel::sql_query; |
| 11 | +use diesel::sql_types::{BigInt, Text}; |
| 12 | +use diesel::{ExpressionMethods, QueryDsl}; |
| 13 | +use diesel_async::{AsyncPgConnection as PgConnection, RunQueryDsl}; |
| 14 | +use editoast_derive::ModelV2; |
| 15 | +use serde_derive::{Deserialize, Serialize}; |
| 16 | + |
| 17 | +#[derive(Debug, Clone, ModelV2, Deserialize, Serialize)] |
| 18 | +#[model(table = crate::tables::scenariov2)] |
| 19 | +#[model(changeset(public))] |
| 20 | +pub struct ScenarioV2 { |
| 21 | + pub id: i64, |
| 22 | + pub infra_id: i64, |
| 23 | + pub name: String, |
| 24 | + pub description: String, |
| 25 | + pub creation_date: NaiveDateTime, |
| 26 | + pub last_modification: NaiveDateTime, |
| 27 | + pub tags: Vec<Option<String>>, |
| 28 | + pub timetable_id: i64, |
| 29 | + pub study_id: i64, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Debug, Clone, Deserialize, Serialize, QueryableByName)] |
| 33 | +pub struct ScenarioV2WithDetails { |
| 34 | + #[serde(flatten)] |
| 35 | + #[diesel(embed)] |
| 36 | + pub scenario: ScenarioV2, |
| 37 | + #[diesel(sql_type = Text)] |
| 38 | + pub infra_name: String, |
| 39 | + #[diesel(sql_type = BigInt)] |
| 40 | + pub trains_count: i64, |
| 41 | +} |
| 42 | + |
| 43 | +impl ScenarioV2 { |
| 44 | + pub async fn with_details_conn(self, conn: &mut PgConnection) -> Result<ScenarioV2WithDetails> { |
| 45 | + use crate::tables::infra::dsl as infra_dsl; |
| 46 | + use crate::tables::trainschedulev2::dsl::*; |
| 47 | + |
| 48 | + let infra_name = infra_dsl::infra |
| 49 | + .filter(infra_dsl::id.eq(self.infra_id)) |
| 50 | + .select(infra_dsl::name) |
| 51 | + .first::<String>(conn) |
| 52 | + .await?; |
| 53 | + |
| 54 | + let trains_count = trainschedulev2 |
| 55 | + .filter(timetable_id.eq(self.timetable_id)) |
| 56 | + .count() |
| 57 | + .get_result(conn) |
| 58 | + .await?; |
| 59 | + |
| 60 | + Ok(ScenarioV2WithDetails { |
| 61 | + scenario: self, |
| 62 | + infra_name, |
| 63 | + trains_count, |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[async_trait] |
| 69 | +impl List<(i64, Ordering)> for ScenarioV2 { |
| 70 | + /// List all scenarios with the number of trains. |
| 71 | + /// This functions takes a study_id to filter scenarios. |
| 72 | + async fn list_conn( |
| 73 | + conn: &mut PgConnection, |
| 74 | + page: i64, |
| 75 | + page_size: i64, |
| 76 | + params: (i64, Ordering), |
| 77 | + ) -> Result<PaginatedResponse<Self>> { |
| 78 | + let study_id = params.0; |
| 79 | + let ordering = params.1.to_sql(); |
| 80 | + let scenario_rows = sql_query(format!("WITH scenarios_with_train_counts AS ( |
| 81 | + SELECT t.*, COUNT(train_schedule.id) as trains_count |
| 82 | + FROM scenariov2 as t |
| 83 | + LEFT JOIN train_schedule ON t.timetable_id = train_schedule.timetable_id WHERE t.study_id = $1 |
| 84 | + GROUP BY t.id ORDER BY {ordering} |
| 85 | + ) |
| 86 | + SELECT scenarios_with_train_counts.*, infra.name as infra_name |
| 87 | + FROM scenarios_with_train_counts |
| 88 | + JOIN infra ON infra.id = infra_id")) |
| 89 | + .bind::<BigInt, _>(study_id) |
| 90 | + .paginate(page, page_size) |
| 91 | + .load_and_count::<Row<ScenarioV2>>(conn).await?; |
| 92 | + |
| 93 | + let results: Vec<ScenarioV2> = scenario_rows |
| 94 | + .results |
| 95 | + .into_iter() |
| 96 | + .map(Self::from_row) |
| 97 | + .collect(); |
| 98 | + Ok(PaginatedResponse { |
| 99 | + count: scenario_rows.count, |
| 100 | + previous: scenario_rows.previous, |
| 101 | + next: scenario_rows.next, |
| 102 | + results, |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments