Move some indexing functions into methods of insertable models.

This commit is contained in:
Jordan Petridis 2017-11-17 18:58:44 +02:00
parent 8eca46871d
commit 282a29e7dd
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 40 additions and 28 deletions

View File

@ -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();

View File

@ -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<Source> {
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<Podcast> {
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(())
}
}