diff --git a/editoast/src/views/layers/mod.rs b/editoast/src/views/layers/mod.rs index aa660a926aa..7e1b54f90f4 100644 --- a/editoast/src/views/layers/mod.rs +++ b/editoast/src/views/layers/mod.rs @@ -8,13 +8,10 @@ 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::Integer; -use diesel_async::RunQueryDsl; use editoast_derive::EditoastError; use mvt_utils::create_and_fill_mvt_tile; -use mvt_utils::get_geo_json_sql_query; use mvt_utils::GeoJsonAndData; +use mvt_utils::GeoPoint; use redis::AsyncCommands; use serde::Deserialize; use serde::Serialize; @@ -208,15 +205,9 @@ async fn cache_and_get_mvt_tile( .body(value)); } - let geo_json_query = get_geo_json_sql_query(&layer.table_name, view); - let mut conn = db_pool.get().await?; - let records = sql_query(geo_json_query) - .bind::(z as i32) - .bind::(x as i32) - .bind::(y as i32) - .bind::(infra as i32) - .get_results::(&mut conn) - .await?; + let conn = &mut db_pool.get().await?; + let records = + GeoJsonAndData::get_records(conn, &layer, view, infra, &GeoPoint::new(z, x, y)).await?; let mvt_bytes: Vec = create_and_fill_mvt_tile(layer_slug, records) .to_bytes() diff --git a/editoast/src/views/layers/mvt_utils.rs b/editoast/src/views/layers/mvt_utils.rs index 9d980b36efb..7d9fffde4f6 100644 --- a/editoast/src/views/layers/mvt_utils.rs +++ b/editoast/src/views/layers/mvt_utils.rs @@ -1,5 +1,10 @@ +use diesel::sql_query; +use diesel::sql_types::Integer; use diesel::sql_types::Jsonb; use diesel::sql_types::Text; +use diesel_async::RunQueryDsl; +use editoast_models::DbConnection; +use editoast_models::EditoastModelsError; use geos::geojson::Geometry; use geos::geojson::Value as GeoJsonValue; use mvt::Feature; @@ -11,6 +16,7 @@ use serde::Deserialize; use serde::Serialize; use serde_json::Value as JsonValue; +use crate::map::Layer; use crate::map::View; #[derive(Clone, QueryableByName, Queryable, Debug, Serialize, Deserialize)] @@ -31,7 +37,39 @@ fn geometry_into_mvt_geom_type(geometry: &Geometry) -> GeomType { } } +#[derive(Debug)] +pub struct GeoPoint { + x: u64, + y: u64, + z: u64, +} + +impl GeoPoint { + pub fn new(x: u64, y: u64, z: u64) -> Self { + Self { x, y, z } + } +} + impl GeoJsonAndData { + pub async fn get_records( + conn: &mut DbConnection, + layer: &Layer, + view: &View, + infra: i64, + geo_point: &GeoPoint, + ) -> Result, EditoastModelsError> { + let geo_json_query = get_geo_json_sql_query(&layer.table_name, view); + let records = sql_query(geo_json_query) + .bind::(geo_point.z as i32) + .bind::(geo_point.x as i32) + .bind::(geo_point.y as i32) + .bind::(infra as i32) + .get_results::(conn) + .await?; + + Ok(records) + } + /// Converts GeoJsonAndData as mvt GeomData pub fn as_geom_data(&self) -> GeomData { let geo_json = serde_json::from_str::(&self.geo_json).unwrap();