diff --git a/hammond-data/src/models/insertables.rs b/hammond-data/src/models/insertables.rs new file mode 100644 index 0000000..64f8c0e --- /dev/null +++ b/hammond-data/src/models/insertables.rs @@ -0,0 +1,45 @@ +use schema::{episode, podcast, source}; + +#[derive(Insertable)] +#[table_name = "source"] +#[derive(Debug, Clone)] +pub struct NewSource<'a> { + pub uri: &'a str, + pub last_modified: Option<&'a str>, + pub http_etag: Option<&'a str>, +} + +impl<'a> NewSource<'a> { + pub fn new_with_uri(uri: &'a str) -> NewSource { + NewSource { + uri, + last_modified: None, + http_etag: None, + } + } +} + +#[derive(Insertable)] +#[table_name = "episode"] +#[derive(Debug, Clone)] +pub struct NewEpisode<'a> { + pub title: Option<&'a str>, + pub uri: Option<&'a str>, + pub description: Option<&'a str>, + pub published_date: Option, + pub length: Option, + pub guid: Option<&'a str>, + pub epoch: i32, + pub podcast_id: i32, +} + +#[derive(Insertable)] +#[table_name = "podcast"] +#[derive(Debug, Clone)] +pub struct NewPodcast { + pub title: String, + pub link: String, + pub description: String, + pub image_uri: Option, + pub source_id: i32, +} diff --git a/hammond-data/src/models/mod.rs b/hammond-data/src/models/mod.rs new file mode 100644 index 0000000..fd24e4b --- /dev/null +++ b/hammond-data/src/models/mod.rs @@ -0,0 +1,6 @@ +mod insertables; +mod queryables; + +// Re-export the structs so the API doesn't change and brake everything else. +pub use self::queryables::{Episode, Podcast, Source}; +pub use self::insertables::{NewEpisode, NewPodcast, NewSource}; diff --git a/hammond-data/src/models.rs b/hammond-data/src/models/queryables.rs similarity index 84% rename from hammond-data/src/models.rs rename to hammond-data/src/models/queryables.rs index 55e7423..15b0aa2 100644 --- a/hammond-data/src/models.rs +++ b/hammond-data/src/models/queryables.rs @@ -1,3 +1,4 @@ + use reqwest; use diesel::SaveChangesDsl; use diesel::result::QueryResult; @@ -7,6 +8,8 @@ use schema::{episode, podcast, source}; use index_feed::Database; use errors::*; +use models::insertables::NewPodcast; + #[derive(Queryable, Identifiable, AsChangeset, Associations)] #[table_name = "episode"] #[changeset_options(treat_none_as_null = "true")] @@ -147,6 +150,23 @@ pub struct Podcast { source_id: i32, } +/// This is meant only to be used to make unit tests easier. +impl From for Podcast { + fn from(pd: NewPodcast) -> Podcast { + Podcast { + id: 0, + title: pd.title, + link: pd.link, + description: pd.description, + image_uri: pd.image_uri, + source_id: pd.source_id, + always_dl: false, + archive: false, + favorite: false, + } + } +} + impl Podcast { pub fn id(&self) -> i32 { self.id @@ -276,64 +296,3 @@ impl<'a> Source { self.save_changes::(&*tempdb) } } - -#[derive(Insertable)] -#[table_name = "source"] -#[derive(Debug, Clone)] -pub struct NewSource<'a> { - pub uri: &'a str, - pub last_modified: Option<&'a str>, - pub http_etag: Option<&'a str>, -} - -impl<'a> NewSource<'a> { - pub fn new_with_uri(uri: &'a str) -> NewSource { - NewSource { - uri, - last_modified: None, - http_etag: None, - } - } -} - -#[derive(Insertable)] -#[table_name = "episode"] -#[derive(Debug, Clone)] -pub struct NewEpisode<'a> { - pub title: Option<&'a str>, - pub uri: Option<&'a str>, - pub description: Option<&'a str>, - pub published_date: Option, - pub length: Option, - pub guid: Option<&'a str>, - pub epoch: i32, - pub podcast_id: i32, -} - -#[derive(Insertable)] -#[table_name = "podcast"] -#[derive(Debug, Clone)] -pub struct NewPodcast { - pub title: String, - pub link: String, - pub description: String, - pub image_uri: Option, - pub source_id: i32, -} - -impl NewPodcast { - // This is meant only to be used to make unit tests easier. - pub fn into_podcast(self) -> Podcast { - Podcast { - id: 0, - title: self.title, - link: self.link, - description: self.description, - image_uri: self.image_uri, - source_id: self.source_id, - always_dl: false, - archive: false, - favorite: false, - } - } -} diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index b38de15..1b25d65 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -215,7 +215,7 @@ mod tests { image_uri: Some("http://newrustacean.com/podcast.png".to_string()), source_id: 0, }; - let pd = pd.into_podcast(); + let pd = Podcast::from(pd); let img_path = cache_image(&pd); let foo_ = format!( "{}{}/cover.png", diff --git a/hammond-gtk/src/widgets/podcast.rs b/hammond-gtk/src/widgets/podcast.rs index b3d1d97..0d07609 100644 --- a/hammond-gtk/src/widgets/podcast.rs +++ b/hammond-gtk/src/widgets/podcast.rs @@ -142,7 +142,7 @@ mod tests { image_uri: Some("http://newrustacean.com/podcast.png".to_string()), source_id: 0, }; - let pd = pd.into_podcast(); + let pd = Podcast::from(pd); let pxbuf = get_pixbuf_from_path(&pd); assert!(pxbuf.is_some());