aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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
7 files changed, 69 insertions, 73 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)
-}