diff options
-rw-r--r-- | public/js/app.js | 10 | ||||
-rw-r--r-- | src/controllers/screening.rs | 4 | ||||
-rw-r--r-- | src/db.rs | 7 | ||||
-rw-r--r-- | src/models/film.rs | 2 | ||||
-rw-r--r-- | src/models/room.rs | 2 | ||||
-rw-r--r-- | src/models/screening.rs | 37 | ||||
-rw-r--r-- | tests/apitests.rs | 8 |
7 files changed, 49 insertions, 21 deletions
diff --git a/public/js/app.js b/public/js/app.js index 6148be4..c6899a7 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -27,7 +27,7 @@ function fetch_rooms(data_done) { function load_screenings(data_done) { let req = new XMLHttpRequest() req.addEventListener("load", data_done) - req.open("GET", "http://localhost:8000/screenings.json") + req.open("GET", "http://localhost:8000/screenings") req.responseType = "json" req.send() } @@ -50,13 +50,13 @@ function log_film(film) { } window.addEventListener("load", function() { - /* load_screenings(function() { - this.response.forEach(function(film) { - log_film(film) + console.log(this.response) + this.response.forEach(function(screening) { + console.log(screening.film.title + "@" + screening.room.name + ": " + screening.date) }) }) - */ + fetch_rooms(function() { let table = document.getElementById("screening-table") let row = document.createElement("tr") diff --git a/src/controllers/screening.rs b/src/controllers/screening.rs index 29b6afa..a26720e 100644 --- a/src/controllers/screening.rs +++ b/src/controllers/screening.rs @@ -18,7 +18,7 @@ use crate::{ db, - models::{Film, Room, Screening}, + models::{Screening}, }; use rocket::get; @@ -27,6 +27,6 @@ use rocket_contrib::{ }; #[get("/")] -pub fn get_screenings(db: db::Connection) -> Json<Vec<(Screening, Option<Film>, Option<Room>)>> { +pub fn get_screenings(db: db::Connection) -> Json<Vec<Screening>> { Json(db.get_screenings().unwrap()) } @@ -53,12 +53,13 @@ impl Connection { .execute(&**self) } - pub fn get_screenings(&self) -> QueryResult<Vec<(models::Screening, Option<models::Film>, Option<models::Room>)>> { + pub fn get_screenings(&self) -> QueryResult<Vec<models::Screening>> { use crate::schema::screenings::dsl::*; - screenings + let s = screenings .left_join(films::table) .left_join(rooms::table) - .get_results(&**self) + .get_results(&**self)?; + Ok(models::Screening::from_query(s)) } pub fn get_rooms(&self) -> QueryResult<Vec<models::Room>> { diff --git a/src/models/film.rs b/src/models/film.rs index 8f17d42..f280cf6 100644 --- a/src/models/film.rs +++ b/src/models/film.rs @@ -19,7 +19,7 @@ use crate::schema::films; use serde_derive::{Deserialize, Serialize}; -#[derive(Deserialize, Identifiable, PartialEq, Serialize, Queryable)] +#[derive(Clone, Default, Deserialize, Identifiable, PartialEq, Serialize, Queryable)] pub struct Film { pub id: i32, pub title: String, diff --git a/src/models/room.rs b/src/models/room.rs index d475901..40c5d19 100644 --- a/src/models/room.rs +++ b/src/models/room.rs @@ -19,7 +19,7 @@ use crate::schema::rooms; use serde_derive::{Deserialize, Serialize}; -#[derive(Deserialize, Identifiable, PartialEq, Serialize, Queryable)] +#[derive(Clone, Default, Deserialize, Identifiable, PartialEq, Serialize, Queryable)] pub struct Room { pub id: i32, pub name: String, diff --git a/src/models/screening.rs b/src/models/screening.rs index 5058d65..4ae42a2 100644 --- a/src/models/screening.rs +++ b/src/models/screening.rs @@ -16,15 +16,16 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ +use crate::models::{Film, Room}; use crate::schema::*; use serde_derive::{Deserialize, Serialize}; joinable!(screenings -> rooms (room_id)); joinable!(screenings -> films (film_id)); -#[derive(Deserialize, Identifiable, PartialEq, Serialize, Queryable)] -pub struct Screening { - pub id: i32, +#[derive(Deserialize, Insertable)] +#[table_name = "screenings"] +pub struct NewScreening { pub film_id: i32, pub room_id: i32, pub date: chrono::NaiveDate, @@ -32,12 +33,38 @@ pub struct Screening { pub end_time: chrono::NaiveTime, } -#[derive(Deserialize, Insertable)] +#[derive(Deserialize, Identifiable, PartialEq, Serialize, Queryable)] #[table_name = "screenings"] -pub struct NewScreening { +pub struct ScreeningRecord { + pub id: i32, pub film_id: i32, pub room_id: i32, pub date: chrono::NaiveDate, pub start_time: chrono::NaiveTime, pub end_time: chrono::NaiveTime, } + +#[derive(Deserialize, Serialize)] +pub struct Screening { + pub id: i32, + pub film: Film, + pub room: Room, + pub date: chrono::NaiveDate, + pub start_time: chrono::NaiveTime, + pub end_time: chrono::NaiveTime, +} + +impl Screening { + pub fn from_query(s: Vec<(ScreeningRecord, Option<Film>, Option<Room>)>) -> Vec<Screening> { + s.iter().map(|(s, f, r)| { + Screening { + id: s.id, + film: f.as_ref().unwrap().clone().to_owned(), + room: r.as_ref().unwrap().clone().to_owned(), + date: s.date, + start_time: s.start_time, + end_time: s.end_time, + } + }).collect() + } +} diff --git a/tests/apitests.rs b/tests/apitests.rs index a488c33..e0803a8 100644 --- a/tests/apitests.rs +++ b/tests/apitests.rs @@ -132,13 +132,13 @@ fn getting_screenings_from_api() { let mut response = client.get("/screenings").dispatch(); assert_eq!(response.content_type(), Some(ContentType::JSON)); - let fetched_screenings: Vec<(Screening, Film, Room)> = + let fetched_screenings: Vec<Screening> = serde_json::from_str(&response.body_string().unwrap()).unwrap(); assert_eq!(fetched_screenings.len(), 1); - let (_scr, ff, rr) = &fetched_screenings[0]; - assert_eq!(ff.title, "Hellraiser"); - assert_eq!(rr.name, "Main room"); + let scr = &fetched_screenings[0]; + assert_eq!(scr.film.title, "Hellraiser"); + assert_eq!(scr.room.name, "Main room"); }); } |