use std::sync::Arc; use axum::body::Body; use axum::extract::Path; use axum::routing::{delete, get, post}; use axum::{extract::State, response::Response}; use axum::{Extension, Json, Router}; use rest_types::BookingTO; use uuid::Uuid; use crate::{error_handler, Context, RestStateDef}; use service::booking::{Booking, BookingService}; pub fn generate_route() -> Router { Router::new() .route("/", get(get_all_bookings::)) .route("/:id", get(get_booking::)) .route("/", post(create_booking::)) .route("/:id", delete(delete_booking::)) } pub async fn get_all_bookings( rest_state: State, Extension(context): Extension, ) -> Response { error_handler( (async { let bookings: Arc<[BookingTO]> = rest_state .booking_service() .get_all(context.into()) .await? .iter() .map(BookingTO::from) .collect(); Ok(Response::builder() .status(200) .body(Body::new(serde_json::to_string(&bookings).unwrap())) .unwrap()) }) .await, ) } pub async fn get_booking( rest_state: State, Extension(context): Extension, Path(booking_id): Path, ) -> Response { error_handler( (async { let booking = rest_state .booking_service() .get(booking_id, context.into()) .await?; Ok(Response::builder() .status(200) .body(Body::new( serde_json::to_string(&BookingTO::from(&booking)).unwrap(), )) .unwrap()) }) .await, ) } pub async fn create_booking( rest_state: State, Extension(context): Extension, Json(booking): Json, ) -> Response { error_handler( (async { let booking = rest_state .booking_service() .create(&Booking::from(&booking), context.into()) .await?; Ok(Response::builder() .status(200) .body(Body::new( serde_json::to_string(&BookingTO::from(&booking)).unwrap(), )) .unwrap()) }) .await, ) } pub async fn delete_booking( rest_state: State, Extension(context): Extension, Path(booking_id): Path, ) -> Response { error_handler( (async { rest_state .booking_service() .delete(booking_id, context.into()) .await?; Ok(Response::builder().status(200).body(Body::empty()).unwrap()) }) .await, ) }