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;
#[derive(Clone)]
pub struct CompactMediafile {
pub id: i64,
pub name: String,
pub duration: Option<i64>,
pub target_file: 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 {
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())
}
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())
}
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())
}
}