get by position in general endpoint is now working properly well

This commit is contained in:
filipriec
2025-03-05 18:59:19 +01:00
parent 1a3c063fdd
commit c27fa586d3
6 changed files with 187 additions and 4 deletions

View File

@@ -1,15 +1,15 @@
// src/server/services/tables_data_service.rs
use tonic::{Request, Response, Status};
use common::proto::multieko2::tables_data::tables_data_server::TablesData;
use common::proto::multieko2::common::CountResponse;
use common::proto::multieko2::common::{CountResponse, PositionRequest};
use common::proto::multieko2::tables_data::{
PostTableDataRequest, PostTableDataResponse,
PutTableDataRequest, PutTableDataResponse,
DeleteTableDataRequest, DeleteTableDataResponse,
GetTableDataRequest, GetTableDataResponse,
GetTableDataCountRequest,
GetTableDataCountRequest, GetTableDataByPositionRequest,
};
use crate::tables_data::handlers::{post_table_data, put_table_data, delete_table_data, get_table_data, get_table_data_count};
use crate::tables_data::handlers::{post_table_data, put_table_data, delete_table_data, get_table_data, get_table_data_count, get_table_data_by_position};
use sqlx::PgPool;
#[derive(Debug)]
@@ -64,4 +64,13 @@ impl TablesData for TablesDataService {
let response = get_table_data_count(&self.db_pool, request).await?;
Ok(Response::new(response))
}
async fn get_table_data_by_position(
&self,
request: Request<GetTableDataByPositionRequest>,
) -> Result<Response<GetTableDataResponse>, Status> {
let request = request.into_inner();
let response = get_table_data_by_position(&self.db_pool, request).await?;
Ok(Response::new(response))
}
}

View File

@@ -4,9 +4,11 @@ pub mod put_table_data;
pub mod delete_table_data;
pub mod get_table_data;
pub mod get_table_data_count;
pub mod get_table_data_by_position;
pub use post_table_data::post_table_data;
pub use put_table_data::put_table_data;
pub use delete_table_data::delete_table_data;
pub use get_table_data::get_table_data;
pub use get_table_data_count::get_table_data_count;
pub use get_table_data_by_position::get_table_data_by_position;

View File

@@ -0,0 +1,72 @@
// src/tables_data/handlers/get_table_data_by_position.rs
use tonic::Status;
use sqlx::PgPool;
use common::proto::multieko2::tables_data::{
GetTableDataByPositionRequest, GetTableDataRequest, GetTableDataResponse
};
use super::get_table_data;
pub async fn get_table_data_by_position(
db_pool: &PgPool,
request: GetTableDataByPositionRequest,
) -> Result<GetTableDataResponse, Status> {
let profile_name = request.profile_name;
let table_name = request.table_name;
if request.position < 1 {
return Err(Status::invalid_argument("Position must be at least 1"));
}
let profile = sqlx::query!(
"SELECT id FROM profiles WHERE name = $1",
profile_name
)
.fetch_optional(db_pool)
.await
.map_err(|e| Status::internal(format!("Profile lookup error: {}", e)))?;
let profile_id = profile.ok_or_else(|| Status::not_found("Profile not found"))?.id;
let table_exists = sqlx::query!(
r#"SELECT EXISTS(
SELECT 1 FROM table_definitions
WHERE profile_id = $1 AND table_name = $2
)"#,
profile_id,
table_name
)
.fetch_one(db_pool)
.await
.map_err(|e| Status::internal(format!("Table verification error: {}", e)))?
.exists
.unwrap_or(false);
if !table_exists {
return Err(Status::not_found("Table not found"));
}
let id: i64 = sqlx::query_scalar(
&format!(
r#"SELECT id FROM "{}"
WHERE deleted = FALSE
ORDER BY id ASC
OFFSET $1
LIMIT 1"#,
table_name
)
)
.bind(request.position - 1)
.fetch_optional(db_pool)
.await
.map_err(|e| Status::internal(format!("Position query failed: {}", e)))?
.ok_or_else(|| Status::not_found("Position out of bounds"))?;
get_table_data(
db_pool,
GetTableDataRequest {
profile_name,
table_name,
id,
}
).await
}