hammond_data: Add an EpisodeMinimal Diesel model.

This commit is contained in:
Jordan Petridis 2018-01-17 10:21:16 +02:00
parent 574cfae5c6
commit 5d88998180
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
4 changed files with 115 additions and 8 deletions

View File

@ -6,6 +6,7 @@ use diesel::prelude::*;
use database::connection; use database::connection;
use errors::*; use errors::*;
use models::Podcast; use models::Podcast;
use models::new_episode::{NewEpisodeMinimal, NewEpisodeMinimalBuilder};
use schema::episode; use schema::episode;
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)] #[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
@ -411,3 +412,86 @@ impl EpisodeCleanerQuery {
.execute(&*tempdb)?) .execute(&*tempdb)?)
} }
} }
#[derive(Queryable, AsChangeset, PartialEq)]
#[table_name = "episode"]
#[changeset_options(treat_none_as_null = "true")]
#[primary_key(title, podcast_id)]
#[derive(Debug, Clone)]
/// Diesel Model to be used for FIXME.
pub struct EpisodeMinimal {
rowid: i32,
title: String,
uri: Option<String>,
epoch: i32,
duration: Option<i32>,
guid: Option<String>,
podcast_id: i32,
}
impl From<Episode> for EpisodeMinimal {
fn from(e: Episode) -> Self {
EpisodeMinimal {
rowid: e.rowid,
title: e.title,
uri: e.uri,
guid: e.guid,
epoch: e.epoch,
duration: e.duration,
podcast_id: e.podcast_id,
}
}
}
impl Into<NewEpisodeMinimal> for EpisodeMinimal {
fn into(self) -> NewEpisodeMinimal {
NewEpisodeMinimalBuilder::default()
.title(self.title)
.uri(self.uri)
.guid(self.guid)
.epoch(self.epoch)
.duration(self.duration)
.podcast_id(self.podcast_id)
.build()
.unwrap()
}
}
impl EpisodeMinimal {
/// Get the value of the sqlite's `ROW_ID`
pub fn rowid(&self) -> i32 {
self.rowid
}
/// Get the value of the `title` field.
pub fn title(&self) -> &str {
&self.title
}
/// Get the value of the `uri`.
///
/// Represents the url(usually) that the media file will be located at.
pub fn uri(&self) -> Option<&str> {
self.uri.as_ref().map(|s| s.as_str())
}
/// Get the `epoch` value.
///
/// Retrieved from the rss Item publish date.
/// Value is set to Utc whenever possible.
pub fn epoch(&self) -> i32 {
self.epoch
}
/// Get the `duration` value.
///
/// The number represents the duration of the item/episode in seconds.
pub fn duration(&self) -> Option<i32> {
self.duration
}
/// `Podcast` table foreign key.
pub fn podcast_id(&self) -> i32 {
self.podcast_id
}
}

View File

@ -12,13 +12,13 @@ pub(crate) use self::new_episode::{NewEpisode, NewEpisodeMinimal};
pub(crate) use self::new_podcast::NewPodcast; pub(crate) use self::new_podcast::NewPodcast;
pub(crate) use self::new_source::NewSource; pub(crate) use self::new_source::NewSource;
pub use self::episode::{Episode, EpisodeWidgetQuery}; pub use self::episode::{Episode, EpisodeMinimal, EpisodeWidgetQuery};
pub(crate) use self::episode::EpisodeCleanerQuery; pub(crate) use self::episode::EpisodeCleanerQuery;
pub use self::podcast::{Podcast, PodcastCoverQuery}; pub use self::podcast::{Podcast, PodcastCoverQuery};
pub use self::source::Source; pub use self::source::Source;
#[allow(dead_code)] #[allow(dead_code)]
enum IndexState<T> { pub enum IndexState<T> {
Index(T), Index(T),
Update(T), Update(T),
NotChanged, NotChanged,

View File

@ -8,8 +8,7 @@ use rss;
use dbqueries; use dbqueries;
use errors::*; use errors::*;
use models::{Insert, Update}; use models::{Episode, Insert, Update};
use models::Episode;
use parser; use parser;
use utils::{replace_extra_spaces, url_cleaner}; use utils::{replace_extra_spaces, url_cleaner};
@ -168,10 +167,8 @@ impl NewEpisodeMinimal {
bail!("No url specified for the item.") bail!("No url specified for the item.")
}; };
let date = parse_rfc822( // Default to rfc2822 represantation of epoch 0.
// Default to rfc2822 represantation of epoch 0. let date = parse_rfc822(item.pub_date().unwrap_or("Thu, 1 Jan 1970 00:00:00 +0000"));
item.pub_date().unwrap_or("Thu, 1 Jan 1970 00:00:00 +0000"),
);
// Should treat information from the rss feeds as invalid by default. // Should treat information from the rss feeds as invalid by default.
// Case: Thu, 05 Aug 2016 06:00:00 -0400 <-- Actually that was friday. // Case: Thu, 05 Aug 2016 06:00:00 -0400 <-- Actually that was friday.
let epoch = date.map(|x| x.timestamp() as i32).unwrap_or(0); let epoch = date.map(|x| x.timestamp() as i32).unwrap_or(0);
@ -216,3 +213,27 @@ impl NewEpisodeMinimal {
.unwrap() .unwrap()
} }
} }
#[allow(dead_code)]
// Ignore the following getters. They are used in unit tests mainly.
impl NewEpisodeMinimal {
pub(crate) fn title(&self) -> &str {
self.title.as_ref()
}
pub(crate) fn uri(&self) -> Option<&str> {
self.uri.as_ref().map(|s| s.as_str())
}
pub(crate) fn guid(&self) -> Option<&str> {
self.guid.as_ref().map(|s| s.as_str())
}
pub(crate) fn epoch(&self) -> i32 {
self.epoch
}
pub(crate) fn podcast_id(&self) -> i32 {
self.podcast_id
}
}

View File

@ -38,3 +38,5 @@ table! {
http_etag -> Nullable<Text>, http_etag -> Nullable<Text>,
} }
} }
allow_tables_to_appear_in_same_query!(episode, podcast, source,);