Document Episode Model.

This commit is contained in:
Jordan Petridis 2017-11-28 08:25:04 +02:00
parent d81f9c6ad7
commit 3707b70b41
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
3 changed files with 34 additions and 3 deletions

View File

@ -20,6 +20,7 @@ use std::str::FromStr;
#[changeset_options(treat_none_as_null = "true")] #[changeset_options(treat_none_as_null = "true")]
#[belongs_to(Podcast, foreign_key = "podcast_id")] #[belongs_to(Podcast, foreign_key = "podcast_id")]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
/// Diesel Model of the episode table.
pub struct Episode { pub struct Episode {
id: i32, id: i32,
title: Option<String>, title: Option<String>,
@ -39,99 +40,128 @@ pub struct Episode {
} }
impl Episode { impl Episode {
/// Get the value of the `title` field.
pub fn title(&self) -> Option<&str> { pub fn title(&self) -> Option<&str> {
self.title.as_ref().map(|s| s.as_str()) self.title.as_ref().map(|s| s.as_str())
} }
/// Set the `title`.
pub fn set_title(&mut self, value: Option<&str>) { pub fn set_title(&mut self, value: Option<&str>) {
self.title = value.map(|x| x.to_string()); self.title = value.map(|x| x.to_string());
} }
/// uri is guaranted to exist based on the db rules /// Get the value of the `uri`.
/// Represents the url(usually) that the media file will be located at.
pub fn uri(&self) -> &str { pub fn uri(&self) -> &str {
self.uri.as_ref() self.uri.as_ref()
} }
/// Set the `uri`.
pub fn set_uri(&mut self, value: &str) { pub fn set_uri(&mut self, value: &str) {
self.uri = value.to_string(); self.uri = value.to_string();
} }
/// Get the value of the `local_uri`.
/// Represents the local uri,usually filesystem path, that the media file will be located at.
pub fn local_uri(&self) -> Option<&str> { pub fn local_uri(&self) -> Option<&str> {
self.local_uri.as_ref().map(|s| s.as_str()) self.local_uri.as_ref().map(|s| s.as_str())
} }
/// Set the `local_uri`.
pub fn set_local_uri(&mut self, value: Option<&str>) { pub fn set_local_uri(&mut self, value: Option<&str>) {
self.local_uri = value.map(|x| x.to_string()); self.local_uri = value.map(|x| x.to_string());
} }
/// Get the value of the `description`.
pub fn description(&self) -> Option<&str> { pub fn description(&self) -> Option<&str> {
self.description.as_ref().map(|s| s.as_str()) self.description.as_ref().map(|s| s.as_str())
} }
/// Set the `description`.
pub fn set_description(&mut self, value: Option<&str>) { pub fn set_description(&mut self, value: Option<&str>) {
self.description = value.map(|x| x.to_string()); self.description = value.map(|x| x.to_string());
} }
/// Get the the `published_date`.
pub fn published_date(&self) -> Option<&str> { pub fn published_date(&self) -> Option<&str> {
self.published_date.as_ref().map(|s| s.as_str()) self.published_date.as_ref().map(|s| s.as_str())
} }
/// Set the `published_date`.
pub fn set_published_date(&mut self, value: Option<&str>) { pub fn set_published_date(&mut self, value: Option<&str>) {
self.published_date = value.map(|x| x.to_string().to_owned()); self.published_date = value.map(|x| x.to_string().to_owned());
} }
/// Get the value of the `description`.
pub fn guid(&self) -> Option<&str> { pub fn guid(&self) -> Option<&str> {
self.guid.as_ref().map(|s| s.as_str()) self.guid.as_ref().map(|s| s.as_str())
} }
/// Set the `guid`.
pub fn set_guid(&mut self, value: Option<&str>) { pub fn set_guid(&mut self, value: Option<&str>) {
self.guid = value.map(|x| x.to_string()); self.guid = value.map(|x| x.to_string());
} }
/// Get the `epoch` value.
/// Retrieved from the rss Item publish date.
pub fn epoch(&self) -> i32 { pub fn epoch(&self) -> i32 {
self.epoch self.epoch
} }
/// Set the `epoch`.
pub fn set_epoch(&mut self, value: i32) { pub fn set_epoch(&mut self, value: i32) {
self.epoch = value; self.epoch = value;
} }
/// Get the `length`.
pub fn length(&self) -> Option<i32> { pub fn length(&self) -> Option<i32> {
self.length self.length
} }
/// Set the `length`.
pub fn set_length(&mut self, value: Option<i32>) { pub fn set_length(&mut self, value: Option<i32>) {
self.length = value; self.length = value;
} }
/// Epoch representation of the last time the episode was played.
/// None/Null for unplayed.
pub fn played(&self) -> Option<i32> { pub fn played(&self) -> Option<i32> {
self.played self.played
} }
/// Set the `played` value.
pub fn set_played(&mut self, value: Option<i32>) { pub fn set_played(&mut self, value: Option<i32>) {
self.played = value; self.played = value;
} }
/// Represents the archiving policy for the episode.
pub fn archive(&self) -> bool { pub fn archive(&self) -> bool {
self.archive self.archive
} }
/// Set the `archive` policy.
/// If true, the download cleanr will ignore the episode
/// and the corresponding media value will never be automaticly deleted.
pub fn set_archive(&mut self, b: bool) { pub fn set_archive(&mut self, b: bool) {
self.archive = b self.archive = b
} }
/// Get the `favorite` statues of the `Episode`.
pub fn favorite(&self) -> bool { pub fn favorite(&self) -> bool {
self.favorite self.favorite
} }
/// Set `favorite`.
pub fn set_favorite(&mut self, b: bool) { pub fn set_favorite(&mut self, b: bool) {
self.favorite = b self.favorite = b
} }
/// `Podcast` foreign key.
pub fn podcast_id(&self) -> i32 { pub fn podcast_id(&self) -> i32 {
self.podcast_id self.podcast_id
} }
/// Sets the `played` value with the current `epoch` timestap and save it.
pub fn set_played_now(&mut self) -> Result<()> { pub fn set_played_now(&mut self) -> Result<()> {
let epoch = Utc::now().timestamp() as i32; let epoch = Utc::now().timestamp() as i32;
self.set_played(Some(epoch)); self.set_played(Some(epoch));
@ -139,6 +169,7 @@ impl Episode {
Ok(()) Ok(())
} }
/// Helper method to easily save/"sync" current state of self to the Database.
pub fn save(&self) -> Result<Episode> { pub fn save(&self) -> Result<Episode> {
let db = connection(); let db = connection();
let tempdb = db.get()?; let tempdb = db.get()?;

View File

@ -207,7 +207,7 @@ mod tests {
// Convert Source it into a Feed and index it // Convert Source it into a Feed and index it
let feed = source.into_feed().unwrap(); let feed = source.into_feed().unwrap();
index(vec!(feed)); index(vec![feed]);
// Get the Podcast // Get the Podcast
let pd = dbqueries::get_podcast_from_source_id(sid).unwrap(); let pd = dbqueries::get_podcast_from_source_id(sid).unwrap();

View File

@ -134,7 +134,7 @@ mod tests {
// Convert Source it into a Feed and index it // Convert Source it into a Feed and index it
let feed = source.into_feed().unwrap(); let feed = source.into_feed().unwrap();
index(vec!(feed)); index(vec![feed]);
// Get the Podcast // Get the Podcast
let pd = dbqueries::get_podcast_from_source_id(sid).unwrap(); let pd = dbqueries::get_podcast_from_source_id(sid).unwrap();