aboutsummaryrefslogblamecommitdiffstats
path: root/src/db.rs
blob: 60bbcde0c2a38b9c5aea0e720e9c1ff54651f8b3 (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: &models::Room,
        film: &models::Film,
        date: chrono::NaiveDate,
        start_time: chrono::NaiveTime,
        end_time: chrono::NaiveTime) -> QueryResult<usize>
    {
        let s = models::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>> {
        use crate::schema::screenings::dsl::*;
        screenings.load(&**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 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 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)
    }
}