diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2019-04-28 13:47:58 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2019-04-28 13:47:58 +0200 |
commit | caa7b4429e68135404ad5a01aa1d7f530abae090 (patch) | |
tree | 2f2b2331de7e126bc2552c4e285cd75f69256cf4 /src | |
parent | 30f65b62e84fcd9975643716d61fa21bc8495291 (diff) | |
download | ramaskrik-social-caa7b4429e68135404ad5a01aa1d7f530abae090.tar.gz ramaskrik-social-caa7b4429e68135404ad5a01aa1d7f530abae090.tar.bz2 ramaskrik-social-caa7b4429e68135404ad5a01aa1d7f530abae090.zip |
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.
Diffstat (limited to 'src')
-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 |
5 files changed, 40 insertions, 12 deletions
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() + } +} |