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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::DatabaseError;
use serde::Deserialize;
use serde::Serialize;
use std::fmt;

/// Enum represents a media type and can be used on a library or on a media.
/// When returned in a http response, the fields are lowercase.
#[derive(Copy, Serialize, Debug, Clone, Eq, PartialEq, Deserialize, Hash, sqlx::Type)]
#[serde(rename_all = "lowercase")]
#[sqlx(rename_all = "lowercase")]
pub enum MediaType {
    Movie,
    Tv,
    Episode,
}

impl fmt::Display for MediaType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Movie => "movie",
                Self::Tv => "tv",
                Self::Episode => "episode",
            }
        )
    }
}

impl Default for MediaType {
    fn default() -> Self {
        Self::Movie
    }
}

/// Library struct which we can use to deserialize database queries into.
#[derive(Serialize, Deserialize, Clone)]
pub struct Library {
    /// unique id provided by postgres
    pub id: i64,
    /// unique name of the library
    pub name: String,

    /// a path on the filesystem that holds media. ie /home/user/media/movies
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub locations: Vec<String>,

    /// Enum used to identify the media type that this library contains. At the
    /// moment only `movie` and `tv` are supported
    // TODO: support mixed content, music
    pub media_type: MediaType,
}

impl Library {
    /// Method returns all libraries that exist in the database in the form of a Vec.
    /// If no libraries are found the the Vec will just be empty.
    ///
    /// This method will not return the locations indexed for this library, if you need those you
    /// must query for them separately.
    pub async fn get_all(conn: &mut crate::Transaction<'_>) -> Vec<Self> {
        sqlx::query!(r#"SELECT id, name, media_type as "media_type: MediaType" FROM library WHERE NOT hidden"#)
            .fetch_all(&mut *conn)
            .await
            .unwrap_or_default()
            .into_iter()
            .map(|x| Self {
                id: x.id,
                name: x.name,
                media_type: x.media_type,
                locations: vec![],
            })
            .collect()
    }

    pub async fn get_locations(
        conn: &mut crate::Transaction<'_>,
        id: i64,
    ) -> Result<Vec<String>, DatabaseError> {
        Ok(sqlx::query_scalar!(
            "SELECT location FROM indexed_paths
            WHERE library_id = ?",
            id
        )
        .fetch_all(&mut *conn)
        .await?)
    }

    /// Method filters the database for a library with the id supplied and returns it.
    /// This method will also fetch the indexed locations for this library.
    ///
    /// # Arguments
    /// * `conn` - mutable reference to a sqlx transaction.
    /// * `lib_id` - a integer that is the id of the library we are trying to query
    pub async fn get_one(
        conn: &mut crate::Transaction<'_>,
        lib_id: i64,
    ) -> Result<Self, DatabaseError> {
        let library = sqlx::query!(
            r#"SELECT id, name, media_type as "media_type: MediaType" FROM library
            WHERE id = ?"#,
            lib_id
        )
        .fetch_one(&mut *conn)
        .await?;

        let locations = sqlx::query_scalar!(
            r#"SELECT location FROM indexed_paths
            WHERE library_id = ?"#,
            lib_id
        )
        .fetch_all(&mut *conn)
        .await?;

        Ok(Self {
            id: library.id,
            name: library.name,
            media_type: library.media_type,
            locations,
        })
    }

    /// Method filters the database for a library with the id supplied and deletes it.
    ///
    /// # Arguments
    /// * `conn` - mutable reference to a sqlx transaction.
    /// * `lib_id` - a integer that is the id of the library we are trying to query
    pub async fn delete(
        conn: &mut crate::Transaction<'_>,
        id_to_del: i64,
    ) -> Result<usize, DatabaseError> {
        Ok(sqlx::query!("DELETE FROM library WHERE id = ?", id_to_del)
            .execute(&mut *conn)
            .await?
            .rows_affected() as usize)
    }

    pub async fn mark_hidden(
        conn: &mut crate::Transaction<'_>,
        id: i64,
    ) -> Result<usize, DatabaseError> {
        Ok(
            sqlx::query!("UPDATE library SET hidden = 1 WHERE id = ?", id)
                .execute(&mut *conn)
                .await?
                .rows_affected() as usize,
        )
    }

    pub async fn get_size(
        tx: &mut crate::Transaction<'_>,
        id: i64,
    ) -> Result<usize, DatabaseError> {
        Ok(sqlx::query!(
            "SELECT COUNT(id) as size FROM _tblmedia WHERE library_id = ?",
            id
        )
        .fetch_one(&mut *tx)
        .await?
        .size as usize)
    }
}

/// InsertableLibrary struct, same as [`Library`](Library) but without the id field.
#[derive(Clone, Serialize, Deserialize)]
pub struct InsertableLibrary {
    pub name: String,
    pub locations: Vec<String>,
    pub media_type: MediaType,
}

impl InsertableLibrary {
    /// Method inserts a InsertableLibrary object into the database (makes a new library).
    ///
    /// # Arguments
    /// * `conn` - mutable reference to a sqlx transaction.
    pub async fn insert(&self, conn: &mut crate::Transaction<'_>) -> Result<i64, DatabaseError> {
        let lib_id = sqlx::query!(
            r#"INSERT INTO library (name, media_type) VALUES ($1, $2)"#,
            self.name,
            self.media_type
        )
        .execute(&mut *conn)
        .await?
        .last_insert_rowid();

        for location in &self.locations {
            sqlx::query!(
                r#"INSERT into indexed_paths(location, library_id)
                VALUES ($1, $2)"#,
                location,
                lib_id
            )
            .execute(&mut *conn)
            .await?;
        }

        Ok(lib_id)
    }
}