From 3c24b9f9d9ceca665d503bb75c06bce90d26c746 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Wed, 20 Dec 2017 19:00:14 +0200 Subject: [PATCH] hammond-data::utils: Added new Diesel model for the download checker. --- hammond-data/src/dbqueries.rs | 8 +++-- hammond-data/src/models/queryables.rs | 52 +++++++++++++++++++++++++++ hammond-data/src/utils.rs | 16 ++++++--- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/hammond-data/src/dbqueries.rs b/hammond-data/src/dbqueries.rs index 261bd8b..2ce9061 100644 --- a/hammond-data/src/dbqueries.rs +++ b/hammond-data/src/dbqueries.rs @@ -2,7 +2,8 @@ use diesel::prelude::*; use diesel; -use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, PodcastCoverQuery, Source}; +use models::queryables::{Episode, EpisodeDownloadCleanerQuery, EpisodeWidgetQuery, Podcast, + PodcastCoverQuery, Source}; use chrono::prelude::*; use errors::*; @@ -32,14 +33,15 @@ pub fn get_episodes() -> Result> { Ok(episode.order(epoch.desc()).load::(&*con)?) } -pub fn get_downloaded_episodes() -> Result> { +pub(crate) fn get_downloaded_episodes() -> Result> { use schema::episode::dsl::*; let db = connection(); let con = db.get()?; Ok(episode + .select((rowid, local_uri)) .filter(local_uri.is_not_null()) - .load::(&*con)?) + .load::(&*con)?) } pub fn get_played_episodes() -> Result> { diff --git a/hammond-data/src/models/queryables.rs b/hammond-data/src/models/queryables.rs index bd15320..cf6dded 100644 --- a/hammond-data/src/models/queryables.rs +++ b/hammond-data/src/models/queryables.rs @@ -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, +} + +impl From 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 { + 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)] #[belongs_to(Source, foreign_key = "source_id")] #[changeset_options(treat_none_as_null = "true")] diff --git a/hammond-data/src/utils.rs b/hammond-data/src/utils.rs index f15c757..39ea52b 100644 --- a/hammond-data/src/utils.rs +++ b/hammond-data/src/utils.rs @@ -8,7 +8,7 @@ use itertools::Itertools; use errors::*; use dbqueries; -use models::queryables::Episode; +use models::queryables::{Episode, EpisodeDownloadCleanerQuery}; use std::path::Path; use std::fs; @@ -25,7 +25,7 @@ fn download_checker() -> Result<()> { Ok(()) } -fn checker_helper(ep: &mut Episode) { +fn checker_helper(ep: &mut EpisodeDownloadCleanerQuery) { if !Path::new(ep.local_uri().unwrap()).exists() { ep.set_local_uri(None); let res = ep.save(); @@ -180,12 +180,16 @@ mod tests { #[test] fn test_download_checker() { - let _tmp_dir = helper_db(); + let tmp_dir = helper_db(); download_checker().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!("foo_bar", episodes.first().unwrap().title()); + assert_eq!( + Some(valid_path.to_str().unwrap()), + episodes.first().unwrap().local_uri() + ); } #[test] @@ -194,7 +198,9 @@ mod tests { let mut episode = { let db = connection(); 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);