feat: person search endpoint

This commit is contained in:
Timo Ley 2023-06-24 22:27:53 +02:00
parent 9a7f992265
commit 531ca33851
3 changed files with 50 additions and 4 deletions

View file

@ -77,6 +77,12 @@ pub struct ClerkMonthsQuery {
pub months: i32, pub months: i32,
} }
#[derive(Deserialize)]
pub struct PersonQuery {
pub firstname: Option<String>,
pub lastname: Option<String>,
}
#[derive(Deserialize, Clone)] #[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct BookingBody { pub struct BookingBody {

View file

@ -1,7 +1,15 @@
use axum::{Router, routing::BoxRoute, extract::{Path, Extension}, response::IntoResponse, Json, handler::{get, post}}; use axum::{Router, routing::BoxRoute, extract::{Path, Extension, Query}, response::IntoResponse, Json, handler::get};
use hyper::StatusCode; use hyper::StatusCode;
use crate::{Service, error::ServiceError, model::{PersonBody, Person}}; use crate::{Service, error::ServiceError, model::{PersonBody, Person, PersonQuery}};
async fn persons(
Query(query): Query<PersonQuery>,
Extension(service): Extension<Service>,
) -> Result<impl IntoResponse, ServiceError> {
let persons = service.get_persons(query).await?;
Ok(Json(persons))
}
async fn person( async fn person(
Path(id): Path<i32>, Path(id): Path<i32>,
@ -29,7 +37,7 @@ async fn add_person(
pub fn routes() -> Router<BoxRoute> { pub fn routes() -> Router<BoxRoute> {
Router::new() Router::new()
.route("/", post(add_person)) .route("/", get(persons).post(add_person))
.route("/:id", get(person)) .route("/:id", get(person))
.boxed() .boxed()
} }

View file

@ -1,9 +1,41 @@
use hyper::StatusCode; use hyper::StatusCode;
use sibyl::Row; use sibyl::Row;
use crate::{ServiceInner, error::ServiceError, model::{PersonBody, Person, Address}}; use crate::{ServiceInner, error::ServiceError, model::{PersonBody, Person, Address, PersonQuery}};
impl ServiceInner { impl ServiceInner {
pub async fn get_persons(&self, filter: PersonQuery) -> Result<Vec<Person>, ServiceError> {
let session = self.pool.get_session().await?;
let stmt = session.prepare("
SELECT
p.personId,
p.name,
p.lastName,
p.age,
ad.street,
ad.houseNumber,
ad.postalCode,
ad.city,
ad.country
FROM Person p
INNER JOIN Address ad ON p.addressId = ad.addressId
WHERE p.name LIKE :FIRST AND p.lastName LIKE :LAST
").await?;
let rows = stmt.query((
(":FIRST", format!("%{}%", filter.firstname.unwrap_or_default())),
(":LAST", format!("%{}%", filter.lastname.unwrap_or_default()))
)).await?;
let mut persons: Vec<Person> = vec![];
while let Some(row) = rows.next().await? {
persons.push(Self::map_person_row(row, 0)?);
}
Ok(persons)
}
pub async fn get_person(&self, id: i32) -> Result<Option<Person>, ServiceError> { pub async fn get_person(&self, id: i32) -> Result<Option<Person>, ServiceError> {
let session = self.pool.get_session().await?; let session = self.pool.get_session().await?;