-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
editoast: add categorie to rolling stock model
Signed-off-by: hamz2a <[email protected]>
- Loading branch information
Showing
15 changed files
with
253 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
mod rolling_stock_category; | ||
pub use rolling_stock_category::RollingStockCategory; | ||
|
||
mod rolling_stock_categories; | ||
pub use rolling_stock_categories::RollingStockCategories; | ||
|
||
editoast_common::schemas! { | ||
rolling_stock_category::schemas(), | ||
rolling_stock_categories::schemas(), | ||
} |
23 changes: 23 additions & 0 deletions
23
editoast/editoast_models/src/rolling_stock/rolling_stock_categories.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use utoipa::ToSchema; | ||
|
||
use super::RollingStockCategory; | ||
|
||
editoast_common::schemas! { | ||
RollingStockCategories, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, ToSchema)] | ||
pub struct RollingStockCategories(pub Vec<RollingStockCategory>); | ||
|
||
impl From<Vec<Option<RollingStockCategory>>> for RollingStockCategories { | ||
fn from(categories: Vec<Option<RollingStockCategory>>) -> Self { | ||
Self(categories.into_iter().flatten().collect()) | ||
} | ||
} | ||
impl From<RollingStockCategories> for Vec<Option<RollingStockCategory>> { | ||
fn from(categories: RollingStockCategories) -> Self { | ||
categories.0.into_iter().map(Some).collect() | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
editoast/editoast_models/src/rolling_stock/rolling_stock_category.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::io::Write; | ||
use std::str::FromStr; | ||
|
||
use diesel::deserialize::FromSql; | ||
use diesel::deserialize::FromSqlRow; | ||
use diesel::expression::AsExpression; | ||
use diesel::pg::Pg; | ||
use diesel::pg::PgValue; | ||
use diesel::serialize::Output; | ||
use diesel::serialize::ToSql; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use strum::Display; | ||
use strum::EnumString; | ||
use strum::IntoStaticStr; | ||
use utoipa::ToSchema; | ||
|
||
editoast_common::schemas! { | ||
RollingStockCategory, | ||
} | ||
|
||
#[derive( | ||
Debug, | ||
Clone, | ||
PartialEq, | ||
Serialize, | ||
Deserialize, | ||
ToSchema, | ||
FromSqlRow, | ||
AsExpression, | ||
EnumString, | ||
IntoStaticStr, | ||
Display, | ||
)] | ||
#[diesel(sql_type = crate::tables::sql_types::RollingStockCategory)] | ||
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")] | ||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")] | ||
pub enum RollingStockCategory { | ||
Unknown, | ||
HighSpeedTrain, | ||
IntercityTrain, | ||
RegionalTrainMU, | ||
NightTrain, | ||
CommuterTrain, | ||
FreightTrain, | ||
FastFreightTrain, | ||
TramTrain, | ||
TouristicTrain, | ||
WorkTrain, | ||
} | ||
|
||
impl FromSql<crate::tables::sql_types::RollingStockCategory, Pg> for RollingStockCategory { | ||
fn from_sql(value: PgValue) -> diesel::deserialize::Result<Self> { | ||
let s = std::str::from_utf8(value.as_bytes()).map_err(|_| "Invalid UTF-8 data")?; | ||
RollingStockCategory::from_str(s) | ||
.map_err(|_| "Unrecognized enum variant for RollingStockCategory".into()) | ||
} | ||
} | ||
|
||
impl ToSql<crate::tables::sql_types::RollingStockCategory, Pg> for RollingStockCategory { | ||
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> diesel::serialize::Result { | ||
let variant: &str = self.into(); | ||
out.write_all(variant.as_bytes())?; | ||
Ok(diesel::serialize::IsNull::No) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...t/migrations/2025-02-03-094532_add_primary_and_other_categories_to_rolling_stock/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ALTER TABLE rolling_stock | ||
DROP COLUMN primary_category, | ||
DROP COLUMN other_categories; | ||
|
||
DROP TYPE rolling_stock_category; |
17 changes: 17 additions & 0 deletions
17
...ast/migrations/2025-02-03-094532_add_primary_and_other_categories_to_rolling_stock/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
CREATE TYPE rolling_stock_category AS ENUM ( | ||
'UNKNOWN', | ||
'HIGH_SPEED_TRAIN', | ||
'INTERCITY_TRAIN', | ||
'REGIONAL_TRAIN_MU', | ||
'NIGHT_TRAIN', | ||
'COMMUTER_TRAIN', | ||
'FREIGHT_TRAIN', | ||
'FAST_FREIGHT_TRAIN', | ||
'TRAM_TRAIN', | ||
'TOURISTIC_TRAIN', | ||
'WORK_TRAIN' | ||
); | ||
|
||
ALTER TABLE rolling_stock | ||
ADD COLUMN primary_category rolling_stock_category NOT NULL DEFAULT 'UNKNOWN', | ||
ADD COLUMN other_categories rolling_stock_category[] NOT NULL DEFAULT '{}'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.