From caa7b4429e68135404ad5a01aa1d7f530abae090 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 28 Apr 2019 13:47:58 +0200 Subject: Update /screenings endpoint to return full info on screenings. This makes each screening record self-contained, and the json payload more consistent with how it semantically fit together. --- public/js/app.js | 10 +++++----- src/controllers/screening.rs | 4 ++-- src/db.rs | 7 ++++--- src/models/film.rs | 2 +- src/models/room.rs | 2 +- src/models/screening.rs | 37 ++++++++++++++++++++++++++++++++----- 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, Option)>> { +pub fn get_screenings(db: db::Connection) -> Json> { Json(db.get_screenings().unwrap()) } diff --git a/src/db.rs b/src/db.rs index d7a12b0..d24384f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -53,12 +53,13 @@ impl Connection { .execute(&**self) } - pub fn get_screenings(&self) -> QueryResult, Option)>> { + pub fn get_screenings(&self) -> QueryResult> { 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> { 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 . */ +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, Option)>) -> Vec { + 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 = 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"); }); } -- cgit v1.2.3