diff --git a/geneit_backend/src/controllers/accommodations_reservations_controller.rs b/geneit_backend/src/controllers/accommodations_reservations_controller.rs index eac0f7b..3159d3a 100644 --- a/geneit_backend/src/controllers/accommodations_reservations_controller.rs +++ b/geneit_backend/src/controllers/accommodations_reservations_controller.rs @@ -86,3 +86,16 @@ pub async fn full_list(m: FamilyInPath) -> HttpResult { pub async fn get_single(m: FamilyAndAccommodationReservationInPath) -> HttpResult { 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()) +} diff --git a/geneit_backend/src/main.rs b/geneit_backend/src/main.rs index 3e898d3..f9c0250 100644 --- a/geneit_backend/src/main.rs +++ b/geneit_backend/src/main.rs @@ -245,7 +245,10 @@ async fn main() -> std::io::Result<()> { web::get().to(accommodations_reservations_controller::get_single), ) // TODO : update - // TODO : delete + .route( + "/family/{id}/accommodations/reservation/{reservation_id}", + web::delete().to(accommodations_reservations_controller::delete), + ) // TODO : validate or reject // [ACCOMMODATIONS] Calendars controller // TODO : create diff --git a/geneit_backend/src/models.rs b/geneit_backend/src/models.rs index 2acf822..5bbf860 100644 --- a/geneit_backend/src/models.rs +++ b/geneit_backend/src/models.rs @@ -510,6 +510,10 @@ impl AccommodationReservation { pub fn family_id(&self) -> FamilyID { FamilyID(self.family_id) } + + pub fn user_id(&self) -> UserID { + UserID(self.user_id) + } } #[derive(Insertable)] diff --git a/geneit_backend/src/services/accommodations_reservations_service.rs b/geneit_backend/src/services/accommodations_reservations_service.rs index fa00c4a..f3425cc 100644 --- a/geneit_backend/src/services/accommodations_reservations_service.rs +++ b/geneit_backend/src/services/accommodations_reservations_service.rs @@ -37,6 +37,20 @@ pub async fn update(r: &mut AccommodationReservation) -> anyhow::Result<()> { 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 pub async fn get_all_of_accommodation( id: AccommodationID,