From ad0e585abed7a189b6117e9187c9afff809d1414 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 8 Apr 2019 15:55:14 +0200 Subject: Move functions to query/modify db to connection object. Makes tha API a bit nicer by asking the database rather than passing the database on to each model. Reserve models for method/functions that work on the model structs themselves. --- src/controllers/film.rs | 2 +- src/controllers/room.rs | 2 +- src/controllers/screening.rs | 4 +-- src/db.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++ src/models/film.rs | 18 +------------ src/models/room.rs | 18 +------------ src/models/screening.rs | 36 +------------------------ tests/apitests.rs | 19 +++++++------- 8 files changed, 78 insertions(+), 83 deletions(-) diff --git a/src/controllers/film.rs b/src/controllers/film.rs index f4bcd03..e61a543 100644 --- a/src/controllers/film.rs +++ b/src/controllers/film.rs @@ -28,5 +28,5 @@ use rocket_contrib::{ #[get("/")] pub fn get_films(db: db::Connection) -> Json> { - Json(film::get_all(&db).unwrap()) + Json(db.get_films().unwrap()) } diff --git a/src/controllers/room.rs b/src/controllers/room.rs index df77414..a203454 100644 --- a/src/controllers/room.rs +++ b/src/controllers/room.rs @@ -28,5 +28,5 @@ use rocket_contrib::{ #[get("/")] pub fn get_rooms(db: db::Connection) -> Json> { - Json(room::get_all(&db).unwrap()) + Json(db.get_rooms().unwrap()) } diff --git a/src/controllers/screening.rs b/src/controllers/screening.rs index d060eb7..622e9cb 100644 --- a/src/controllers/screening.rs +++ b/src/controllers/screening.rs @@ -18,7 +18,7 @@ use crate::{ db, - models::{screening::{self, Screening}, film::Film, room::Room}, + models::{screening::Screening, film::Film, room::Room}, }; use rocket::get; @@ -28,5 +28,5 @@ use rocket_contrib::{ #[get("/")] pub fn get_screenings(db: db::Connection) -> Json, Option)>> { - Json(screening::get_all(&db).unwrap()) + Json(db.get_screenings().unwrap()) } diff --git a/src/db.rs b/src/db.rs index b299911..53c4ea4 100644 --- a/src/db.rs +++ b/src/db.rs @@ -16,6 +16,13 @@ along with this program. If not, see . */ +use crate::{ + models, + schema::*, +}; + +use diesel::prelude::*; + use rocket_contrib::{ database, databases::diesel, @@ -23,3 +30,58 @@ use rocket_contrib::{ #[database("main")] pub struct Connection(diesel::PgConnection); + +impl Connection { + pub fn create_screening( + &self, + room: &models::room::Room, + film: &models::film::Film, + date: chrono::NaiveDate, + start_time: chrono::NaiveTime, + end_time: chrono::NaiveTime) -> QueryResult + { + let s = models::screening::NewScreening { + room_id: room.id, + film_id: film.id, + date: date, + start_time: start_time, + end_time: end_time, + }; + + diesel::insert_into(screenings::table) + .values(&s) + .execute(&**self) + } + + pub fn get_screenings(&self) -> QueryResult, Option)>> { + use crate::schema::screenings::dsl::*; + screenings + .left_join(films::table) + .left_join(rooms::table) + .get_results(&**self) + } + + pub fn get_rooms(&self) -> QueryResult> { + use crate::schema::rooms::dsl::*; + rooms.load(&**self) + } + + pub fn get_room_by_name(&self, room_name: &str) -> QueryResult { + use crate::schema::rooms::dsl::*; + rooms + .filter(name.eq(&room_name)) + .get_result(&**self) + } + + pub fn get_films(&self) -> QueryResult> { + use crate::schema::films::dsl::*; + films.load(&**self) + } + + pub fn get_film_by_title(&self, film_title: &str) -> QueryResult { + use crate::schema::films::dsl::*; + films + .filter(title.eq(&film_title)) + .get_result(&**self) + } +} diff --git a/src/models/film.rs b/src/models/film.rs index 8207e5d..8f17d42 100644 --- a/src/models/film.rs +++ b/src/models/film.rs @@ -16,11 +16,7 @@ along with this program. If not, see . */ -use crate::{ - db::Connection, - schema::films, -}; -use diesel::prelude::*; +use crate::schema::films; use serde_derive::{Deserialize, Serialize}; #[derive(Deserialize, Identifiable, PartialEq, Serialize, Queryable)] @@ -36,15 +32,3 @@ pub struct NewFilm<'a> { pub title: &'a str, pub url: Option<&'a str>, } - -pub fn get_all(db: &Connection) -> QueryResult> { - use crate::schema::films::dsl::*; - films.load::(&**db) -} - -pub fn by_title(ftitle: &str, db: &Connection) -> QueryResult { - use crate::schema::films::dsl::*; - films - .filter(title.eq(&ftitle)) - .get_result::(&**db) -} diff --git a/src/models/room.rs b/src/models/room.rs index 6360c88..d475901 100644 --- a/src/models/room.rs +++ b/src/models/room.rs @@ -16,11 +16,7 @@ along with this program. If not, see . */ -use crate::{ - db::Connection, - schema::rooms, -}; -use diesel::prelude::*; +use crate::schema::rooms; use serde_derive::{Deserialize, Serialize}; #[derive(Deserialize, Identifiable, PartialEq, Serialize, Queryable)] @@ -28,15 +24,3 @@ pub struct Room { pub id: i32, pub name: String, } - -pub fn get_all(db: &Connection) -> QueryResult> { - use crate::schema::rooms::dsl::*; - rooms.load::(&**db) -} - -pub fn by_name(rname: &str, db: &Connection) -> QueryResult { - use crate::schema::rooms::dsl::*; - rooms - .filter(name.eq(&rname)) - .get_result::(&**db) -} diff --git a/src/models/screening.rs b/src/models/screening.rs index cd08e88..5058d65 100644 --- a/src/models/screening.rs +++ b/src/models/screening.rs @@ -16,12 +16,7 @@ along with this program. If not, see . */ -use crate::{ - db::Connection, - models::{film::Film, room::Room}, - schema::*, -}; -use diesel::prelude::*; +use crate::schema::*; use serde_derive::{Deserialize, Serialize}; joinable!(screenings -> rooms (room_id)); @@ -46,32 +41,3 @@ pub struct NewScreening { pub start_time: chrono::NaiveTime, pub end_time: chrono::NaiveTime, } - -pub fn get_all(db: &Connection) -> QueryResult, Option)>> { - use crate::schema::screenings::dsl::*; - screenings - .left_join(films::table) - .left_join(rooms::table) - .get_results::<(Screening, Option, Option)>(&**db) -} - -pub fn create( - room: &Room, - film: &Film, - date: chrono::NaiveDate, - start_time: chrono::NaiveTime, - end_time: chrono::NaiveTime, - db: &Connection) -> QueryResult -{ - let s = NewScreening { - room_id: room.id, - film_id: film.id, - date: date, - start_time: start_time, - end_time: end_time, - }; - - diesel::insert_into(screenings::table) - .values(&s) - .execute(&**db) -} diff --git a/tests/apitests.rs b/tests/apitests.rs index 58be302..54b7961 100644 --- a/tests/apitests.rs +++ b/tests/apitests.rs @@ -18,7 +18,6 @@ use ramaskrik; use ramaskrik::models::{ - self, film::{ Film, NewFilm }, room::Room, screening, @@ -125,25 +124,25 @@ fn getting_films_from_api() { #[test] fn getting_screenings_from_api() { server_with_db(|server, db| { - let r = models::room::by_name("Main room", &db).unwrap(); - let f = models::film::by_title("Hellraiser", &db).unwrap(); - screening::create(&r, &f, + let r = db.get_room_by_name("Main room").unwrap(); + let f = db.get_film_by_title("Hellraiser").unwrap(); + + db.create_screening(&r, &f, chrono::NaiveDate::from_ymd(2019, 10, 21), chrono::NaiveTime::from_hms(18, 00, 00), - chrono::NaiveTime::from_hms(19, 34, 00), - &db).unwrap(); + chrono::NaiveTime::from_hms(19, 34, 00)).unwrap(); let client = rocket::local::Client::new(server).unwrap(); let mut response = client.get("/screenings").dispatch(); assert_eq!(response.content_type(), Some(ContentType::JSON)); - let fetched_screenings: Vec<(screening::Screening, Option, Option)> = + let fetched_screenings: Vec<(screening::Screening, Film, Room)> = 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.as_ref().unwrap().title, "Hellraiser"); - assert_eq!(rr.as_ref().unwrap().name, "Main room"); + let (_scr, ff, rr) = &fetched_screenings[0]; + assert_eq!(ff.title, "Hellraiser"); + assert_eq!(rr.name, "Main room"); }); } -- cgit v1.2.3