aboutsummaryrefslogtreecommitdiffstats
path: root/src/db.rs
blob: 6bce5e820a75b3d870b67c6e1c397a6e7b452ce2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
    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)
    }
}