working perfectly well now

This commit is contained in:
filipriec
2025-02-21 14:58:52 +01:00
parent 7771299c81
commit 03e41eda9d
8 changed files with 82 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
-- Add migration script here
CREATE TABLE adresar (
id BIGSERIAL PRIMARY KEY,
deleted BOOLEAN NOT NULL DEFAULT FALSE,
firma TEXT NOT NULL,
kz TEXT,
drc TEXT,
@@ -16,5 +17,6 @@ CREATE TABLE adresar (
telefon TEXT,
skladu TEXT,
fax TEXT,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

View File

@@ -9,7 +9,8 @@ pub async fn delete_adresar(
) -> Result<DeleteAdresarResponse, Status> {
let rows_affected = sqlx::query!(
r#"
DELETE FROM adresar
UPDATE adresar
SET deleted = true
WHERE id = $1
"#,
request.id
@@ -23,3 +24,4 @@ pub async fn delete_adresar(
success: rows_affected > 0,
})
}

View File

@@ -11,7 +11,24 @@ pub async fn get_adresar(
let adresar = sqlx::query_as!(
Adresar,
r#"
SELECT id, firma, kz, drc, ulica, psc, mesto, stat, banka, ucet, skladm, ico, kontakt, telefon, skladu, fax
SELECT
id,
deleted,
firma,
kz,
drc,
ulica,
psc,
mesto,
stat,
banka,
ucet,
skladm,
ico,
kontakt,
telefon,
skladu,
fax
FROM adresar
WHERE id = $1
"#,
@@ -40,3 +57,4 @@ pub async fn get_adresar(
fax: adresar.fax.unwrap_or_default(),
})
}

View File

@@ -1,4 +1,4 @@
// src/adresar/handlers/get_by_position.rs
// src/adresar/handlers/get_adresar_by_position.rs
use tonic::{Status};
use sqlx::PgPool;
use crate::proto::multieko2::{PositionRequest, AdresarResponse, GetAdresarRequest};
@@ -11,16 +11,24 @@ pub async fn get_adresar_by_position(
if request.position < 1 {
return Err(Status::invalid_argument("Position must be at least 1"));
}
let offset = request.position - 1;
// Find the ID of the Nth non-deleted record
let id: i64 = sqlx::query_scalar!(
"SELECT id FROM adresar ORDER BY id ASC OFFSET $1 LIMIT 1",
offset
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
}

View File

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

View File

@@ -12,9 +12,16 @@ pub async fn post_adresar(
Adresar,
r#"
INSERT INTO adresar (
firma, kz, drc, ulica, psc, mesto, stat, banka, ucet, skladm, ico, kontakt, telefon, skladu, fax
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
RETURNING id, firma, kz, drc, ulica, psc, mesto, stat, banka, ucet, skladm, ico, kontakt, telefon, skladu, fax
firma, kz, drc, ulica, psc, mesto, stat, banka, ucet,
skladm, ico, kontakt, telefon, skladu, fax, deleted
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9,
$10, $11, $12, $13, $14, $15, $16
)
RETURNING
id, deleted, firma, kz, drc, ulica, psc, mesto, stat,
banka, ucet, skladm, ico, kontakt, telefon, skladu, fax
"#,
request.firma,
request.kz,
@@ -30,7 +37,8 @@ pub async fn post_adresar(
request.kontakt,
request.telefon,
request.skladu,
request.fax
request.fax,
false // Set deleted to false by default
)
.fetch_one(db_pool)
.await
@@ -38,6 +46,8 @@ pub async fn post_adresar(
Ok(AdresarResponse {
id: adresar.id,
// Do not include `deleted` in the response since it's not
// defined in the proto message.
firma: adresar.firma,
kz: adresar.kz.unwrap_or_default(),
drc: adresar.drc.unwrap_or_default(),
@@ -55,3 +65,4 @@ pub async fn post_adresar(
fax: adresar.fax.unwrap_or_default(),
})
}

View File

@@ -28,8 +28,25 @@ pub async fn put_adresar(
telefon = $14,
skladu = $15,
fax = $16
WHERE id = $1
RETURNING id, firma, kz, drc, ulica, psc, mesto, stat, banka, ucet, skladm, ico, kontakt, telefon, skladu, fax
WHERE id = $1 AND deleted = FALSE
RETURNING
id,
deleted,
firma,
kz,
drc,
ulica,
psc,
mesto,
stat,
banka,
ucet,
skladm,
ico,
kontakt,
telefon,
skladu,
fax
"#,
request.id,
request.firma,
@@ -71,3 +88,4 @@ pub async fn put_adresar(
fax: adresar.fax.unwrap_or_default(),
})
}

View File

@@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Adresar {
pub id: i64,
pub deleted: bool,
pub firma: String,
pub kz: Option<String>,
pub drc: Option<String>,