Setters and getters, and decoupling of insert and update logic for Episode struct.

This commit is contained in:
Jordan Petridis 2017-09-21 11:44:28 +03:00
parent 5dc6c002fe
commit 78a892b4be
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 117 additions and 22 deletions

View File

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

View File

@ -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<String> {
self.title
}
pub fn set_title(&mut self, value: Option<String>) {
self.title = value;
}
// FIXME: Return &str instead of String
pub fn uri(self) -> Option<String> {
self.uri
}
pub fn set_uri(&mut self, value: Option<String>) {
self.uri = value;
}
// FIXME: Return &str instead of String
pub fn local_uri(self) -> Option<String> {
self.local_uri
}
pub fn set_local_uri(&mut self, value: Option<String>) {
self.local_uri = value;
}
// FIXME: Return &str instead of String
pub fn description(self) -> Option<String> {
self.description
}
pub fn set_description(&mut self, value: Option<String>) {
self.description = value;
}
// FIXME: Return &str instead of String
pub fn published_date(self) -> Option<String> {
self.published_date
}
pub fn set_published_date(&mut self, value: Option<String>) {
self.published_date = value;
}
// FIXME: Return &str instead of String
pub fn guid(self) -> Option<String> {
self.guid
}
pub fn set_guid(&mut self, value: Option<String>) {
self.guid = value;
}
pub fn epoch(&self) -> Option<i32> {
self.epoch
}
pub fn set_epoch(&mut self, value: Option<i32>) {
self.epoch = value;
}
pub fn length(&self) -> Option<i32> {
self.length
}
pub fn set_length(&mut self, value: Option<i32>) {
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
}