/* Social program for Ramaskrik. Copyright (C) 2019 Harald Eilertsen This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ use crate::{ models, schema::*, }; use diesel::prelude::*; use rocket_sync_db_pools::{ database, diesel, }; #[database("main")] pub struct Connection(diesel::PgConnection); impl Connection { pub async fn create_screening( &self, room_id: i32, film_id: i32, start_time: chrono::DateTime, end_time: chrono::DateTime) -> QueryResult { let s = models::NewScreening { room_id, film_id, start_time, end_time }; self.run(move |conn| { diesel::insert_into(screenings::table) .values(&s) .execute(conn) }).await } pub async fn create_screening_from_aggregate( &self, room: &models::Room, film: &models::Film, start_time: chrono::DateTime, end_time: chrono::DateTime) -> QueryResult { self.create_screening(room.id, film.id, start_time, end_time).await } pub async fn update_screening( &self, screening_id: i32, room_id: i32, film_id: i32, start_time: chrono::DateTime, end_time: chrono::DateTime) -> QueryResult { let s = models::Screening { id: screening_id, film_id, room_id, start_time, end_time }; self.run(move |conn| { diesel::update(screenings::table) .filter(screenings::id.eq(screening_id)) .set(&s) .execute(conn) }).await } pub async fn get_screenings(&self) -> QueryResult> { self.run(|conn| { use crate::schema::screenings::dsl::*; screenings.load(conn) }).await } pub async fn get_screening(&self, screening_id: i32) -> QueryResult { self.run(move |conn| { use crate::schema::screenings::dsl::*; screenings.filter(id.eq(screening_id)).get_result(conn) }).await } pub async fn get_aggregated_screenings(&self) -> QueryResult> { let s = self.run(|conn| { use crate::schema::screenings::dsl::*; screenings .left_join(films::table) .left_join(rooms::table) .get_results(conn) }).await; Ok(models::AggregatedScreening::from_query(s?)) } pub async fn delete_screening(&self, screening_id: i32) -> QueryResult { self.run(move |conn| { use crate::schema::screenings::dsl::*; diesel::delete(screenings.filter(id.eq(screening_id))) .execute(conn) }).await } pub async fn create_room(&self, room_name: String) -> QueryResult { self.run(move |conn| { use crate::schema::rooms::dsl::*; diesel::insert_into(rooms) .values(&name.eq(room_name)) .execute(conn) }).await } pub async fn get_rooms(&self) -> QueryResult> { self.run(|conn| { use crate::schema::rooms::dsl::*; rooms.load(conn) }).await } pub async fn get_room_by_name(&self, room_name: String) -> QueryResult { self.run(move |conn| { use crate::schema::rooms::dsl::*; rooms .filter(name.eq(&room_name)) .get_result(conn) }).await } pub async fn create_film(&self, film_title: String, film_url: Option) -> QueryResult { self.run(move |conn| { use crate::schema::films::dsl::*; diesel::insert_into(films) .values(&(title.eq(film_title), url.eq(film_url))) .execute(conn) }).await } pub async fn get_films(&self) -> QueryResult> { self.run(|conn| { use crate::schema::films::dsl::*; films.load(conn) }).await } pub async fn get_film_by_title(&self, film_title: String) -> QueryResult { self.run(move |conn| { use crate::schema::films::dsl::*; films .filter(title.eq(&film_title)) .get_result(conn) }).await } }