hammond_data: Make index a trait that requires Insert + Update.
This commit is contained in:
parent
1c96288178
commit
6abf2535b0
@ -8,7 +8,7 @@ use rss;
|
||||
|
||||
use dbqueries;
|
||||
use errors::*;
|
||||
use models::{IndexState, Update};
|
||||
use models::{Index, IndexState, Update};
|
||||
use models::{NewEpisode, NewPodcast, Podcast};
|
||||
use pipeline::*;
|
||||
|
||||
|
||||
@ -83,7 +83,7 @@ impl Episode {
|
||||
self.description = value.map(|x| x.to_string());
|
||||
}
|
||||
|
||||
/// Get the value of the `description`.
|
||||
/// Get the Episode's `guid`.
|
||||
pub fn guid(&self) -> Option<&str> {
|
||||
self.guid.as_ref().map(|s| s.as_str())
|
||||
}
|
||||
@ -475,6 +475,11 @@ impl EpisodeMinimal {
|
||||
self.uri.as_ref().map(|s| s.as_str())
|
||||
}
|
||||
|
||||
/// Get the Episode's `guid`.
|
||||
pub fn guid(&self) -> Option<&str> {
|
||||
self.guid.as_ref().map(|s| s.as_str())
|
||||
}
|
||||
|
||||
/// Get the `epoch` value.
|
||||
///
|
||||
/// Retrieved from the rss Item publish date.
|
||||
|
||||
@ -35,3 +35,7 @@ pub trait Insert {
|
||||
pub trait Update {
|
||||
fn update(&self, i32) -> Result<()>;
|
||||
}
|
||||
|
||||
pub trait Index: Insert + Update {
|
||||
fn index(&self) -> Result<()>;
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ use rss;
|
||||
use database::connection;
|
||||
use dbqueries;
|
||||
use errors::*;
|
||||
use models::{Episode, Insert, Update};
|
||||
use models::{Episode, Index, Insert, Update};
|
||||
use parser;
|
||||
use utils::{replace_extra_spaces, url_cleaner};
|
||||
|
||||
@ -75,6 +75,30 @@ impl Update for NewEpisode {
|
||||
}
|
||||
}
|
||||
|
||||
impl Index for NewEpisode {
|
||||
fn index(&self) -> Result<()> {
|
||||
let exists = dbqueries::episode_exists(self.title(), self.podcast_id())?;
|
||||
|
||||
match exists {
|
||||
false => self.insert(),
|
||||
true => {
|
||||
let old = 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())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NewEpisode {
|
||||
/// Parses an `rss::Item` into a `NewEpisode` Struct.
|
||||
pub(crate) fn new(item: &rss::Item, podcast_id: i32) -> Result<Self> {
|
||||
@ -86,29 +110,6 @@ impl NewEpisode {
|
||||
self.index()?;
|
||||
dbqueries::get_episode_from_pk(&self.title, self.podcast_id)
|
||||
}
|
||||
|
||||
pub(crate) fn index(&self) -> Result<()> {
|
||||
let ep = dbqueries::get_episode_from_pk(&self.title, self.podcast_id);
|
||||
|
||||
match ep {
|
||||
Ok(foo) => {
|
||||
if foo.podcast_id() != self.podcast_id {
|
||||
error!("NEP pid: {}\nEP pid: {}", self.podcast_id, foo.podcast_id());
|
||||
};
|
||||
|
||||
if foo.title() != self.title.as_str() || foo.epoch() != self.epoch
|
||||
|| foo.uri() != self.uri.as_ref().map(|s| s.as_str())
|
||||
|| foo.duration() != self.duration
|
||||
{
|
||||
self.update(foo.rowid())?;
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
self.insert()?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
@ -134,6 +135,10 @@ impl NewEpisode {
|
||||
self.epoch
|
||||
}
|
||||
|
||||
pub(crate) fn duration(&self) -> Option<i32> {
|
||||
self.duration
|
||||
}
|
||||
|
||||
pub(crate) fn length(&self) -> Option<i32> {
|
||||
self.length
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@ use diesel::prelude::*;
|
||||
use ammonia;
|
||||
use rss;
|
||||
|
||||
use models::{Insert, Update};
|
||||
use models::{Index, Insert, Update};
|
||||
use models::Podcast;
|
||||
use schema::podcast;
|
||||
|
||||
@ -57,6 +57,28 @@ impl Update for NewPodcast {
|
||||
}
|
||||
}
|
||||
|
||||
impl Index for NewPodcast {
|
||||
fn index(&self) -> Result<()> {
|
||||
let exists = dbqueries::podcast_exists(self.source_id)?;
|
||||
|
||||
match exists {
|
||||
false => self.insert(),
|
||||
true => {
|
||||
let old = 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.update(old.id())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NewPodcast {
|
||||
/// Parses a `rss::Channel` into a `NewPodcast` Struct.
|
||||
pub(crate) fn new(chan: &rss::Channel, source_id: i32) -> NewPodcast {
|
||||
@ -92,26 +114,7 @@ impl NewPodcast {
|
||||
// Look out for when tryinto lands into stable.
|
||||
pub(crate) fn into_podcast(self) -> Result<Podcast> {
|
||||
self.index()?;
|
||||
Ok(dbqueries::get_podcast_from_source_id(self.source_id)?)
|
||||
}
|
||||
|
||||
pub(crate) fn index(&self) -> Result<()> {
|
||||
let pd = dbqueries::get_podcast_from_source_id(self.source_id);
|
||||
|
||||
match pd {
|
||||
Ok(foo) => {
|
||||
if (foo.link() != self.link) || (foo.title() != self.title)
|
||||
|| (foo.image_uri() != self.image_uri.as_ref().map(|x| x.as_str()))
|
||||
{
|
||||
info!("NewEpisode: {:?}\n OldEpisode: {:?}", self, foo);
|
||||
self.update(foo.id())?;
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
self.insert()?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
dbqueries::get_podcast_from_source_id(self.source_id).map_err(From::from)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user