From 3a87573fd9fc164ab18b8be0c92c0b3d3e7eb105 Mon Sep 17 00:00:00 2001 From: Timo Ley Date: Fri, 23 Jun 2023 17:12:13 +0200 Subject: [PATCH] feat: add client endpoints --- src/routes/clients.rs | 27 ++++++++++++++++ src/routes/mod.rs | 2 ++ src/sql/clients.rs | 75 +++++++++++++++++++++++++++++++++++++++++++ src/sql/mod.rs | 3 +- 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/routes/clients.rs create mode 100644 src/sql/clients.rs diff --git a/src/routes/clients.rs b/src/routes/clients.rs new file mode 100644 index 0000000..3923ea9 --- /dev/null +++ b/src/routes/clients.rs @@ -0,0 +1,27 @@ +use axum::{Router, routing::BoxRoute, extract::{Extension, Path}, response::IntoResponse, Json, handler::get}; +use hyper::StatusCode; + +use crate::{Service, error::ServiceError}; + +async fn clients( + Extension(service): Extension, +) -> Result { + let clients = service.get_clients().await?; + Ok(Json(clients)) +} + +async fn client( + Path(id): Path, + Extension(service): Extension, +) -> Result { + let client = service.get_client(id).await? + .ok_or_else(|| ServiceError::ErrorResponse(StatusCode::NOT_FOUND, Some("client not found".to_string())))?; + Ok(Json(client)) +} + +pub fn routes() -> Router { + Router::new() + .route("/", get(clients)) + .route("/:id", get(client)) + .boxed() +} \ No newline at end of file diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 42eae38..61899d7 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -3,11 +3,13 @@ use axum::{Router, routing::BoxRoute}; mod rooms; mod clerks; mod bookings; +mod clients; pub fn routes() -> Router { Router::new() .nest("/rooms", rooms::routes()) .nest("/bookings", bookings::routes()) .nest("/clerks", clerks::routes()) + .nest("/clients", clients::routes()) .boxed() } \ No newline at end of file diff --git a/src/sql/clients.rs b/src/sql/clients.rs new file mode 100644 index 0000000..af595d5 --- /dev/null +++ b/src/sql/clients.rs @@ -0,0 +1,75 @@ +use sibyl::Row; + +use crate::{ServiceInner, model::Client, error::ServiceError}; + +impl ServiceInner { + + pub async fn get_clients(&self) -> Result, ServiceError> { + let session = self.pool.get_session().await?; + let stmt = session.prepare(" + SELECT + c.bankDetails, + p.personId, + p.name, + p.lastName, + p.age, + ad.street, + ad.houseNumber, + ad.postalCode, + ad.city, + ad.country + FROM Client c + INNER JOIN Person p ON p.personId = c.personId + INNER JOIN Address ad ON p.addressId = ad.addressId + ").await?; + + let rows = stmt.query("").await?; + + let mut clients: Vec = vec![]; + + while let Some(row) = rows.next().await? { + clients.push(Self::map_client_row(row)?); + } + + Ok(clients) + } + + pub async fn get_client(&self, id: i32) -> Result, ServiceError> { + let session = self.pool.get_session().await?; + let stmt = session.prepare(" + SELECT + c.bankDetails, + p.personId, + p.name, + p.lastName, + p.age, + ad.street, + ad.houseNumber, + ad.postalCode, + ad.city, + ad.country + FROM Client c + INNER JOIN Person p ON p.personId = c.personId + INNER JOIN Address ad ON p.addressId = ad.addressId + WHERE p.personId = :ID + ").await?; + + let row = stmt.query_single(id).await?; + + Ok(match row { + Some(row) => { + Some(Self::map_client_row(row)?) + }, + None => None, + }) + } + + pub fn map_client_row(row: Row) -> Result { + Ok(Client { + id: row.get(1)?, + bank_details: row.get(0)?, + person_data: Self::map_person_row(row, 1)?, + }) + } + +} \ No newline at end of file diff --git a/src/sql/mod.rs b/src/sql/mod.rs index f43fdff..f0834a7 100644 --- a/src/sql/mod.rs +++ b/src/sql/mod.rs @@ -1,3 +1,4 @@ mod bookings; mod rooms; -mod clerks; \ No newline at end of file +mod clerks; +mod clients; \ No newline at end of file