Can create reservation requests
This commit is contained in:
@@ -2,8 +2,69 @@ 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::models::NewAccommodationReservation;
|
||||
use crate::services::accommodations_reservations_service;
|
||||
use actix_web::HttpResponse;
|
||||
use crate::utils::time_utils::time;
|
||||
use actix_web::{web, HttpResponse};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct CreateReservationQuery {
|
||||
start: usize,
|
||||
end: usize,
|
||||
}
|
||||
|
||||
/// Create a reservation
|
||||
pub async fn create_reservation(
|
||||
a: FamilyAndAccommodationInPath,
|
||||
req: web::Json<CreateReservationQuery>,
|
||||
) -> HttpResult {
|
||||
if !a.open_to_reservations {
|
||||
return Ok(HttpResponse::ExpectationFailed()
|
||||
.json("The accommodation is not open to reservations!"));
|
||||
}
|
||||
|
||||
if (req.start as i64) < (time() as i64 - 3600 * 24 * 30) {
|
||||
return Ok(HttpResponse::BadRequest().json("Start time is too far in the past!"));
|
||||
}
|
||||
|
||||
if req.start > req.end {
|
||||
return Ok(HttpResponse::BadRequest().json("End time happens before start time!"));
|
||||
}
|
||||
|
||||
let existing = accommodations_reservations_service::get_reservations_for_time_interval(
|
||||
a.id(),
|
||||
req.start,
|
||||
req.end,
|
||||
)
|
||||
.await?;
|
||||
|
||||
if existing.iter().any(|r| r.validated != Some(false)) {
|
||||
return Ok(
|
||||
HttpResponse::Conflict().json("This reservation is in conflict with another one!")
|
||||
);
|
||||
}
|
||||
|
||||
let mut reservation =
|
||||
accommodations_reservations_service::create(&NewAccommodationReservation {
|
||||
family_id: a.family_id().0,
|
||||
accommodation_id: a.id().0,
|
||||
user_id: a.membership().user_id().0,
|
||||
time_create: time() as i64,
|
||||
time_update: time() as i64,
|
||||
reservation_start: req.start as i64,
|
||||
reservation_end: req.end as i64,
|
||||
})
|
||||
.await?;
|
||||
|
||||
// Auto validate reservation if requested
|
||||
if !a.need_validation {
|
||||
reservation.validated = Some(true);
|
||||
|
||||
accommodations_reservations_service::update(&mut reservation).await?;
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(reservation))
|
||||
}
|
||||
|
||||
/// Get the reservations for a given accommodation
|
||||
pub async fn get_accommodation_reservations(a: FamilyAndAccommodationInPath) -> HttpResult {
|
||||
|
||||
Reference in New Issue
Block a user