Add a new Diesel Model for the EpisodeWidget.

This commit is contained in:
Jordan Petridis 2017-12-14 12:01:35 +02:00
parent ebbebf7735
commit 8fe6b526a5
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
5 changed files with 157 additions and 10 deletions

View File

@ -2,7 +2,7 @@
use diesel::prelude::*;
use diesel;
use models::queryables::{Episode, Podcast, Source};
use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, Source};
use chrono::prelude::*;
use errors::*;
@ -103,6 +103,21 @@ pub fn get_pd_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
.load::<Episode>(&*con)?)
}
pub fn get_pd_episodeswidgets(parent: &Podcast) -> Result<Vec<EpisodeWidgetQuery>> {
use schema::episode::dsl::*;
let db = connection();
let con = db.get()?;
Ok(
episode.select((rowid, title, uri, local_uri, epoch, length, played, podcast_id))
.filter(podcast_id.eq(parent.id()))
// .group_by(epoch)
.order(epoch.desc())
.load::<EpisodeWidgetQuery>(&*con)?,
)
}
pub fn get_pd_unplayed_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
use schema::episode::dsl::*;

View File

@ -61,7 +61,7 @@ pub(crate) mod models;
mod parser;
mod schema;
pub use models::queryables::{Episode, Podcast, Source};
pub use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, Source};
/// [XDG Base Direcotory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) Paths.
#[allow(missing_debug_implementations)]

View File

@ -1,4 +1,6 @@
use chrono::prelude::*;
use diesel::prelude::*;
use diesel;
use reqwest;
use diesel::SaveChangesDsl;
@ -189,6 +191,133 @@ impl Episode {
}
}
#[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 constructing `EpisodeWidgets`.
pub struct EpisodeWidgetQuery {
rowid: i32,
title: String,
uri: Option<String>,
local_uri: Option<String>,
epoch: i32,
length: Option<i32>,
played: Option<i32>,
// favorite: bool,
// archive: bool,
podcast_id: i32,
}
impl EpisodeWidgetQuery {
/// 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 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> {
self.local_uri.as_ref().map(|s| s.as_str())
}
/// Set the `local_uri`.
pub fn set_local_uri(&mut self, value: Option<&str>) {
self.local_uri = value.map(|x| x.to_string());
}
/// 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 `length`.
pub fn length(&self) -> Option<i32> {
self.length
}
/// Set the `length`.
pub fn set_length(&mut self, value: Option<i32>) {
self.length = value;
}
/// Epoch representation of the last time the episode was played.
///
/// None/Null for unplayed.
pub fn played(&self) -> Option<i32> {
self.played
}
/// Set the `played` value.
pub fn set_played(&mut self, value: Option<i32>) {
self.played = value;
}
// /// Represents the archiving policy for the episode.
// pub fn archive(&self) -> bool {
// 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) {
// self.archive = b
// }
// /// Get the `favorite` status of the `Episode`.
// pub fn favorite(&self) -> bool {
// self.favorite
// }
// /// Set `favorite` status.
// pub fn set_favorite(&mut self, b: bool) {
// self.favorite = b
// }
/// `Podcast` table foreign key.
pub fn podcast_id(&self) -> i32 {
self.podcast_id
}
/// Sets the `played` value with the current `epoch` timestap and save it.
pub fn set_played_now(&mut self) -> Result<()> {
let epoch = Utc::now().timestamp() as i32;
self.set_played(Some(epoch));
self.save()?;
Ok(())
}
/// Helper method to easily save/"sync" current state of self to the Database.
pub fn save(&self) -> Result<usize> {
use schema::episode::dsl::*;
let db = connection();
let tempdb = db.get()?;
Ok(diesel::update(episode).set(self).execute(&*tempdb)?)
}
}
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
#[belongs_to(Source, foreign_key = "source_id")]
#[changeset_options(treat_none_as_null = "true")]

View File

@ -8,7 +8,7 @@ use std::io::{BufWriter, Read, Write};
use std::path::Path;
use errors::*;
use hammond_data::{Episode, Podcast};
use hammond_data::{Episode, EpisodeWidgetQuery, Podcast};
use hammond_data::xdg_dirs::{DL_DIR, HAMMOND_CACHE};
// TODO: Replace path that are of type &str with std::path.
@ -106,7 +106,7 @@ pub fn get_download_folder(pd_title: &str) -> Result<String> {
}
// TODO: Refactor
pub fn get_episode(ep: &mut Episode, download_folder: &str) -> Result<()> {
pub fn get_episode(ep: &mut EpisodeWidgetQuery, download_folder: &str) -> Result<()> {
// Check if its alrdy downloaded
if ep.local_uri().is_some() {
if Path::new(ep.local_uri().unwrap()).exists() {

View File

@ -5,10 +5,10 @@ use gtk::prelude::*;
use open;
use hammond_data::dbqueries;
use hammond_data::{Episode, Podcast};
use hammond_downloader::downloader;
use hammond_data::{EpisodeWidgetQuery, Podcast};
use hammond_data::utils::*;
use hammond_data::errors::*;
use hammond_downloader::downloader;
use std::thread;
use std::cell::RefCell;
@ -73,13 +73,16 @@ impl EpisodeWidget {
}
}
pub fn new_initialized(episode: &mut Episode, pd: &Podcast) -> EpisodeWidget {
pub fn new_initialized(episode: &mut EpisodeWidgetQuery, pd: &Podcast) -> EpisodeWidget {
let widget = EpisodeWidget::new();
widget.init(episode, pd);
widget
}
fn init(&self, episode: &mut Episode, pd: &Podcast) {
// TODO: calculate lenght.
// TODO: wire the progress_bar to the downloader.
// TODO: wire the cancel button.
fn init(&self, episode: &mut EpisodeWidgetQuery, pd: &Podcast) {
self.title.set_xalign(0.0);
self.title.set_text(episode.title());
@ -131,7 +134,7 @@ impl EpisodeWidget {
// TODO: show notification when dl is finished.
fn on_download_clicked(
pd_title: &str,
ep: &mut Episode,
ep: &mut EpisodeWidgetQuery,
download_bttn: &gtk::Button,
play_bttn: &gtk::Button,
del_bttn: &gtk::Button,
@ -225,7 +228,7 @@ fn receive() -> glib::Continue {
}
pub fn episodes_listbox(pd: &Podcast) -> Result<gtk::ListBox> {
let episodes = dbqueries::get_pd_episodes(pd)?;
let episodes = dbqueries::get_pd_episodeswidgets(pd)?;
let list = gtk::ListBox::new();
episodes.into_iter().for_each(|mut ep| {