Add an accommodations reservations module #188

Merged
pierre merged 81 commits from accomodation_module into master 2024-06-22 21:30:26 +00:00
4 changed files with 35 additions and 1 deletions
Showing only changes of commit 33b03a4d74 - Show all commits

View File

@ -86,3 +86,16 @@ pub async fn full_list(m: FamilyInPath) -> HttpResult {
pub async fn get_single(m: FamilyAndAccommodationReservationInPath) -> HttpResult { pub async fn get_single(m: FamilyAndAccommodationReservationInPath) -> HttpResult {
Ok(HttpResponse::Ok().json(m.to_reservation())) Ok(HttpResponse::Ok().json(m.to_reservation()))
} }
/// Delete a reservation
pub async fn delete(m: FamilyAndAccommodationReservationInPath) -> HttpResult {
if m.membership().user_id() != m.user_id() {
return Ok(
HttpResponse::BadRequest().json("Only the owner of a reservation can delete it!")
);
}
accommodations_reservations_service::delete(m.to_reservation()).await?;
Ok(HttpResponse::Accepted().finish())
}

View File

@ -245,7 +245,10 @@ async fn main() -> std::io::Result<()> {
web::get().to(accommodations_reservations_controller::get_single), web::get().to(accommodations_reservations_controller::get_single),
) )
// TODO : update // TODO : update
// TODO : delete .route(
"/family/{id}/accommodations/reservation/{reservation_id}",
web::delete().to(accommodations_reservations_controller::delete),
)
// TODO : validate or reject // TODO : validate or reject
// [ACCOMMODATIONS] Calendars controller // [ACCOMMODATIONS] Calendars controller
// TODO : create // TODO : create

View File

@ -510,6 +510,10 @@ impl AccommodationReservation {
pub fn family_id(&self) -> FamilyID { pub fn family_id(&self) -> FamilyID {
FamilyID(self.family_id) FamilyID(self.family_id)
} }
pub fn user_id(&self) -> UserID {
UserID(self.user_id)
}
} }
#[derive(Insertable)] #[derive(Insertable)]

View File

@ -37,6 +37,20 @@ pub async fn update(r: &mut AccommodationReservation) -> anyhow::Result<()> {
Ok(()) Ok(())
} }
/// Delete a reservation
pub async fn delete(r: AccommodationReservation) -> anyhow::Result<()> {
// Remove the reservation
db_connection::execute(|conn| {
diesel::delete(
accommodations_reservations::dsl::accommodations_reservations
.filter(accommodations_reservations::dsl::id.eq(r.id().0)),
)
.execute(conn)
})?;
Ok(())
}
/// Get all the reservations of an accommodation /// Get all the reservations of an accommodation
pub async fn get_all_of_accommodation( pub async fn get_all_of_accommodation(
id: AccommodationID, id: AccommodationID,