Can get single accommodation reservation information
This commit is contained in:
parent
1332b001c8
commit
3efae7bfff
@ -1,5 +1,6 @@
|
||||
use crate::controllers::HttpResult;
|
||||
use crate::extractors::accommodation_extractor::FamilyAndAccommodationInPath;
|
||||
use crate::extractors::accommodation_reservation_extractor::FamilyAndAccommodationReservationInPath;
|
||||
use crate::extractors::family_extractor::FamilyInPath;
|
||||
use crate::services::accommodations_reservations_service;
|
||||
use actix_web::HttpResponse;
|
||||
@ -15,3 +16,8 @@ pub async fn full_list(m: FamilyInPath) -> HttpResult {
|
||||
Ok(HttpResponse::Ok()
|
||||
.json(accommodations_reservations_service::get_all_of_family(m.family_id()).await?))
|
||||
}
|
||||
|
||||
/// Get a single accommodation reservation
|
||||
pub async fn get_single(m: FamilyAndAccommodationReservationInPath) -> HttpResult {
|
||||
Ok(HttpResponse::Ok().json(m.to_reservation()))
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ impl FromRequest for FamilyAndAccommodationInPath {
|
||||
.await?
|
||||
.accommodation_id;
|
||||
|
||||
FamilyAndAccommodationInPath::load_accommodation_from_path(family, accommodation_id)
|
||||
Self::load_accommodation_from_path(family, accommodation_id)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to extract accommodation ID from URL! {}", e);
|
||||
|
@ -0,0 +1,99 @@
|
||||
use crate::extractors::family_extractor::FamilyInPath;
|
||||
use crate::models::{
|
||||
Accommodation, AccommodationReservation, AccommodationReservationID, FamilyID, Membership,
|
||||
};
|
||||
use crate::services::{accommodations_list_service, accommodations_reservations_service};
|
||||
use actix_web::dev::Payload;
|
||||
use actix_web::{FromRequest, HttpRequest};
|
||||
use serde::Deserialize;
|
||||
use std::fmt::Debug;
|
||||
use std::ops::Deref;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
enum AccommodationReservationExtractorErr {
|
||||
#[error("Accommodation reservation {0:?} does not belong to family {1:?}!")]
|
||||
AccommodationNotInFamily(AccommodationReservationID, FamilyID),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FamilyAndAccommodationReservationInPath(
|
||||
Membership,
|
||||
Accommodation,
|
||||
AccommodationReservation,
|
||||
);
|
||||
|
||||
impl FamilyAndAccommodationReservationInPath {
|
||||
async fn load_accommodation_reservation_from_path(
|
||||
family: FamilyInPath,
|
||||
reservation_id: AccommodationReservationID,
|
||||
) -> anyhow::Result<Self> {
|
||||
let reservation = accommodations_reservations_service::get_by_id(reservation_id).await?;
|
||||
let accommodation =
|
||||
accommodations_list_service::get_by_id(reservation.accommodation_id()).await?;
|
||||
|
||||
if accommodation.family_id() != family.family_id()
|
||||
|| reservation.family_id() != family.family_id()
|
||||
{
|
||||
return Err(
|
||||
AccommodationReservationExtractorErr::AccommodationNotInFamily(
|
||||
reservation.id(),
|
||||
family.family_id(),
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
|
||||
Ok(Self(family.into(), accommodation, reservation))
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for FamilyAndAccommodationReservationInPath {
|
||||
type Target = AccommodationReservation;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.2
|
||||
}
|
||||
}
|
||||
|
||||
impl FamilyAndAccommodationReservationInPath {
|
||||
pub fn membership(&self) -> &Membership {
|
||||
&self.0
|
||||
}
|
||||
|
||||
pub fn to_accommodation(self) -> Accommodation {
|
||||
self.1
|
||||
}
|
||||
|
||||
pub fn to_reservation(self) -> AccommodationReservation {
|
||||
self.2
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ReservationIDInPath {
|
||||
reservation_id: AccommodationReservationID,
|
||||
}
|
||||
|
||||
impl FromRequest for FamilyAndAccommodationReservationInPath {
|
||||
type Error = actix_web::Error;
|
||||
type Future = futures_util::future::LocalBoxFuture<'static, Result<Self, Self::Error>>;
|
||||
|
||||
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
|
||||
let req = req.clone();
|
||||
Box::pin(async move {
|
||||
let family = FamilyInPath::extract(&req).await?;
|
||||
|
||||
let reservation_id =
|
||||
actix_web::web::Path::<ReservationIDInPath>::from_request(&req, &mut Payload::None)
|
||||
.await?
|
||||
.reservation_id;
|
||||
|
||||
Self::load_accommodation_reservation_from_path(family, reservation_id)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to extract accommodation ID from URL! {}", e);
|
||||
actix_web::error::ErrorNotFound("Could not fetch accommodation information!")
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
pub mod accommodation_extractor;
|
||||
pub mod accommodation_reservation_extractor;
|
||||
pub mod couple_extractor;
|
||||
pub mod family_extractor;
|
||||
pub mod member_extractor;
|
||||
|
@ -227,10 +227,6 @@ async fn main() -> std::io::Result<()> {
|
||||
web::delete().to(accommodations_list_controller::delete),
|
||||
)
|
||||
// [ACCOMODATIONS] Reservations controller
|
||||
// TODO : create
|
||||
// TODO : get single
|
||||
// TODO : update
|
||||
// TODO : delete
|
||||
.route(
|
||||
"/family/{id}/accommodations/reservations/accommodation/{accommodation_id}",
|
||||
web::get()
|
||||
@ -240,6 +236,13 @@ async fn main() -> std::io::Result<()> {
|
||||
"/family/{id}/accommodations/reservations/full_list",
|
||||
web::get().to(accommodations_reservations_controller::full_list),
|
||||
)
|
||||
// TODO : create
|
||||
.route(
|
||||
"/family/{id}/accommodations/reservation/{reservation_id}",
|
||||
web::get().to(accommodations_reservations_controller::get_single),
|
||||
)
|
||||
// TODO : update
|
||||
// TODO : delete
|
||||
// TODO : validate or reject
|
||||
// Photos controller
|
||||
.route(
|
||||
|
@ -498,6 +498,20 @@ pub struct AccommodationReservation {
|
||||
pub validated: Option<bool>,
|
||||
}
|
||||
|
||||
impl AccommodationReservation {
|
||||
pub fn id(&self) -> AccommodationReservationID {
|
||||
AccommodationReservationID(self.id)
|
||||
}
|
||||
|
||||
pub fn accommodation_id(&self) -> AccommodationID {
|
||||
AccommodationID(self.accommodation_id)
|
||||
}
|
||||
|
||||
pub fn family_id(&self) -> FamilyID {
|
||||
FamilyID(self.family_id)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = accommodations_reservations)]
|
||||
pub struct NewAccommodationReservation {
|
||||
|
@ -1,5 +1,7 @@
|
||||
use crate::connections::db_connection;
|
||||
use crate::models::{AccommodationID, AccommodationReservation, FamilyID};
|
||||
use crate::models::{
|
||||
AccommodationID, AccommodationReservation, AccommodationReservationID, FamilyID,
|
||||
};
|
||||
use crate::schema::accommodations_reservations;
|
||||
use diesel::prelude::*;
|
||||
|
||||
@ -22,3 +24,12 @@ pub async fn get_all_of_family(id: FamilyID) -> anyhow::Result<Vec<Accommodation
|
||||
.get_results(conn)
|
||||
})
|
||||
}
|
||||
|
||||
/// Get a single accommodation reservation by its id
|
||||
pub async fn get_by_id(id: AccommodationReservationID) -> anyhow::Result<AccommodationReservation> {
|
||||
db_connection::execute(|conn| {
|
||||
accommodations_reservations::table
|
||||
.filter(accommodations_reservations::dsl::id.eq(id.0))
|
||||
.get_result(conn)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user