From 0a2b71082b0fa84948fa639b6586f4c994abf56c Mon Sep 17 00:00:00 2001 From: Timo Ley Date: Fri, 23 Jun 2023 10:46:15 +0200 Subject: [PATCH] feat: endpoints for rooms --- src/model.rs | 3 ++- src/routes.rs | 19 ++++++++++++++++ src/sql.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/src/model.rs b/src/model.rs index 5addfcc..3a1481d 100644 --- a/src/model.rs +++ b/src/model.rs @@ -59,11 +59,12 @@ pub struct Booking { } #[derive(Serialize)] +#[serde(rename_all = "camelCase")] pub struct CleaningPlan { pub id: i32, pub date: String, pub duration: i32, - pub room: Room, + pub room_id: i32, } #[derive(Deserialize)] diff --git a/src/routes.rs b/src/routes.rs index 84107ba..6e28386 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -10,6 +10,23 @@ async fn rooms( Ok(Json(rooms)) } +async fn room( + Path(id): Path, + Extension(service): Extension, +) -> Result { + let room = service.get_room(id).await? + .ok_or_else(|| ServiceError::ErrorResponse(StatusCode::NOT_FOUND, Some("room not found".to_string())))?; + Ok(Json(room)) +} + +async fn cleaning_plans( + Path(id): Path, + Extension(service): Extension, +) -> Result { + let cleaning_plans = service.get_room_cleaning_plans(id).await?; + Ok(Json(cleaning_plans)) +} + async fn bookings( Query(query): Query, Extension(service): Extension, @@ -114,6 +131,8 @@ async fn update_booking( pub fn routes() -> Router { Router::new() .route("/rooms", get(rooms)) + .route("/rooms/:id", get(room)) + .route("/rooms/:id/cleaningPlans", get(cleaning_plans)) .route("/bookings", get(bookings).post(add_booking)) .route("/bookings/:id", get(booking).delete(delete_booking).put(update_booking)) .route("/bookings/:id/rooms", get(booking_rooms)) diff --git a/src/sql.rs b/src/sql.rs index 595112c..7d799b3 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -1,6 +1,6 @@ use hyper::StatusCode; -use crate::{ServiceInner, model::{Room, Booking, Person, Address, BookingBody}, error::ServiceError}; +use crate::{ServiceInner, model::{Room, Booking, Person, Address, BookingBody, CleaningPlan}, error::ServiceError}; impl ServiceInner { @@ -37,6 +37,65 @@ impl ServiceInner { Ok(rooms) } + + pub async fn get_room(&self, id: i32) -> Result, ServiceError> { + let session = self.pool.get_session().await?; + let stmt = session.prepare(" + SELECT + roomNumber, + floor, + roomTyp, + \"size\", + accessibility, + beds + FROM room WHERE roomNumber = :ID + ").await?; + + let row = stmt.query_single(id).await?; + + Ok(match row { + Some(row) => { + let acc: i32 = row.get(5)?; + Some(Room { + room_number: row.get(0)?, + floor: row.get(1)?, + size: row.get(3)?, + room_type: row.get(2)?, + beds: row.get(4)?, + accessibility: acc != 0, + }) + }, + None => None, + }) + } + + pub async fn get_room_cleaning_plans(&self, roomid: i32) -> Result, ServiceError> { + let session = self.pool.get_session().await?; + let stmt = session.prepare(" + SELECT + cleaningPlanId, + TO_CHAR(\"date\", 'YYYY-MM-DD'), + duration + FROM CleaningPlan + WHERE roomNumber = :ROOMID + ").await?; + + let rows = stmt.query(roomid).await?; + + let mut cleaning_plans: Vec = vec![]; + + while let Some(row) = rows.next().await? { + let cleaning_plan = CleaningPlan { + id: row.get(0)?, + date: row.get(1)?, + duration: row.get(2)?, + room_id: roomid, + }; + cleaning_plans.push(cleaning_plan); + } + + Ok(cleaning_plans) + } pub async fn get_bookings(&self) -> Result, ServiceError> { let session = self.pool.get_session().await?;