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
use crate::DatabaseError;
use crate::Transaction;

use std::path::Path;
use std::path::PathBuf;

/// A compact version of `MediaFile`. Useful in cases where we need to request some basic info for
/// a lot of mediafiles, and as such a `SELECT *` is not viable.
#[derive(Clone)]
pub struct CompactMediafile {
    pub id: i64,
    pub name: String,
    pub duration: Option<i64>,
    pub target_file: PathBuf,
}

/// Intermediary record which is later converted into a `CompactMediafile`. This is needed because
/// sqlx doesnt support deserializing into a `PathBuf`.
struct Record {
    id: i64,
    name: String,
    duration: Option<i64>,
    target_file: String,
}

impl From<Record> for CompactMediafile {
    fn from(
        Record {
            id,
            name,
            duration,
            target_file,
        }: Record,
    ) -> Self {
        Self {
            id,
            name,
            duration,
            target_file: Path::new(&target_file).to_path_buf(),
        }
    }
}

impl CompactMediafile {
    /// Method will return all the unmatched mediafiles for a specific library.
    pub async fn unmatched_for_library(
        tx: &mut Transaction<'_>,
        library_id: i64,
    ) -> Result<Vec<Self>, DatabaseError> {
        Ok(sqlx::query_as!(
            Record,
            r#"SELECT id, raw_name as name, duration, target_file FROM mediafile
               WHERE library_id = ? AND media_id IS NULL"#,
            library_id
        )
        .fetch_all(tx)
        .await?
        .into_iter()
        .map(Into::into)
        .collect())
    }

    /// Method will return all mediafiles for a media id.
    pub async fn all_for_media(
        tx: &mut Transaction<'_>,
        media_id: i64,
    ) -> Result<Vec<Self>, DatabaseError> {
        Ok(sqlx::query_as!(
            Record,
            r#"SELECT id, raw_name as name, duration, target_file FROM mediafile
               WHERE mediafile.media_id = ?"#,
            media_id
        )
        .fetch_all(tx)
        .await?
        .into_iter()
        .map(Into::into)
        .collect())
    }

    /// Method will return all mediafiles for a tv show.
    pub async fn all_for_tv(
        tx: &mut Transaction<'_>,
        tv_id: i64,
    ) -> Result<Vec<Self>, DatabaseError> {
        Ok(sqlx::query_as!(
            Record,
            "SELECT mediafile.id, raw_name as name, duration, target_file FROM mediafile
             INNER JOIN episode ON mediafile.media_id = episode.id
             INNER JOIN _tblseason ON episode.seasonid = _tblseason.id
             WHERE _tblseason.tvshowid = ?
             GROUP BY episode.id
             ",
            tv_id
        )
        .fetch_all(&mut *tx)
        .await?
        .into_iter()
        .map(Into::into)
        .collect())
    }
}