Split hammond-data/models into sub modules.

This commit is contained in:
Jordan Petridis 2017-11-15 17:42:22 +02:00
parent 118846f255
commit cfb876e006
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
5 changed files with 73 additions and 63 deletions

View File

@ -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<String>,
pub length: Option<i32>,
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<String>,
pub source_id: i32,
}

View File

@ -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};

View File

@ -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<NewPodcast> 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::<Source>(&*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<String>,
pub length: Option<i32>,
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<String>,
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,
}
}
}

View File

@ -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",

View File

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