hammond-data: Factor out save method of diesel models into a Trait.
This commit is contained in:
parent
fb5264c479
commit
332a439b7a
@ -21,6 +21,7 @@ use tokio_core::reactor::Core;
|
|||||||
use hammond_data::FeedBuilder;
|
use hammond_data::FeedBuilder;
|
||||||
use hammond_data::Source;
|
use hammond_data::Source;
|
||||||
use hammond_data::database::truncate_db;
|
use hammond_data::database::truncate_db;
|
||||||
|
use hammond_data::pipeline;
|
||||||
// use hammond_data::errors::*;
|
// use hammond_data::errors::*;
|
||||||
|
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
@ -67,7 +68,7 @@ fn bench_pipeline(c: &mut Criterion) {
|
|||||||
c.bench_function("pipline", |b| {
|
c.bench_function("pipline", |b| {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
let sources = hammond_data::dbqueries::get_sources().unwrap();
|
let sources = hammond_data::dbqueries::get_sources().unwrap();
|
||||||
hammond_data::pipeline::pipeline(sources, true).unwrap();
|
pipeline::run(sources, true).unwrap();
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
truncate_db().unwrap();
|
truncate_db().unwrap();
|
||||||
|
|||||||
@ -65,6 +65,7 @@ mod schema;
|
|||||||
|
|
||||||
pub use feed::{Feed, FeedBuilder};
|
pub use feed::{Feed, FeedBuilder};
|
||||||
pub use models::{Episode, EpisodeWidgetQuery, Podcast, PodcastCoverQuery, Source};
|
pub use models::{Episode, EpisodeWidgetQuery, Podcast, PodcastCoverQuery, Source};
|
||||||
|
pub use models::Save;
|
||||||
|
|
||||||
/// [XDG Base Direcotory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) Paths.
|
/// [XDG Base Direcotory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) Paths.
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
|
|||||||
@ -5,7 +5,7 @@ use diesel::prelude::*;
|
|||||||
|
|
||||||
use database::connection;
|
use database::connection;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use models::Podcast;
|
use models::{Podcast, Save};
|
||||||
use schema::episode;
|
use schema::episode;
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
|
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
|
||||||
@ -31,6 +31,16 @@ pub struct Episode {
|
|||||||
podcast_id: i32,
|
podcast_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Save<Episode> for Episode {
|
||||||
|
/// Helper method to easily save/"sync" current state of self to the Database.
|
||||||
|
fn save(&self) -> Result<Episode> {
|
||||||
|
let db = connection();
|
||||||
|
let tempdb = db.get()?;
|
||||||
|
|
||||||
|
self.save_changes::<Episode>(&*tempdb).map_err(From::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Episode {
|
impl Episode {
|
||||||
/// Get the value of the sqlite's `ROW_ID`
|
/// Get the value of the sqlite's `ROW_ID`
|
||||||
pub fn rowid(&self) -> i32 {
|
pub fn rowid(&self) -> i32 {
|
||||||
@ -175,14 +185,6 @@ impl Episode {
|
|||||||
self.set_played(Some(epoch));
|
self.set_played(Some(epoch));
|
||||||
self.save().map(|_| ())
|
self.save().map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper method to easily save/"sync" current state of self to the Database.
|
|
||||||
pub fn save(&self) -> Result<Episode> {
|
|
||||||
let db = connection();
|
|
||||||
let tempdb = db.get()?;
|
|
||||||
|
|
||||||
self.save_changes::<Episode>(&*tempdb).map_err(From::from)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, AsChangeset, PartialEq)]
|
#[derive(Queryable, AsChangeset, PartialEq)]
|
||||||
@ -221,6 +223,21 @@ impl From<Episode> for EpisodeWidgetQuery {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Save<usize> for EpisodeWidgetQuery {
|
||||||
|
/// Helper method to easily save/"sync" current state of self to the Database.
|
||||||
|
fn save(&self) -> Result<usize> {
|
||||||
|
use schema::episode::dsl::*;
|
||||||
|
|
||||||
|
let db = connection();
|
||||||
|
let tempdb = db.get()?;
|
||||||
|
|
||||||
|
diesel::update(episode.filter(rowid.eq(self.rowid)))
|
||||||
|
.set(self)
|
||||||
|
.execute(&*tempdb)
|
||||||
|
.map_err(From::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl EpisodeWidgetQuery {
|
impl EpisodeWidgetQuery {
|
||||||
/// Get the value of the sqlite's `ROW_ID`
|
/// Get the value of the sqlite's `ROW_ID`
|
||||||
pub fn rowid(&self) -> i32 {
|
pub fn rowid(&self) -> i32 {
|
||||||
@ -330,19 +347,6 @@ impl EpisodeWidgetQuery {
|
|||||||
self.set_played(Some(epoch));
|
self.set_played(Some(epoch));
|
||||||
self.save().map(|_| ())
|
self.save().map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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()?;
|
|
||||||
|
|
||||||
diesel::update(episode.filter(rowid.eq(self.rowid)))
|
|
||||||
.set(self)
|
|
||||||
.execute(&*tempdb)
|
|
||||||
.map_err(From::from)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, AsChangeset, PartialEq)]
|
#[derive(Queryable, AsChangeset, PartialEq)]
|
||||||
@ -357,6 +361,21 @@ pub struct EpisodeCleanerQuery {
|
|||||||
played: Option<i32>,
|
played: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Save<usize> for EpisodeCleanerQuery {
|
||||||
|
/// Helper method to easily save/"sync" current state of self to the Database.
|
||||||
|
fn save(&self) -> Result<usize> {
|
||||||
|
use schema::episode::dsl::*;
|
||||||
|
|
||||||
|
let db = connection();
|
||||||
|
let tempdb = db.get()?;
|
||||||
|
|
||||||
|
diesel::update(episode.filter(rowid.eq(self.rowid)))
|
||||||
|
.set(self)
|
||||||
|
.execute(&*tempdb)
|
||||||
|
.map_err(From::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Episode> for EpisodeCleanerQuery {
|
impl From<Episode> for EpisodeCleanerQuery {
|
||||||
fn from(e: Episode) -> EpisodeCleanerQuery {
|
fn from(e: Episode) -> EpisodeCleanerQuery {
|
||||||
EpisodeCleanerQuery {
|
EpisodeCleanerQuery {
|
||||||
@ -397,19 +416,6 @@ impl EpisodeCleanerQuery {
|
|||||||
pub fn set_played(&mut self, value: Option<i32>) {
|
pub fn set_played(&mut self, value: Option<i32>) {
|
||||||
self.played = value;
|
self.played = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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()?;
|
|
||||||
|
|
||||||
diesel::update(episode.filter(rowid.eq(self.rowid())))
|
|
||||||
.set(self)
|
|
||||||
.execute(&*tempdb)
|
|
||||||
.map_err(From::from)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, AsChangeset, PartialEq)]
|
#[derive(Queryable, AsChangeset, PartialEq)]
|
||||||
|
|||||||
@ -43,3 +43,9 @@ pub trait Update {
|
|||||||
pub trait Index: Insert + Update {
|
pub trait Index: Insert + Update {
|
||||||
fn index(&self) -> Result<()>;
|
fn index(&self) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// FIXME: DOCS
|
||||||
|
pub trait Save<T> {
|
||||||
|
/// Helper method to easily save/"sync" current state of a diesel model to the Database.
|
||||||
|
fn save(&self) -> Result<T>;
|
||||||
|
}
|
||||||
|
|||||||
@ -157,7 +157,7 @@ mod tests {
|
|||||||
use rss::Channel;
|
use rss::Channel;
|
||||||
|
|
||||||
use database::truncate_db;
|
use database::truncate_db;
|
||||||
use models::NewPodcastBuilder;
|
use models::{NewPodcastBuilder, Save};
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
|
|||||||
@ -2,7 +2,7 @@ use diesel::SaveChangesDsl;
|
|||||||
|
|
||||||
use database::connection;
|
use database::connection;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use models::Source;
|
use models::{Save, Source};
|
||||||
use schema::podcast;
|
use schema::podcast;
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
|
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
|
||||||
@ -23,6 +23,16 @@ pub struct Podcast {
|
|||||||
source_id: i32,
|
source_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Save<Podcast> for Podcast {
|
||||||
|
/// Helper method to easily save/"sync" current state of self to the Database.
|
||||||
|
fn save(&self) -> Result<Podcast> {
|
||||||
|
let db = connection();
|
||||||
|
let tempdb = db.get()?;
|
||||||
|
|
||||||
|
self.save_changes::<Podcast>(&*tempdb).map_err(From::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Podcast {
|
impl Podcast {
|
||||||
/// Get the Feed `id`.
|
/// Get the Feed `id`.
|
||||||
pub fn id(&self) -> i32 {
|
pub fn id(&self) -> i32 {
|
||||||
@ -107,14 +117,6 @@ impl Podcast {
|
|||||||
pub fn source_id(&self) -> i32 {
|
pub fn source_id(&self) -> i32 {
|
||||||
self.source_id
|
self.source_id
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper method to easily save/"sync" current state of self to the Database.
|
|
||||||
pub fn save(&self) -> Result<Podcast> {
|
|
||||||
let db = connection();
|
|
||||||
let tempdb = db.get()?;
|
|
||||||
|
|
||||||
self.save_changes::<Podcast>(&*tempdb).map_err(From::from)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Debug, Clone)]
|
#[derive(Queryable, Debug, Clone)]
|
||||||
|
|||||||
@ -16,7 +16,7 @@ use futures_cpupool::CpuPool;
|
|||||||
use database::connection;
|
use database::connection;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use feed::{Feed, FeedBuilder};
|
use feed::{Feed, FeedBuilder};
|
||||||
use models::NewSource;
|
use models::{NewSource, Save};
|
||||||
use schema::source;
|
use schema::source;
|
||||||
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@ -33,6 +33,16 @@ pub struct Source {
|
|||||||
http_etag: Option<String>,
|
http_etag: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Save<Source> for Source {
|
||||||
|
/// Helper method to easily save/"sync" current state of self to the Database.
|
||||||
|
fn save(&self) -> Result<Source> {
|
||||||
|
let db = connection();
|
||||||
|
let con = db.get()?;
|
||||||
|
|
||||||
|
self.save_changes::<Source>(&con).map_err(From::from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Source {
|
impl Source {
|
||||||
/// Get the source `id` column.
|
/// Get the source `id` column.
|
||||||
pub fn id(&self) -> i32 {
|
pub fn id(&self) -> i32 {
|
||||||
@ -74,14 +84,6 @@ impl Source {
|
|||||||
self.http_etag = value.map(|x| x.to_string());
|
self.http_etag = value.map(|x| x.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper method to easily save/"sync" current state of self to the Database.
|
|
||||||
pub fn save(&self) -> Result<Source> {
|
|
||||||
let db = connection();
|
|
||||||
let con = db.get()?;
|
|
||||||
|
|
||||||
self.save_changes::<Source>(&con).map_err(From::from)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extract Etag and LastModifier from res, and update self and the
|
/// Extract Etag and LastModifier from res, and update self and the
|
||||||
/// corresponding db row.
|
/// corresponding db row.
|
||||||
fn update_etag(&mut self, res: &Response) -> Result<()> {
|
fn update_etag(&mut self, res: &Response) -> Result<()> {
|
||||||
|
|||||||
@ -8,7 +8,7 @@ use url::{Position, Url};
|
|||||||
|
|
||||||
use dbqueries;
|
use dbqueries;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use models::{EpisodeCleanerQuery, Podcast};
|
use models::{EpisodeCleanerQuery, Podcast, Save};
|
||||||
use xdg_dirs::DL_DIR;
|
use xdg_dirs::DL_DIR;
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|||||||
@ -11,7 +11,7 @@ use std::path::Path;
|
|||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery};
|
use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery, Save};
|
||||||
use hammond_data::xdg_dirs::HAMMOND_CACHE;
|
use hammond_data::xdg_dirs::HAMMOND_CACHE;
|
||||||
|
|
||||||
// TODO: Replace path that are of type &str with std::path.
|
// TODO: Replace path that are of type &str with std::path.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user