aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/controllers/film.rs2
-rw-r--r--src/controllers/room.rs2
-rw-r--r--src/controllers/screening.rs4
-rw-r--r--src/db.rs62
-rw-r--r--src/models/film.rs18
-rw-r--r--src/models/room.rs18
-rw-r--r--src/models/screening.rs36
-rw-r--r--tests/apitests.rs19
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<Vec<film::Film>> {
- 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<Vec<room::Room>> {
- 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<Vec<(Screening, Option<Film>, Option<Room>)>> {
- 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 <https://www.gnu.org/licenses/>.
*/
+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<usize>
+ {
+ 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<Vec<(models::screening::Screening, Option<models::film::Film>, Option<models::room::Room>)>> {
+ use crate::schema::screenings::dsl::*;
+ screenings
+ .left_join(films::table)
+ .left_join(rooms::table)
+ .get_results(&**self)
+ }
+
+ pub fn get_rooms(&self) -> QueryResult<Vec<models::room::Room>> {
+ use crate::schema::rooms::dsl::*;
+ rooms.load(&**self)
+ }
+
+ pub fn get_room_by_name(&self, room_name: &str) -> QueryResult<models::room::Room> {
+ use crate::schema::rooms::dsl::*;
+ rooms
+ .filter(name.eq(&room_name))
+ .get_result(&**self)
+ }
+
+ pub fn get_films(&self) -> QueryResult<Vec<models::film::Film>> {
+ use crate::schema::films::dsl::*;
+ films.load(&**self)
+ }
+
+ pub fn get_film_by_title(&self, film_title: &str) -> QueryResult<models::film::Film> {
+ 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 <https://www.gnu.org/licenses/>.
*/
-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<Vec<Film>> {
- use crate::schema::films::dsl::*;
- films.load::<Film>(&**db)
-}
-
-pub fn by_title(ftitle: &str, db: &Connection) -> QueryResult<Film> {
- use crate::schema::films::dsl::*;
- films
- .filter(title.eq(&ftitle))
- .get_result::<Film>(&**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 <https://www.gnu.org/licenses/>.
*/
-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<Vec<Room>> {
- use crate::schema::rooms::dsl::*;
- rooms.load::<Room>(&**db)
-}
-
-pub fn by_name(rname: &str, db: &Connection) -> QueryResult<Room> {
- use crate::schema::rooms::dsl::*;
- rooms
- .filter(name.eq(&rname))
- .get_result::<Room>(&**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 <https://www.gnu.org/licenses/>.
*/
-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<Vec<(Screening, Option<Film>, Option<Room>)>> {
- use crate::schema::screenings::dsl::*;
- screenings
- .left_join(films::table)
- .left_join(rooms::table)
- .get_results::<(Screening, Option<Film>, Option<Room>)>(&**db)
-}
-
-pub fn create(
- room: &Room,
- film: &Film,
- date: chrono::NaiveDate,
- start_time: chrono::NaiveTime,
- end_time: chrono::NaiveTime,
- db: &Connection) -> QueryResult<usize>
-{
- 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<Film>, Option<Room>)> =
+ 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");
});
}