aboutsummaryrefslogblamecommitdiffstats
path: root/src/db.rs
blob: a5341f1fee3b4332a0cd76c2e70e19ef9e9587fe (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

















                                                                               






                       
                           
             
           



                                            

                 
                                  
              

                     

                                                                      
     
                                                                                




                                                  

     
                                                 


                            

                                                                      
     
                                                                           

     
                                  







                                                                                               





                                                        

     




                                                                               

     




                                                                                            

     









                                                                                                    

     





                                                                                   

     






                                                                              

     




                                                                     

     






                                                                                          

     






                                                                                                         

     




                                                                     

     






                                                                                            

     
/*
    Social program for Ramaskrik.
    Copyright (C) 2019  Harald Eilertsen <haraldei@anduin.net>

    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 <https://www.gnu.org/licenses/>.
*/

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<chrono::Utc>,
        end_time: chrono::DateTime<chrono::Utc>) -> QueryResult<usize>
    {
        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<chrono::Utc>,
        end_time: chrono::DateTime<chrono::Utc>) -> QueryResult<usize>
    {
        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<chrono::Utc>,
        end_time: chrono::DateTime<chrono::Utc>) -> QueryResult<usize>
    {
        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<Vec<models::Screening>> {
        self.run(|conn| {
            use crate::schema::screenings::dsl::*;
            screenings.load(conn)
        }).await
    }

    pub async fn get_screening(&self, screening_id: i32) -> QueryResult<models::Screening> {
        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<Vec<models::AggregatedScreening>> {
        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<usize> {
        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<usize> {
        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<Vec<models::Room>> {
        self.run(|conn| {
            use crate::schema::rooms::dsl::*;
            rooms.load(conn)
        }).await
    }

    pub async fn get_room_by_name(&self, room_name: String) -> QueryResult<models::Room> {
        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<String>) -> QueryResult<usize> {
        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<Vec<models::Film>> {
        self.run(|conn| {
            use crate::schema::films::dsl::*;
            films.load(conn)
        }).await
    }

    pub async fn get_film_by_title(&self, film_title: String) -> QueryResult<models::Film> {
        self.run(move |conn| {
            use crate::schema::films::dsl::*;
            films
                .filter(title.eq(&film_title))
                .get_result(conn)
        }).await
    }
}