complete redesign
This commit is contained in:
35
server/src/adresar/handlers/get_adresar_by_position.rs
Normal file
35
server/src/adresar/handlers/get_adresar_by_position.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
// src/adresar/handlers/get_adresar_by_position.rs
|
||||
use tonic::{Status};
|
||||
use sqlx::PgPool;
|
||||
use common::proto::multieko2::adresar::{AdresarResponse, GetAdresarRequest};
|
||||
use common::proto::multieko2::common::PositionRequest;
|
||||
use super::get_adresar;
|
||||
|
||||
pub async fn get_adresar_by_position(
|
||||
db_pool: &PgPool,
|
||||
request: PositionRequest,
|
||||
) -> Result<AdresarResponse, Status> {
|
||||
if request.position < 1 {
|
||||
return Err(Status::invalid_argument("Position must be at least 1"));
|
||||
}
|
||||
|
||||
// Find the ID of the Nth non-deleted record
|
||||
let id: i64 = sqlx::query_scalar!(
|
||||
r#"
|
||||
SELECT id
|
||||
FROM adresar
|
||||
WHERE deleted = FALSE
|
||||
ORDER BY id ASC
|
||||
OFFSET $1
|
||||
LIMIT 1
|
||||
"#,
|
||||
request.position - 1
|
||||
)
|
||||
.fetch_optional(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(e.to_string()))?
|
||||
.ok_or_else(|| Status::not_found("Position out of bounds"))?;
|
||||
|
||||
// Now fetch the complete record using the existing get_adresar function
|
||||
get_adresar(db_pool, GetAdresarRequest { id }).await
|
||||
}
|
||||
Reference in New Issue
Block a user