From dcfe66e4d409a2b89b93be2ead8a7a84ab863dc0 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 10 Aug 2019 17:37:08 +0200 Subject: Rename Screening model to AggregateScreening --- src/controllers/screening.rs | 6 +++--- src/db.rs | 4 ++-- src/lib.rs | 2 +- src/models.rs | 2 +- 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> { - Json(db.get_screenings().unwrap()) +pub fn get_aggregated_screenings(db: db::Connection) -> Json> { + Json(db.get_aggregated_screenings().unwrap()) } diff --git a/src/db.rs b/src/db.rs index d24384f..59c4df3 100644 --- a/src/db.rs +++ b/src/db.rs @@ -53,13 +53,13 @@ impl Connection { .execute(&**self) } - pub fn get_screenings(&self) -> QueryResult> { + pub fn get_aggregated_screenings(&self) -> QueryResult> { 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> { diff --git a/src/lib.rs b/src/lib.rs index 9ae797d..d861aaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,5 +45,5 @@ pub fn build_rocket(db_url: &str) -> Result> { .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, Option)>) -> Vec { +impl AggregatedScreening { + pub fn from_query(s: Vec<(ScreeningRecord, Option, Option)>) -> Vec { 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(), -- cgit v1.2.3