35 lines
1023 B
Rust
35 lines
1023 B
Rust
// src/adresar/handlers/get_adresar_by_position.rs
|
|
use tonic::{Status};
|
|
use sqlx::PgPool;
|
|
use crate::proto::multieko2::{PositionRequest, AdresarResponse, GetAdresarRequest};
|
|
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
|
|
}
|