aboutsummaryrefslogblamecommitdiffstats
path: root/src/db.rs
blob: 6bce5e820a75b3d870b67c6e1c397a6e7b452ce2 (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_contrib::{
    database,
    databases::diesel,
};

#[database("main")]
pub struct Connection(diesel::PgConnection);

impl Connection {
    pub 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 };
        diesel::insert_into(screenings::table)
            .values(&s)
            .execute(&**self)
    }

    pub 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)
    }

    pub 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 };
        diesel::update(screenings::table)
            .filter(screenings::id.eq(screening_id))
            .set(&s)
            .execute(&**self)
    }

    pub fn get_screenings(&self) -> QueryResult<Vec<models::Screening>> {
        use crate::schema::screenings::dsl::*;
        screenings.load(&**self)
    }

    pub fn get_screening(&self, screening_id: i32) -> QueryResult<models::Screening> {
        use crate::schema::screenings::dsl::*;
        screenings.filter(id.eq(screening_id)).get_result(&**self)
    }

    pub fn get_aggregated_screenings(&self) -> QueryResult<Vec<models::AggregatedScreening>> {
        use crate::schema::screenings::dsl::*;
        let s = screenings
            .left_join(films::table)
            .left_join(rooms::table)
            .get_results(&**self)?;
        Ok(models::AggregatedScreening::from_query(s))
    }

    pub fn delete_screening(&self, screening_id: i32) -> QueryResult<usize> {
        use crate::schema::screenings::dsl::*;
        diesel::delete(screenings.filter(id.eq(screening_id)))
            .execute(&**self)
    }

    pub fn create_room(&self, room_name: &String) -> QueryResult<usize> {
        use crate::schema::rooms::dsl::*;
        diesel::insert_into(rooms)
            .values(&name.eq(room_name))
            .execute(&**self)
    }

    pub fn get_rooms(&self) -> QueryResult<Vec<models::Room>> {
        use crate::schema::rooms::dsl::*;
        rooms.load(&**self)
    }

    pub fn get_room_by_name(&self, room_name: &str) -> QueryResult<models::Room> {
        use crate::schema::rooms::dsl::*;
        rooms
            .filter(name.eq(&room_name))
            .get_result(&**self)
    }

    pub fn create_film(&self, film_title: &str, film_url: &Option<String>) -> QueryResult<usize> {
        use crate::schema::films::dsl::*;
        diesel::insert_into(films)
            .values(&(title.eq(film_title), url.eq(film_url)))
            .execute(&**self)
    }

    pub fn get_films(&self) -> QueryResult<Vec<models::Film>> {
        use crate::schema::films::dsl::*;
        films.load(&**self)
    }

    pub fn get_film_by_title(&self, film_title: &str) -> QueryResult<models::Film> {
        use crate::schema::films::dsl::*;
        films
            .filter(title.eq(&film_title))
            .get_result(&**self)
    }
}