hammond-data: Factor out save method of diesel models into a Trait.

This commit is contained in:
Jordan Petridis 2018-01-28 22:09:06 +02:00
parent fb5264c479
commit 332a439b7a
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
9 changed files with 75 additions and 57 deletions

View File

@ -21,6 +21,7 @@ use tokio_core::reactor::Core;
use hammond_data::FeedBuilder;
use hammond_data::Source;
use hammond_data::database::truncate_db;
use hammond_data::pipeline;
// use hammond_data::errors::*;
use std::io::BufReader;
@ -67,7 +68,7 @@ fn bench_pipeline(c: &mut Criterion) {
c.bench_function("pipline", |b| {
b.iter(|| {
let sources = hammond_data::dbqueries::get_sources().unwrap();
hammond_data::pipeline::pipeline(sources, true).unwrap();
pipeline::run(sources, true).unwrap();
})
});
truncate_db().unwrap();

View File

@ -65,6 +65,7 @@ mod schema;
pub use feed::{Feed, FeedBuilder};
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.
#[allow(missing_debug_implementations)]

View File

@ -5,7 +5,7 @@ use diesel::prelude::*;
use database::connection;
use errors::*;
use models::Podcast;
use models::{Podcast, Save};
use schema::episode;
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
@ -31,6 +31,16 @@ pub struct Episode {
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 {
/// Get the value of the sqlite's `ROW_ID`
pub fn rowid(&self) -> i32 {
@ -175,14 +185,6 @@ impl Episode {
self.set_played(Some(epoch));
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)]
@ -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 {
/// Get the value of the sqlite's `ROW_ID`
pub fn rowid(&self) -> i32 {
@ -330,19 +347,6 @@ impl EpisodeWidgetQuery {
self.set_played(Some(epoch));
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)]
@ -357,6 +361,21 @@ pub struct EpisodeCleanerQuery {
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 {
fn from(e: Episode) -> EpisodeCleanerQuery {
EpisodeCleanerQuery {
@ -397,19 +416,6 @@ impl EpisodeCleanerQuery {
pub fn set_played(&mut self, value: Option<i32>) {
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)]

View File

@ -43,3 +43,9 @@ pub trait Update {
pub trait Index: Insert + Update {
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>;
}

View File

@ -157,7 +157,7 @@ mod tests {
use rss::Channel;
use database::truncate_db;
use models::NewPodcastBuilder;
use models::{NewPodcastBuilder, Save};
use std::fs::File;
use std::io::BufReader;

View File

@ -2,7 +2,7 @@ use diesel::SaveChangesDsl;
use database::connection;
use errors::*;
use models::Source;
use models::{Save, Source};
use schema::podcast;
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
@ -23,6 +23,16 @@ pub struct Podcast {
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 {
/// Get the Feed `id`.
pub fn id(&self) -> i32 {
@ -107,14 +117,6 @@ impl Podcast {
pub fn source_id(&self) -> i32 {
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)]

View File

@ -16,7 +16,7 @@ use futures_cpupool::CpuPool;
use database::connection;
use errors::*;
use feed::{Feed, FeedBuilder};
use models::NewSource;
use models::{NewSource, Save};
use schema::source;
use std::str::FromStr;
@ -33,6 +33,16 @@ pub struct Source {
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 {
/// Get the source `id` column.
pub fn id(&self) -> i32 {
@ -74,14 +84,6 @@ impl Source {
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
/// corresponding db row.
fn update_etag(&mut self, res: &Response) -> Result<()> {

View File

@ -8,7 +8,7 @@ use url::{Position, Url};
use dbqueries;
use errors::*;
use models::{EpisodeCleanerQuery, Podcast};
use models::{EpisodeCleanerQuery, Podcast, Save};
use xdg_dirs::DL_DIR;
use std::fs;

View File

@ -11,7 +11,7 @@ use std::path::Path;
use std::sync::{Arc, Mutex};
use errors::*;
use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery};
use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery, Save};
use hammond_data::xdg_dirs::HAMMOND_CACHE;
// TODO: Replace path that are of type &str with std::path.