Skip to content

Commit

Permalink
editoast: move sql requests from /speed_limit_tags to modelsv2::infra
Browse files Browse the repository at this point in the history
  • Loading branch information
hamz2a committed May 16, 2024
1 parent ad08ba1 commit 95a206f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
18 changes: 18 additions & 0 deletions editoast/src/modelsv2/infra.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod speed_limit_tags;
mod voltage;

use std::pin::Pin;
Expand All @@ -15,6 +16,7 @@ use futures::future::try_join_all;
use futures::Future;
use serde::Deserialize;
use serde::Serialize;
use speed_limit_tags::SpeedLimitTags;
use std::sync::Arc;
use strum::IntoEnumIterator;
use tracing::debug;
Expand Down Expand Up @@ -243,6 +245,22 @@ impl Infra {
let voltages = sql_query(query).load::<Voltage>(conn).await?;
Ok(voltages)
}

pub async fn get_speed_limit_tags(
&self,
conn: &mut DbConnection,
) -> Result<Vec<SpeedLimitTags>> {
let speed_limits_tags = sql_query(
"SELECT DISTINCT jsonb_object_keys(data->'speed_limit_by_tag') AS tag
FROM infra_object_speed_section
WHERE infra_id = $1
ORDER BY tag",
)
.bind::<BigInt, _>(self.id)
.load::<SpeedLimitTags>(conn)
.await?;
Ok(speed_limits_tags)
}
}

#[async_trait]
Expand Down
9 changes: 9 additions & 0 deletions editoast/src/modelsv2/infra/speed_limit_tags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use diesel::sql_types::Text;
use serde::Deserialize;
use serde::Serialize;

#[derive(QueryableByName, Debug, Clone, Serialize, Deserialize)]
pub struct SpeedLimitTags {
#[diesel(sql_type = Text)]
pub tag: String,
}
27 changes: 7 additions & 20 deletions editoast/src/views/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ use actix_web::Either;
use actix_web::HttpResponse;
use actix_web::Responder;
use chashmap::CHashMap;
use diesel::sql_query;
use diesel::sql_types::BigInt;
use diesel::sql_types::Text;
use diesel::QueryableByName;
use editoast_derive::EditoastError;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -141,12 +137,6 @@ impl From<InfraForm> for Changeset<Infra> {
}
}

#[derive(QueryableByName, Debug, Clone, Serialize, Deserialize)]
struct SpeedLimitTags {
#[diesel(sql_type = Text)]
tag: String,
}

#[derive(Debug, Deserialize)]
struct RefreshQueryParams {
#[serde(default)]
Expand Down Expand Up @@ -419,17 +409,12 @@ async fn get_speed_limit_tags(
infra: Path<InfraIdParam>,
db_pool: Data<DbConnectionPool>,
) -> Result<Json<Vec<String>>> {
use diesel_async::RunQueryDsl;
let mut conn = db_pool.get().await?;
let speed_limits_tags: Vec<SpeedLimitTags> = sql_query(
"SELECT DISTINCT jsonb_object_keys(data->'speed_limit_by_tag') AS tag
FROM infra_object_speed_section
WHERE infra_id = $1
ORDER BY tag",
)
.bind::<BigInt, _>(infra.infra_id)
.load(&mut conn)
let conn = &mut db_pool.get().await?;
let infra = Infra::retrieve_or_fail(conn, infra.infra_id, || InfraApiError::NotFound {
infra_id: infra.infra_id,
})
.await?;
let speed_limits_tags = infra.get_speed_limit_tags(conn).await?;
Ok(Json(
speed_limits_tags.into_iter().map(|el| (el.tag)).collect(),
))
Expand Down Expand Up @@ -594,6 +579,8 @@ pub mod tests {
use actix_web::test::call_service;
use actix_web::test::read_body_json;
use actix_web::test::TestRequest;
use diesel::sql_query;
use diesel::sql_types::BigInt;
use diesel_async::RunQueryDsl;
use rstest::*;
use serde_json::json;
Expand Down

0 comments on commit 95a206f

Please sign in to comment.