diff --git a/geneit_backend/src/controllers/accommodations_reservations_controller.rs b/geneit_backend/src/controllers/accommodations_reservations_controller.rs new file mode 100644 index 0000000..b3ec476 --- /dev/null +++ b/geneit_backend/src/controllers/accommodations_reservations_controller.rs @@ -0,0 +1,10 @@ +use crate::controllers::HttpResult; +use crate::extractors::family_extractor::FamilyInPath; +use crate::services::accommodations_reservations_service; +use actix_web::HttpResponse; + +/// Get the full list of accommodations reservations for a family +pub async fn full_list(m: FamilyInPath) -> HttpResult { + Ok(HttpResponse::Ok() + .json(accommodations_reservations_service::get_all_of_family(m.family_id()).await?)) +} diff --git a/geneit_backend/src/controllers/mod.rs b/geneit_backend/src/controllers/mod.rs index 4cd4c7d..ccfb812 100644 --- a/geneit_backend/src/controllers/mod.rs +++ b/geneit_backend/src/controllers/mod.rs @@ -6,6 +6,7 @@ use std::fmt::{Debug, Display, Formatter}; use zip::result::ZipError; pub mod accommodations_list_controller; +pub mod accommodations_reservations_controller; pub mod auth_controller; pub mod couples_controller; pub mod data_controller; diff --git a/geneit_backend/src/main.rs b/geneit_backend/src/main.rs index 4dbaa40..86d5600 100644 --- a/geneit_backend/src/main.rs +++ b/geneit_backend/src/main.rs @@ -6,9 +6,9 @@ use actix_web::{web, App, HttpServer}; use geneit_backend::app_config::AppConfig; use geneit_backend::connections::{db_connection, s3_connection}; use geneit_backend::controllers::{ - accommodations_list_controller, auth_controller, couples_controller, data_controller, - families_controller, members_controller, photos_controller, server_controller, - users_controller, + accommodations_list_controller, accommodations_reservations_controller, auth_controller, + couples_controller, data_controller, families_controller, members_controller, + photos_controller, server_controller, users_controller, }; #[actix_web::main] @@ -228,9 +228,14 @@ async fn main() -> std::io::Result<()> { ) // [ACCOMODATIONS] Reservations controller // TODO : create + // TODO : get single // TODO : update // TODO : delete - // TODO : list + // TODO : list for an accommodation + .route( + "/family/{id}/accommodations/reservations/full_list", + web::get().to(accommodations_reservations_controller::full_list), + ) // TODO : validate or reject // Photos controller .route( diff --git a/geneit_backend/src/models.rs b/geneit_backend/src/models.rs index 498a10d..f30bd1b 100644 --- a/geneit_backend/src/models.rs +++ b/geneit_backend/src/models.rs @@ -1,5 +1,8 @@ use crate::app_config::AppConfig; -use crate::schema::{accommodations_list, couples, families, members, memberships, photos, users}; +use crate::schema::{ + accommodations_list, accommodations_reservations, couples, families, members, memberships, + photos, users, +}; use crate::utils::crypt_utils::sha256; use diesel::prelude::*; @@ -477,3 +480,32 @@ pub struct NewAccommodation { pub time_create: i64, pub time_update: i64, } + +/// Accommodation reservation ID holder +#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash)] +pub struct AccommodationReservationID(pub i32); + +#[derive(Queryable, Debug, serde::Serialize)] +pub struct AccommodationReservation { + id: i32, + family_id: i32, + accommodation_id: i32, + user_id: i32, + time_create: i64, + pub time_update: i64, + pub reservation_start: i64, + pub reservation_end: i64, + pub validated: Option, +} + +#[derive(Insertable)] +#[diesel(table_name = accommodations_reservations)] +pub struct NewAccommodationReservation { + pub family_id: i32, + pub accommodation_id: i32, + pub user_id: i32, + pub time_create: i64, + pub time_update: i64, + pub reservation_start: i64, + pub reservation_end: i64, +} diff --git a/geneit_backend/src/services/accommodations_list_service.rs b/geneit_backend/src/services/accommodations_list_service.rs index 3ebbebc..6d44bc0 100644 --- a/geneit_backend/src/services/accommodations_list_service.rs +++ b/geneit_backend/src/services/accommodations_list_service.rs @@ -29,7 +29,7 @@ pub async fn get_by_id(id: AccommodationID) -> anyhow::Result { }) } -/// Get all the couples of an accommodation +/// Get all the accommodations of a family pub async fn get_all_of_family(id: FamilyID) -> anyhow::Result> { db_connection::execute(|conn| { accommodations_list::table @@ -57,7 +57,7 @@ pub async fn exists( }) } -/// Update the information of a couple +/// Update the information of an accommodation pub async fn update(accommodation: &mut Accommodation) -> anyhow::Result<()> { accommodation.time_update = time() as i64; diff --git a/geneit_backend/src/services/accommodations_reservations_service.rs b/geneit_backend/src/services/accommodations_reservations_service.rs new file mode 100644 index 0000000..ef3c257 --- /dev/null +++ b/geneit_backend/src/services/accommodations_reservations_service.rs @@ -0,0 +1,13 @@ +use crate::connections::db_connection; +use crate::models::{AccommodationReservation, FamilyID}; +use crate::schema::accommodations_reservations; +use diesel::prelude::*; + +/// Get all the accommodations reservations of a family +pub async fn get_all_of_family(id: FamilyID) -> anyhow::Result> { + db_connection::execute(|conn| { + accommodations_reservations::table + .filter(accommodations_reservations::dsl::family_id.eq(id.0)) + .get_results(conn) + }) +} diff --git a/geneit_backend/src/services/mod.rs b/geneit_backend/src/services/mod.rs index b5db75a..2205bef 100644 --- a/geneit_backend/src/services/mod.rs +++ b/geneit_backend/src/services/mod.rs @@ -1,6 +1,7 @@ //! # Backend services pub mod accommodations_list_service; +pub mod accommodations_reservations_service; pub mod couples_service; pub mod families_service; pub mod login_token_service;