feat: add client endpoints
This commit is contained in:
parent
f6cef69a7b
commit
3a87573fd9
27
src/routes/clients.rs
Normal file
27
src/routes/clients.rs
Normal file
|
@ -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<Service>,
|
||||
) -> Result<impl IntoResponse, ServiceError> {
|
||||
let clients = service.get_clients().await?;
|
||||
Ok(Json(clients))
|
||||
}
|
||||
|
||||
async fn client(
|
||||
Path(id): Path<i32>,
|
||||
Extension(service): Extension<Service>,
|
||||
) -> Result<impl IntoResponse, ServiceError> {
|
||||
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<BoxRoute> {
|
||||
Router::new()
|
||||
.route("/", get(clients))
|
||||
.route("/:id", get(client))
|
||||
.boxed()
|
||||
}
|
|
@ -3,11 +3,13 @@ use axum::{Router, routing::BoxRoute};
|
|||
mod rooms;
|
||||
mod clerks;
|
||||
mod bookings;
|
||||
mod clients;
|
||||
|
||||
pub fn routes() -> Router<BoxRoute> {
|
||||
Router::new()
|
||||
.nest("/rooms", rooms::routes())
|
||||
.nest("/bookings", bookings::routes())
|
||||
.nest("/clerks", clerks::routes())
|
||||
.nest("/clients", clients::routes())
|
||||
.boxed()
|
||||
}
|
75
src/sql/clients.rs
Normal file
75
src/sql/clients.rs
Normal file
|
@ -0,0 +1,75 @@
|
|||
use sibyl::Row;
|
||||
|
||||
use crate::{ServiceInner, model::Client, error::ServiceError};
|
||||
|
||||
impl ServiceInner {
|
||||
|
||||
pub async fn get_clients(&self) -> Result<Vec<Client>, 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<Client> = 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<Option<Client>, 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<Client, ServiceError> {
|
||||
Ok(Client {
|
||||
id: row.get(1)?,
|
||||
bank_details: row.get(0)?,
|
||||
person_data: Self::map_person_row(row, 1)?,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
mod bookings;
|
||||
mod rooms;
|
||||
mod clerks;
|
||||
mod clerks;
|
||||
mod clients;
|
Loading…
Reference in a new issue