From 282a29e7dd32115f632aec1095c15fe631227c13 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 17 Nov 2017 18:58:44 +0200 Subject: [PATCH] Move some indexing functions into methods of insertable models. --- hammond-data/src/index_feed.rs | 28 +++--------------- hammond-data/src/models/insertables.rs | 40 +++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/hammond-data/src/index_feed.rs b/hammond-data/src/index_feed.rs index d3fdbb3..e8d2b61 100644 --- a/hammond-data/src/index_feed.rs +++ b/hammond-data/src/index_feed.rs @@ -53,6 +53,7 @@ impl Feed { let conn = db.lock().unwrap(); let e = conn.transaction::<(), Error, _>(|| { + // TODO: episodes.iter().for_each(|x| { let e = index_episode(&conn, x); if let Err(err) = e { @@ -68,28 +69,6 @@ impl Feed { } } -pub fn index_source(con: &SqliteConnection, foo: &NewSource) { - use schema::source::dsl::*; - - // Throw away the result like `insert or ignore` - // Diesel deos not support `insert or ignore` yet. - let _ = diesel::insert_into(source).values(foo).execute(con); -} - -pub fn index_podcast(con: &SqliteConnection, pd: &NewPodcast) -> Result<()> { - use schema::podcast::dsl::*; - - match dbqueries::get_podcast_from_title(con, &pd.title) { - Ok(foo) => if foo.link() != pd.link || foo.description() != pd.description { - diesel::replace_into(podcast).values(pd).execute(con)?; - }, - Err(_) => { - diesel::insert_into(podcast).values(pd).execute(con)?; - } - } - Ok(()) -} - // TODO: Currently using diesel from master git. // Watch out for v0.99.0 beta and change the toml. fn index_episode(con: &SqliteConnection, ep: &NewEpisode) -> QueryResult<()> { @@ -203,8 +182,9 @@ mod tests { ]; inpt.iter().for_each(|feed| { - let tempdb = db.lock().unwrap(); - index_source(&tempdb, &NewSource::new_with_uri(feed)); + NewSource::new_with_uri(feed) + .into_source(&db.clone()) + .unwrap(); }); full_index_loop(&db).unwrap(); diff --git a/hammond-data/src/models/insertables.rs b/hammond-data/src/models/insertables.rs index 4de40e9..03d5b25 100644 --- a/hammond-data/src/models/insertables.rs +++ b/hammond-data/src/models/insertables.rs @@ -1,11 +1,12 @@ use diesel::prelude::*; +use diesel; use schema::{episode, podcast, source}; use models::{Podcast, Source}; use index_feed::Database; use errors::*; -use index_feed; +// use index_feed; use dbqueries; #[derive(Insertable)] @@ -26,10 +27,20 @@ impl<'a> NewSource<'a> { } } + fn index(&self, db: &Database) { + use schema::source::dsl::*; + + let tempdb = db.lock().unwrap(); + // Throw away the result like `insert or ignore` + // Diesel deos not support `insert or ignore` yet. + let _ = diesel::insert_into(source).values(self).execute(&*tempdb); + } + // Look out for when tryinto lands into stable. pub fn into_source(self, db: &Database) -> QueryResult { + self.index(db); + let tempdb = db.lock().unwrap(); - index_feed::index_source(&tempdb, &self); dbqueries::get_source_from_uri(&tempdb, self.uri) } } @@ -62,9 +73,30 @@ pub struct NewPodcast { impl NewPodcast { // Look out for when tryinto lands into stable. pub fn into_podcast(self, db: &Database) -> Result { + self.index(db)?; let tempdb = db.lock().unwrap(); - index_feed::index_podcast(&tempdb, &self)?; - Ok(dbqueries::get_podcast_from_title(&tempdb, &self.title)?) } + + fn index(&self, db: &Database) -> Result<()> { + use schema::podcast::dsl::*; + let pd = { + let tempdb = db.lock().unwrap(); + dbqueries::get_podcast_from_title(&tempdb, &self.title) + }; + + match pd { + Ok(foo) => if foo.link() != self.link { + let tempdb = db.lock().unwrap(); + diesel::replace_into(podcast) + .values(self) + .execute(&*tempdb)?; + }, + Err(_) => { + let tempdb = db.lock().unwrap(); + diesel::insert_into(podcast).values(self).execute(&*tempdb)?; + } + } + Ok(()) + } }