working get count properly for adresar

This commit is contained in:
filipriec
2025-02-18 14:40:23 +01:00
parent 93968b2b2f
commit 9ff6dada40
7 changed files with 113 additions and 2 deletions

View File

@@ -0,0 +1,33 @@
# TOTAL items in the adresar
grpcurl -plaintext localhost:50051 multieko2.Adresar/GetAdresarCount
{
"count": "43"
}
# Item at this count. If there are 43 items, number 1 is the first item
grpcurl -plaintext -d '{"position": 1}' localhost:50051 multieko2.Adresar/GetAdresarByPosition
{
"id": "2",
"firma": "asdfasf",
"kz": " ",
"drc": " ",
"ulica": " ",
"psc": "sdfasdf",
"mesto": "asf",
"stat": "as",
"banka": "df",
"ucet": "asf",
"skladm": "f",
"ico": "f",
"kontakt": "f",
"telefon": "f",
"skladu": "f",
"fax": " "
}
# Item fetched by id. The first item was created and removed, therefore number 1 in ids doenst exists, since first to exist now has number 2
grpcurl -plaintext -d '{"id": 1}' localhost:50051 multieko2.Adresar/GetAdresar
ERROR:
Code: NotFound
Message: no rows returned by a query that expected to return at least one row
╭─    ~ ············································· 69 ✘
╰─

View File

@@ -4,8 +4,12 @@ pub mod post_adresar;
pub mod get_adresar;
pub mod put_adresar;
pub mod delete_adresar;
pub mod get_adresar_count;
pub mod get_adresar_by_position;
pub use post_adresar::post_adresar;
pub use get_adresar::get_adresar;
pub use put_adresar::put_adresar;
pub use delete_adresar::delete_adresar;
pub use get_adresar_count::get_adresar_count;
pub use get_adresar_by_position::get_adresar_by_position;

View File

@@ -0,0 +1,26 @@
// src/adresar/handlers/get_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"));
}
let offset = request.position - 1;
let id: i64 = sqlx::query_scalar!(
"SELECT id FROM adresar ORDER BY id ASC OFFSET $1 LIMIT 1",
offset
)
.fetch_optional(db_pool)
.await
.map_err(|e| Status::internal(e.to_string()))?
.ok_or_else(|| Status::not_found("Position out of bounds"))?;
get_adresar(db_pool, GetAdresarRequest { id }).await
}

View File

@@ -0,0 +1,19 @@
// src/adresar/handlers/get_adresar_count.rs
use tonic::{Status};
use sqlx::PgPool;
use crate::proto::multieko2::{CountResponse, Empty};
pub async fn get_adresar_count(
db_pool: &PgPool,
_request: Empty,
) -> Result<CountResponse, Status> {
let count: i64 = sqlx::query_scalar!(
"SELECT COUNT(*) as count FROM adresar"
)
.fetch_one(db_pool)
.await
.map_err(|e| Status::internal(e.to_string()))?
.unwrap_or(0); // Handle the case where the count is None
Ok(CountResponse { count })
}