hammond-data::utils: Added new Diesel model for the download checker.
This commit is contained in:
parent
2e06205eda
commit
3c24b9f9d9
@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel;
|
use diesel;
|
||||||
use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, PodcastCoverQuery, Source};
|
use models::queryables::{Episode, EpisodeDownloadCleanerQuery, EpisodeWidgetQuery, Podcast,
|
||||||
|
PodcastCoverQuery, Source};
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
@ -32,14 +33,15 @@ pub fn get_episodes() -> Result<Vec<Episode>> {
|
|||||||
Ok(episode.order(epoch.desc()).load::<Episode>(&*con)?)
|
Ok(episode.order(epoch.desc()).load::<Episode>(&*con)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_downloaded_episodes() -> Result<Vec<Episode>> {
|
pub(crate) fn get_downloaded_episodes() -> Result<Vec<EpisodeDownloadCleanerQuery>> {
|
||||||
use schema::episode::dsl::*;
|
use schema::episode::dsl::*;
|
||||||
|
|
||||||
let db = connection();
|
let db = connection();
|
||||||
let con = db.get()?;
|
let con = db.get()?;
|
||||||
Ok(episode
|
Ok(episode
|
||||||
|
.select((rowid, local_uri))
|
||||||
.filter(local_uri.is_not_null())
|
.filter(local_uri.is_not_null())
|
||||||
.load::<Episode>(&*con)?)
|
.load::<EpisodeDownloadCleanerQuery>(&*con)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_played_episodes() -> Result<Vec<Episode>> {
|
pub fn get_played_episodes() -> Result<Vec<Episode>> {
|
||||||
|
|||||||
@ -320,6 +320,58 @@ impl EpisodeWidgetQuery {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, AsChangeset, PartialEq)]
|
||||||
|
#[table_name = "episode"]
|
||||||
|
#[changeset_options(treat_none_as_null = "true")]
|
||||||
|
#[primary_key(title, podcast_id)]
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
/// Diesel Model to be used for constructing `EpisodeWidgets`.
|
||||||
|
pub(crate) struct EpisodeDownloadCleanerQuery {
|
||||||
|
rowid: i32,
|
||||||
|
local_uri: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Episode> for EpisodeDownloadCleanerQuery {
|
||||||
|
fn from(e: Episode) -> EpisodeDownloadCleanerQuery {
|
||||||
|
EpisodeDownloadCleanerQuery {
|
||||||
|
rowid: e.rowid(),
|
||||||
|
local_uri: e.local_uri,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EpisodeDownloadCleanerQuery {
|
||||||
|
/// Get the value of the sqlite's `ROW_ID`
|
||||||
|
pub(crate) fn rowid(&self) -> i32 {
|
||||||
|
self.rowid
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the value of the `local_uri`.
|
||||||
|
///
|
||||||
|
/// Represents the local uri,usually filesystem path,
|
||||||
|
/// that the media file will be located at.
|
||||||
|
pub(crate) fn local_uri(&self) -> Option<&str> {
|
||||||
|
self.local_uri.as_ref().map(|s| s.as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the `local_uri`.
|
||||||
|
pub(crate) fn set_local_uri(&mut self, value: Option<&str>) {
|
||||||
|
self.local_uri = value.map(|x| x.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper method to easily save/"sync" current state of self to the Database.
|
||||||
|
pub(crate) fn save(&self) -> Result<usize> {
|
||||||
|
use schema::episode::dsl::*;
|
||||||
|
|
||||||
|
let db = connection();
|
||||||
|
let tempdb = db.get()?;
|
||||||
|
|
||||||
|
Ok(diesel::update(episode.filter(rowid.eq(self.rowid())))
|
||||||
|
.set(self)
|
||||||
|
.execute(&*tempdb)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
|
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
|
||||||
#[belongs_to(Source, foreign_key = "source_id")]
|
#[belongs_to(Source, foreign_key = "source_id")]
|
||||||
#[changeset_options(treat_none_as_null = "true")]
|
#[changeset_options(treat_none_as_null = "true")]
|
||||||
|
|||||||
@ -8,7 +8,7 @@ use itertools::Itertools;
|
|||||||
|
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use dbqueries;
|
use dbqueries;
|
||||||
use models::queryables::Episode;
|
use models::queryables::{Episode, EpisodeDownloadCleanerQuery};
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@ -25,7 +25,7 @@ fn download_checker() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn checker_helper(ep: &mut Episode) {
|
fn checker_helper(ep: &mut EpisodeDownloadCleanerQuery) {
|
||||||
if !Path::new(ep.local_uri().unwrap()).exists() {
|
if !Path::new(ep.local_uri().unwrap()).exists() {
|
||||||
ep.set_local_uri(None);
|
ep.set_local_uri(None);
|
||||||
let res = ep.save();
|
let res = ep.save();
|
||||||
@ -180,12 +180,16 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_download_checker() {
|
fn test_download_checker() {
|
||||||
let _tmp_dir = helper_db();
|
let tmp_dir = helper_db();
|
||||||
download_checker().unwrap();
|
download_checker().unwrap();
|
||||||
let episodes = dbqueries::get_downloaded_episodes().unwrap();
|
let episodes = dbqueries::get_downloaded_episodes().unwrap();
|
||||||
|
let valid_path = tmp_dir.path().join("virtual_dl.mp3");
|
||||||
|
|
||||||
assert_eq!(episodes.len(), 1);
|
assert_eq!(episodes.len(), 1);
|
||||||
assert_eq!("foo_bar", episodes.first().unwrap().title());
|
assert_eq!(
|
||||||
|
Some(valid_path.to_str().unwrap()),
|
||||||
|
episodes.first().unwrap().local_uri()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -194,7 +198,9 @@ mod tests {
|
|||||||
let mut episode = {
|
let mut episode = {
|
||||||
let db = connection();
|
let db = connection();
|
||||||
let con = db.get().unwrap();
|
let con = db.get().unwrap();
|
||||||
dbqueries::get_episode_from_pk(&con, "bar_baz", 1).unwrap()
|
dbqueries::get_episode_from_pk(&con, "bar_baz", 1)
|
||||||
|
.unwrap()
|
||||||
|
.into()
|
||||||
};
|
};
|
||||||
|
|
||||||
checker_helper(&mut episode);
|
checker_helper(&mut episode);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user