diff --git a/hammond-data/src/models/episode.rs b/hammond-data/src/models/episode.rs index 5bd7bb4..4ce2260 100644 --- a/hammond-data/src/models/episode.rs +++ b/hammond-data/src/models/episode.rs @@ -6,7 +6,6 @@ use diesel::prelude::*; use database::connection; use errors::*; use models::Podcast; -use models::new_episode::{NewEpisodeMinimal, NewEpisodeMinimalBuilder}; use schema::episode; #[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)] @@ -443,20 +442,6 @@ impl From for EpisodeMinimal { } } -impl Into for EpisodeMinimal { - fn into(self) -> NewEpisodeMinimal { - NewEpisodeMinimalBuilder::default() - .title(self.title) - .uri(self.uri) - .guid(self.guid) - .epoch(self.epoch) - .duration(self.duration) - .podcast_id(self.podcast_id) - .build() - .unwrap() - } -} - impl EpisodeMinimal { /// Get the value of the sqlite's `ROW_ID` pub fn rowid(&self) -> i32 { diff --git a/hammond-data/src/models/new_episode.rs b/hammond-data/src/models/new_episode.rs index 35b57eb..3caa8e4 100644 --- a/hammond-data/src/models/new_episode.rs +++ b/hammond-data/src/models/new_episode.rs @@ -10,7 +10,7 @@ use rss; use database::connection; use dbqueries; use errors::*; -use models::{Episode, Index, Insert, Update}; +use models::{Episode, EpisodeMinimal, Index, Insert, Update}; use parser; use utils::{replace_extra_spaces, url_cleaner}; @@ -80,14 +80,10 @@ impl Index for NewEpisode { let exists = dbqueries::episode_exists(self.title(), self.podcast_id())?; if exists { - let old = dbqueries::get_episode_minimal_from_pk(self.title(), self.podcast_id())?; + let other = dbqueries::get_episode_minimal_from_pk(self.title(), self.podcast_id())?; - // This is messy - if (self.title() != old.title()) || (self.uri() != old.uri()) - || (self.duration() != old.duration()) - || (self.epoch() != old.epoch()) || (self.guid() != old.guid()) - { - self.update(old.rowid()) + if self != &other { + self.update(other.rowid()) } else { Ok(()) } @@ -97,6 +93,14 @@ impl Index for NewEpisode { } } +impl PartialEq for NewEpisode { + fn eq(&self, other: &EpisodeMinimal) -> bool { + (self.title() != other.title()) || (self.uri() != other.uri()) + || (self.duration() != other.duration()) || (self.epoch() != other.epoch()) + || (self.guid() != other.guid()) + } +} + impl NewEpisode { /// Parses an `rss::Item` into a `NewEpisode` Struct. #[allow(dead_code)] @@ -161,6 +165,14 @@ pub(crate) struct NewEpisodeMinimal { podcast_id: i32, } +impl PartialEq for NewEpisodeMinimal { + fn eq(&self, other: &EpisodeMinimal) -> bool { + (self.title() != other.title()) || (self.uri() != other.uri()) + || (self.duration() != other.duration()) || (self.epoch() != other.epoch()) + || (self.guid() != other.guid()) + } +} + impl NewEpisodeMinimal { pub(crate) fn new(item: &rss::Item, parent_id: i32) -> Result { if item.title().is_none() { @@ -224,7 +236,6 @@ impl NewEpisodeMinimal { } } -#[allow(dead_code)] // Ignore the following getters. They are used in unit tests mainly. impl NewEpisodeMinimal { pub(crate) fn title(&self) -> &str { @@ -239,6 +250,10 @@ impl NewEpisodeMinimal { self.guid.as_ref().map(|s| s.as_str()) } + pub(crate) fn duration(&self) -> Option { + self.duration + } + pub(crate) fn epoch(&self) -> i32 { self.epoch } diff --git a/hammond-data/src/models/new_podcast.rs b/hammond-data/src/models/new_podcast.rs index 97609cc..41196a7 100644 --- a/hammond-data/src/models/new_podcast.rs +++ b/hammond-data/src/models/new_podcast.rs @@ -62,14 +62,10 @@ impl Index for NewPodcast { let exists = dbqueries::podcast_exists(self.source_id)?; if exists { - let old = dbqueries::get_podcast_from_source_id(self.source_id)?; + let other = dbqueries::get_podcast_from_source_id(self.source_id)?; - // This is messy - if (self.link() != old.link()) || (self.title() != old.title()) - || (self.image_uri() != old.image_uri()) - || (self.description() != old.description()) - { - self.update(old.id()) + if self != &other { + self.update(other.id()) } else { Ok(()) } @@ -79,6 +75,14 @@ impl Index for NewPodcast { } } +impl PartialEq for NewPodcast { + fn eq(&self, other: &Podcast) -> bool { + (self.link() != other.link()) || (self.title() != other.title()) + || (self.image_uri() != other.image_uri()) + || (self.description() != other.description()) + } +} + impl NewPodcast { /// Parses a `rss::Channel` into a `NewPodcast` Struct. pub(crate) fn new(chan: &rss::Channel, source_id: i32) -> NewPodcast { diff --git a/hammond-data/src/pipeline.rs b/hammond-data/src/pipeline.rs index 2d71246..3f8a1cb 100644 --- a/hammond-data/src/pipeline.rs +++ b/hammond-data/src/pipeline.rs @@ -56,7 +56,7 @@ fn determine_ep_state(ep: NewEpisodeMinimal, item: &rss::Item) -> Result