From 78a892b4beba37e4bdc3fe9c939bc47b1e763252 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Thu, 21 Sep 2017 11:44:28 +0300 Subject: [PATCH] Setters and getters, and decoupling of insert and update logic for Episode struct. --- src/index_feed.rs | 57 ++++++++++++++++++++------------ src/models.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 117 insertions(+), 22 deletions(-) diff --git a/src/index_feed.rs b/src/index_feed.rs index 5ff3167..1eadca8 100644 --- a/src/index_feed.rs +++ b/src/index_feed.rs @@ -6,7 +6,7 @@ use schema; use dbqueries; use feedparser; use errors::*; -use models::{NewEpisode, NewSource, Source, Podcast}; +use models::{NewEpisode, NewSource, Source, Podcast, Episode}; pub fn foo() { let inpt = vec![ @@ -35,6 +35,7 @@ fn insert_source(con: &SqliteConnection, url: &str) -> Result<()> { match dbqueries::load_source(con, foo.uri) { Ok(mut bar) => { + // TODO: Cmp first before replacing // FIXME: NewSource has None values for etag, and last_mod atm // bar.set_http_etag(foo.http_etag.map(|x| x.to_string())); // bar.set_last_modified(foo.last_modified.map(|x| x.to_string())); @@ -54,11 +55,12 @@ fn index_podcast(con: &SqliteConnection, channel: &rss::Channel, parent: &Source let pd = feedparser::parse_podcast(channel, parent.id())?; match dbqueries::load_podcast(con, &pd.title) { - Ok(mut bar) => { - bar.set_link(pd.link); - bar.set_description(pd.description); - bar.set_image_uri(pd.image_uri.map(|x| x.to_string())); - bar.save_changes::(con)?; + Ok(mut foo) => { + // TODO: Cmp first before replacing + foo.set_link(pd.link); + foo.set_description(pd.description); + foo.set_image_uri(pd.image_uri.map(|x| x.to_string())); + foo.save_changes::(con)?; } Err(_) => { diesel::insert(&pd).into(schema::podcast::table).execute( @@ -70,6 +72,29 @@ fn index_podcast(con: &SqliteConnection, channel: &rss::Channel, parent: &Source Ok(()) } +fn index_episode(con: &SqliteConnection, item: &rss::Item, parent: &Podcast) -> Result<()> { + let ep = feedparser::parse_episode(item, parent.id())?; + + match dbqueries::load_episode(con, &ep.uri.unwrap()) { + Ok(mut foo) => { + // TODO: Cmp first before replacing + foo.set_title(ep.title.map(|x| x.to_string())); + foo.set_description(ep.description.map(|x| x.to_string())); + foo.set_published_date(ep.published_date.map(|x| x.to_string())); + foo.set_guid(ep.guid.map(|x| x.to_string())); + foo.set_length(ep.length); + foo.set_epoch(ep.length); + foo.save_changes::(con)?; + } + Err(_) => { + diesel::insert(&ep).into(schema::episode::table).execute( + con, + )?; + } + } + + Ok(()) +} pub fn index_loop(db: SqliteConnection) -> Result<()> { // let db = ::establish_connection(); @@ -83,28 +108,18 @@ pub fn index_loop(db: SqliteConnection) -> Result<()> { // This method will defently get split and nuked // but for now its poc let chan = feed.get_podcast_chan(&db)?; - let pd = feedparser::parse_podcast(&chan, feed.id())?; index_podcast(&db, &chan, &feed)?; - // TODO: Separate the insert/update logic - // diesel::insert_or_replace(&pd) - // .into(schema::podcast::table) - // .execute(&db)?; + // Ignore this for the moment + let p = feedparser::parse_podcast(&chan, feed.id())?; + let pd = dbqueries::load_podcast(&db, &p.title)?; - // Holy shit this works! - let episodes: Vec<_> = chan.items() + let _: Vec<_> = chan.items() .iter() - .map(|x| feedparser::parse_episode(x, feed.id()).unwrap()) + .map(|x| index_episode(&db, &x, &pd)) .collect(); - // lazy invoking the compiler to check for the Vec type :3 - // let first: &NewEpisode = episodes.first().unwrap(); - - diesel::insert_or_replace(&episodes) - .into(schema::episode::table) - .execute(&db)?; - info!("{:#?}", pd); // info!("{:#?}", episodes); // info!("{:?}", chan); diff --git a/src/models.rs b/src/models.rs index 5595dfd..bce3596 100644 --- a/src/models.rs +++ b/src/models.rs @@ -7,7 +7,7 @@ use reqwest::header::{ETag, LastModified}; use schema::{episode, podcast, source}; use errors::*; -#[derive(Queryable, Identifiable)] +#[derive(Queryable, Identifiable, AsChangeset)] #[derive(Associations)] #[table_name = "episode"] #[belongs_to(Podcast, foreign_key = "podcast_id")] @@ -25,6 +25,82 @@ pub struct Episode { podcast_id: i32, } +impl Episode { + pub fn id(&self) -> i32 { + self.id + } + + // FIXME: Return &str instead of String + pub fn title(self) -> Option { + self.title + } + + pub fn set_title(&mut self, value: Option) { + self.title = value; + } + + // FIXME: Return &str instead of String + pub fn uri(self) -> Option { + self.uri + } + + pub fn set_uri(&mut self, value: Option) { + self.uri = value; + } + + // FIXME: Return &str instead of String + pub fn local_uri(self) -> Option { + self.local_uri + } + + pub fn set_local_uri(&mut self, value: Option) { + self.local_uri = value; + } + + // FIXME: Return &str instead of String + pub fn description(self) -> Option { + self.description + } + + pub fn set_description(&mut self, value: Option) { + self.description = value; + } + + // FIXME: Return &str instead of String + pub fn published_date(self) -> Option { + self.published_date + } + + pub fn set_published_date(&mut self, value: Option) { + self.published_date = value; + } + + // FIXME: Return &str instead of String + pub fn guid(self) -> Option { + self.guid + } + + pub fn set_guid(&mut self, value: Option) { + self.guid = value; + } + + pub fn epoch(&self) -> Option { + self.epoch + } + + pub fn set_epoch(&mut self, value: Option) { + self.epoch = value; + } + + pub fn length(&self) -> Option { + self.length + } + + pub fn set_length(&mut self, value: Option) { + self.length = value; + } +} + #[derive(Queryable, Identifiable, AsChangeset)] #[derive(Associations)] #[belongs_to(Source, foreign_key = "source_id")] @@ -44,6 +120,10 @@ impl Podcast { self.id } + pub fn title(&self) -> &str { + &self.title + } + pub fn link(&self) -> &str { &self.link }