Setters and getters, and decoupling of insert and update logic for Episode struct.
This commit is contained in:
parent
5dc6c002fe
commit
78a892b4be
@ -6,7 +6,7 @@ use schema;
|
|||||||
use dbqueries;
|
use dbqueries;
|
||||||
use feedparser;
|
use feedparser;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use models::{NewEpisode, NewSource, Source, Podcast};
|
use models::{NewEpisode, NewSource, Source, Podcast, Episode};
|
||||||
|
|
||||||
pub fn foo() {
|
pub fn foo() {
|
||||||
let inpt = vec![
|
let inpt = vec![
|
||||||
@ -35,6 +35,7 @@ fn insert_source(con: &SqliteConnection, url: &str) -> Result<()> {
|
|||||||
|
|
||||||
match dbqueries::load_source(con, foo.uri) {
|
match dbqueries::load_source(con, foo.uri) {
|
||||||
Ok(mut bar) => {
|
Ok(mut bar) => {
|
||||||
|
// TODO: Cmp first before replacing
|
||||||
// FIXME: NewSource has None values for etag, and last_mod atm
|
// 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_http_etag(foo.http_etag.map(|x| x.to_string()));
|
||||||
// bar.set_last_modified(foo.last_modified.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())?;
|
let pd = feedparser::parse_podcast(channel, parent.id())?;
|
||||||
|
|
||||||
match dbqueries::load_podcast(con, &pd.title) {
|
match dbqueries::load_podcast(con, &pd.title) {
|
||||||
Ok(mut bar) => {
|
Ok(mut foo) => {
|
||||||
bar.set_link(pd.link);
|
// TODO: Cmp first before replacing
|
||||||
bar.set_description(pd.description);
|
foo.set_link(pd.link);
|
||||||
bar.set_image_uri(pd.image_uri.map(|x| x.to_string()));
|
foo.set_description(pd.description);
|
||||||
bar.save_changes::<Podcast>(con)?;
|
foo.set_image_uri(pd.image_uri.map(|x| x.to_string()));
|
||||||
|
foo.save_changes::<Podcast>(con)?;
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
diesel::insert(&pd).into(schema::podcast::table).execute(
|
diesel::insert(&pd).into(schema::podcast::table).execute(
|
||||||
@ -70,6 +72,29 @@ fn index_podcast(con: &SqliteConnection, channel: &rss::Channel, parent: &Source
|
|||||||
Ok(())
|
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<()> {
|
pub fn index_loop(db: SqliteConnection) -> Result<()> {
|
||||||
// let db = ::establish_connection();
|
// let db = ::establish_connection();
|
||||||
@ -83,28 +108,18 @@ pub fn index_loop(db: SqliteConnection) -> Result<()> {
|
|||||||
// This method will defently get split and nuked
|
// This method will defently get split and nuked
|
||||||
// but for now its poc
|
// but for now its poc
|
||||||
let chan = feed.get_podcast_chan(&db)?;
|
let chan = feed.get_podcast_chan(&db)?;
|
||||||
let pd = feedparser::parse_podcast(&chan, feed.id())?;
|
|
||||||
|
|
||||||
index_podcast(&db, &chan, &feed)?;
|
index_podcast(&db, &chan, &feed)?;
|
||||||
|
|
||||||
// TODO: Separate the insert/update logic
|
// Ignore this for the moment
|
||||||
// diesel::insert_or_replace(&pd)
|
let p = feedparser::parse_podcast(&chan, feed.id())?;
|
||||||
// .into(schema::podcast::table)
|
let pd = dbqueries::load_podcast(&db, &p.title)?;
|
||||||
// .execute(&db)?;
|
|
||||||
|
|
||||||
// Holy shit this works!
|
let _: Vec<_> = chan.items()
|
||||||
let episodes: Vec<_> = chan.items()
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| feedparser::parse_episode(x, feed.id()).unwrap())
|
.map(|x| index_episode(&db, &x, &pd))
|
||||||
.collect();
|
.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!("{:#?}", pd);
|
||||||
// info!("{:#?}", episodes);
|
// info!("{:#?}", episodes);
|
||||||
// info!("{:?}", chan);
|
// info!("{:?}", chan);
|
||||||
|
|||||||
@ -7,7 +7,7 @@ use reqwest::header::{ETag, LastModified};
|
|||||||
use schema::{episode, podcast, source};
|
use schema::{episode, podcast, source};
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable)]
|
#[derive(Queryable, Identifiable, AsChangeset)]
|
||||||
#[derive(Associations)]
|
#[derive(Associations)]
|
||||||
#[table_name = "episode"]
|
#[table_name = "episode"]
|
||||||
#[belongs_to(Podcast, foreign_key = "podcast_id")]
|
#[belongs_to(Podcast, foreign_key = "podcast_id")]
|
||||||
@ -25,6 +25,82 @@ pub struct Episode {
|
|||||||
podcast_id: i32,
|
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(Queryable, Identifiable, AsChangeset)]
|
||||||
#[derive(Associations)]
|
#[derive(Associations)]
|
||||||
#[belongs_to(Source, foreign_key = "source_id")]
|
#[belongs_to(Source, foreign_key = "source_id")]
|
||||||
@ -44,6 +120,10 @@ impl Podcast {
|
|||||||
self.id
|
self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn title(&self) -> &str {
|
||||||
|
&self.title
|
||||||
|
}
|
||||||
|
|
||||||
pub fn link(&self) -> &str {
|
pub fn link(&self) -> &str {
|
||||||
&self.link
|
&self.link
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user