Initial prototype of decoupling insert/update logic.
This commit is contained in:
parent
6b4b772462
commit
356cc54ba1
@ -1,7 +1,13 @@
|
||||
use diesel::prelude::*;
|
||||
use schema::source::dsl::*;
|
||||
use models::{Podcast, Episode, Source};
|
||||
|
||||
pub fn get_sources(con: &SqliteConnection) -> QueryResult<Vec<Source>> {
|
||||
use schema::source::dsl::*;
|
||||
|
||||
let s = source.load::<Source>(con);
|
||||
s
|
||||
}
|
||||
|
||||
pub fn get_podcasts(con: &SqliteConnection, parent: &Source) -> QueryResult<Vec<Podcast>> {
|
||||
let pds = Podcast::belonging_to(parent).load::<Podcast>(con);
|
||||
// debug!("Returned Podcasts:\n{:?}", pds);
|
||||
@ -13,7 +19,23 @@ pub fn get_pd_episodes(con: &SqliteConnection, parent: &Podcast) -> QueryResult<
|
||||
eps
|
||||
}
|
||||
|
||||
pub fn get_sources(con: &SqliteConnection) -> QueryResult<Vec<Source>>{
|
||||
let s = source.load::<Source>(con);
|
||||
s
|
||||
}
|
||||
pub fn load_source(con: &SqliteConnection, uri_: &str) -> QueryResult<Source> {
|
||||
use schema::source::dsl::*;
|
||||
|
||||
let s = source.filter(uri.eq(uri_)).get_result::<Source>(con);
|
||||
s
|
||||
}
|
||||
|
||||
pub fn load_podcast(con: &SqliteConnection, title_: &str) -> QueryResult<Podcast> {
|
||||
use schema::podcast::dsl::*;
|
||||
|
||||
let pd = podcast.filter(title.eq(title_)).get_result::<Podcast>(con);
|
||||
pd
|
||||
}
|
||||
|
||||
pub fn load_episode(con: &SqliteConnection, uri_: &str) -> QueryResult<Episode> {
|
||||
use schema::episode::dsl::*;
|
||||
|
||||
let ep = episode.filter(uri.eq(uri_)).get_result::<Episode>(con);
|
||||
ep
|
||||
}
|
||||
@ -30,9 +30,17 @@ pub fn foo() {
|
||||
fn insert_source(connection: &SqliteConnection, url: &str) -> Result<()> {
|
||||
let foo = NewSource::new_with_uri(url);
|
||||
|
||||
diesel::insert_or_replace(&foo)
|
||||
.into(schema::source::table)
|
||||
.execute(connection)?;
|
||||
match dbqueries::load_source(connection, foo.uri) {
|
||||
Ok(mut bar) => {
|
||||
bar.set_http_etag(foo.http_etag.map(|x| x.to_string()));
|
||||
bar.set_last_modified(foo.last_modified.map(|x| x.to_string()));
|
||||
}
|
||||
Err(_) => {
|
||||
diesel::insert(&foo).into(schema::source::table).execute(
|
||||
connection,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -62,10 +62,18 @@ impl<'a> Source {
|
||||
self.last_modified
|
||||
}
|
||||
|
||||
pub fn set_last_modified(&mut self, value: Option<String>) {
|
||||
self.last_modified = value;
|
||||
}
|
||||
|
||||
pub fn http_etag(self) -> Option<String> {
|
||||
self.http_etag
|
||||
}
|
||||
|
||||
pub fn set_http_etag(&mut self, value: Option<String>) {
|
||||
self.http_etag = value;
|
||||
}
|
||||
|
||||
/// Fetch the xml feed from the source url, update the etag headers,
|
||||
/// and parse the feed into an rss:Channel and return it.
|
||||
pub fn get_podcast_chan(&mut self, con: &SqliteConnection) -> Result<Channel> {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user