diff options
-rw-r--r-- | src/controllers/screening.rs | 6 | ||||
-rw-r--r-- | src/db.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/models.rs | 2 | ||||
-rw-r--r-- | src/models/screening.rs | 9 |
5 files changed, 12 insertions, 11 deletions
diff --git a/src/controllers/screening.rs b/src/controllers/screening.rs index a26720e..5052075 100644 --- a/src/controllers/screening.rs +++ b/src/controllers/screening.rs @@ -18,7 +18,7 @@ use crate::{ db, - models::{Screening}, + models::AggregatedScreening, }; use rocket::get; @@ -27,6 +27,6 @@ use rocket_contrib::{ }; #[get("/")] -pub fn get_screenings(db: db::Connection) -> Json<Vec<Screening>> { - Json(db.get_screenings().unwrap()) +pub fn get_aggregated_screenings(db: db::Connection) -> Json<Vec<AggregatedScreening>> { + Json(db.get_aggregated_screenings().unwrap()) } @@ -53,13 +53,13 @@ impl Connection { .execute(&**self) } - pub fn get_screenings(&self) -> QueryResult<Vec<models::Screening>> { + pub fn get_aggregated_screenings(&self) -> QueryResult<Vec<models::AggregatedScreening>> { use crate::schema::screenings::dsl::*; let s = screenings .left_join(films::table) .left_join(rooms::table) .get_results(&**self)?; - Ok(models::Screening::from_query(s)) + Ok(models::AggregatedScreening::from_query(s)) } pub fn get_rooms(&self) -> QueryResult<Vec<models::Room>> { @@ -45,5 +45,5 @@ pub fn build_rocket(db_url: &str) -> Result<rocket::Rocket, Box<dyn Error>> { .mount("/", rocket_contrib::serve::StaticFiles::from("./public")) .mount("/rooms", routes![room::get_rooms]) .mount("/films", routes![film::get_films]) - .mount("/screenings", routes![screening::get_screenings])) + .mount("/screenings", routes![screening::get_aggregated_screenings])) } diff --git a/src/models.rs b/src/models.rs index fc45d88..2d49ad1 100644 --- a/src/models.rs +++ b/src/models.rs @@ -22,4 +22,4 @@ pub mod screening; pub use film::{Film, NewFilm}; pub use room::Room; -pub use screening::{NewScreening, Screening}; +pub use screening::{AggregatedScreening, NewScreening}; diff --git a/src/models/screening.rs b/src/models/screening.rs index 4ae42a2..feb98cc 100644 --- a/src/models/screening.rs +++ b/src/models/screening.rs @@ -44,8 +44,9 @@ pub struct ScreeningRecord { pub end_time: chrono::NaiveTime, } +/// Aggregate screening, room and film info into one struct. #[derive(Deserialize, Serialize)] -pub struct Screening { +pub struct AggregatedScreening { pub id: i32, pub film: Film, pub room: Room, @@ -54,10 +55,10 @@ pub struct Screening { pub end_time: chrono::NaiveTime, } -impl Screening { - pub fn from_query(s: Vec<(ScreeningRecord, Option<Film>, Option<Room>)>) -> Vec<Screening> { +impl AggregatedScreening { + pub fn from_query(s: Vec<(ScreeningRecord, Option<Film>, Option<Room>)>) -> Vec<AggregatedScreening> { s.iter().map(|(s, f, r)| { - Screening { + AggregatedScreening { id: s.id, film: f.as_ref().unwrap().clone().to_owned(), room: r.as_ref().unwrap().clone().to_owned(), |