feat: endpoints for rooms

This commit is contained in:
Timo Ley 2023-06-23 10:46:15 +02:00
parent c6aebd8dae
commit 0a2b71082b
3 changed files with 81 additions and 2 deletions

View file

@ -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)]

View file

@ -10,6 +10,23 @@ async fn rooms(
Ok(Json(rooms))
}
async fn room(
Path(id): Path<i32>,
Extension(service): Extension<Service>,
) -> Result<impl IntoResponse, ServiceError> {
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<i32>,
Extension(service): Extension<Service>,
) -> Result<impl IntoResponse, ServiceError> {
let cleaning_plans = service.get_room_cleaning_plans(id).await?;
Ok(Json(cleaning_plans))
}
async fn bookings(
Query(query): Query<BookingQuery>,
Extension(service): Extension<Service>,
@ -114,6 +131,8 @@ async fn update_booking(
pub fn routes() -> Router<BoxRoute> {
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))

View file

@ -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 {
@ -38,6 +38,65 @@ impl ServiceInner {
Ok(rooms)
}
pub async fn get_room(&self, id: i32) -> Result<Option<Room>, 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<Vec<CleaningPlan>, 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<CleaningPlan> = 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<Vec<Booking>, ServiceError> {
let session = self.pool.get_session().await?;
let stmt = session.prepare("