Cleanup the id() method mess of the diesel models.

This commit is contained in:
Jordan Petridis 2018-01-13 07:46:56 +02:00
parent 6517956987
commit e4d77a6ba4
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
15 changed files with 21 additions and 31 deletions

2
Cargo.lock generated
View File

@ -559,7 +559,6 @@ dependencies = [
name = "hammond-downloader" name = "hammond-downloader"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"hammond-data 0.1.0", "hammond-data 0.1.0",
@ -575,7 +574,6 @@ name = "hammond-gtk"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dissolve 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "dissolve 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"gdk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "gdk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gdk-pixbuf 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "gdk-pixbuf 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -232,8 +232,8 @@ pub fn remove_feed(pd: &Podcast) -> Result<()> {
con.transaction(|| -> Result<()> { con.transaction(|| -> Result<()> {
delete_source(&con, pd.source_id())?; delete_source(&con, pd.source_id())?;
delete_podcast(&con, *pd.id())?; delete_podcast(&con, pd.id())?;
delete_podcast_episodes(&con, *pd.id())?; delete_podcast_episodes(&con, pd.id())?;
info!("Feed removed from the Database."); info!("Feed removed from the Database.");
Ok(()) Ok(())
}) })

View File

@ -4,7 +4,6 @@ use rayon::prelude::*;
use diesel::prelude::*; use diesel::prelude::*;
use rayon::iter::IntoParallelIterator; use rayon::iter::IntoParallelIterator;
use diesel::associations::Identifiable;
use rss; use rss;
use dbqueries; use dbqueries;
@ -75,7 +74,7 @@ impl Feed {
let items = self.channel.items(); let items = self.channel.items();
let new_episodes: Vec<_> = items let new_episodes: Vec<_> = items
.par_iter() .par_iter()
.filter_map(|item| parser::new_episode(item, *pd.id()).ok()) .filter_map(|item| parser::new_episode(item, pd.id()).ok())
.collect(); .collect();
new_episodes new_episodes
@ -203,7 +202,7 @@ mod tests {
let feed = fs::File::open(path).unwrap(); let feed = fs::File::open(path).unwrap();
// parse it into a channel // parse it into a channel
let chan = rss::Channel::read_from(BufReader::new(feed)).unwrap(); let chan = rss::Channel::read_from(BufReader::new(feed)).unwrap();
Feed::from_channel_source(chan, *s.id()) Feed::from_channel_source(chan, s.id())
}) })
.collect(); .collect();

View File

@ -113,7 +113,7 @@ impl NewPodcast {
if (foo.link() != self.link) || (foo.title() != self.title) if (foo.link() != self.link) || (foo.title() != self.title)
|| (foo.image_uri() != self.image_uri.as_ref().map(|x| x.as_str())) || (foo.image_uri() != self.image_uri.as_ref().map(|x| x.as_str()))
{ {
self.update(&con, *foo.id())?; self.update(&con, foo.id())?;
} }
} }
Err(_) => { Err(_) => {

View File

@ -451,6 +451,11 @@ pub struct Podcast {
} }
impl Podcast { impl Podcast {
/// Get the Feed `id`.
pub fn id(&self) -> i32 {
self.id
}
/// Get the Feed `title`. /// Get the Feed `title`.
pub fn title(&self) -> &str { pub fn title(&self) -> &str {
&self.title &self.title
@ -551,7 +556,7 @@ pub struct PodcastCoverQuery {
impl From<Podcast> for PodcastCoverQuery { impl From<Podcast> for PodcastCoverQuery {
fn from(p: Podcast) -> PodcastCoverQuery { fn from(p: Podcast) -> PodcastCoverQuery {
PodcastCoverQuery { PodcastCoverQuery {
id: *p.id(), id: p.id(),
title: p.title, title: p.title,
image_uri: p.image_uri, image_uri: p.image_uri,
} }
@ -592,6 +597,11 @@ pub struct Source {
} }
impl<'a> Source { impl<'a> Source {
/// Get the source `id` column.
pub fn id(&self) -> i32 {
self.id
}
/// Represents the location(usually url) of the Feed xml file. /// Represents the location(usually url) of the Feed xml file.
pub fn uri(&self) -> &str { pub fn uri(&self) -> &str {
&self.uri &self.uri
@ -739,7 +749,7 @@ impl<'a> Source {
client: &'a mut Client<HttpsConnector<HttpConnector>>, client: &'a mut Client<HttpsConnector<HttpConnector>>,
ignore_etags: bool, ignore_etags: bool,
) -> Box<Future<Item = Feed, Error = hyper::Error> + 'a> { ) -> Box<Future<Item = Feed, Error = hyper::Error> + 'a> {
let id = *self.id(); let id = self.id();
let feed = request_constructor(&self, client, ignore_etags) let feed = request_constructor(&self, client, ignore_etags)
.map(move |res| { .map(move |res| {
println!("Status: {}", res.status()); println!("Status: {}", res.status());

View File

@ -13,9 +13,5 @@ reqwest = "0.8.2"
tempdir = "0.3.5" tempdir = "0.3.5"
glob = "0.2.11" glob = "0.2.11"
[dependencies.diesel]
features = ["sqlite"]
version = "1.0.0"
[dependencies.hammond-data] [dependencies.hammond-data]
path = "../hammond-data" path = "../hammond-data"

View File

@ -219,7 +219,6 @@ mod tests {
use hammond_data::Source; use hammond_data::Source;
use hammond_data::feed::index; use hammond_data::feed::index;
use hammond_data::dbqueries; use hammond_data::dbqueries;
use diesel::associations::Identifiable;
#[test] #[test]
// This test inserts an rss feed to your `XDG_DATA/hammond/hammond.db` so we make it explicit // This test inserts an rss feed to your `XDG_DATA/hammond/hammond.db` so we make it explicit
@ -231,7 +230,7 @@ mod tests {
// Create and index a source // Create and index a source
let mut source = Source::from_url(url).unwrap(); let mut source = Source::from_url(url).unwrap();
// Copy it's id // Copy it's id
let sid = source.id().clone(); let sid = source.id();
// Convert Source it into a Feed and index it // Convert Source it into a Feed and index it
let feed = source.into_feed(true).unwrap(); let feed = source.into_feed(true).unwrap();

View File

@ -1,4 +1,3 @@
use diesel::result;
use reqwest; use reqwest;
use hammond_data; use hammond_data;
use std::io; use std::io;
@ -7,7 +6,6 @@ error_chain! {
foreign_links { foreign_links {
ReqError(reqwest::Error); ReqError(reqwest::Error);
IoError(io::Error); IoError(io::Error);
DieselResultError(result::Error);
DataError(hammond_data::errors::Error); DataError(hammond_data::errors::Error);
} }
} }

View File

@ -1,6 +1,5 @@
#![recursion_limit = "1024"] #![recursion_limit = "1024"]
extern crate diesel;
#[macro_use] #[macro_use]
extern crate error_chain; extern crate error_chain;
extern crate glob; extern crate glob;

View File

@ -21,10 +21,6 @@ rayon = "0.9.0"
regex = "0.2.3" regex = "0.2.3"
send-cell = "0.1.2" send-cell = "0.1.2"
[dependencies.diesel]
features = ["sqlite"]
version = "1.0.0"
[dependencies.gtk] [dependencies.gtk]
features = ["v3_22"] features = ["v3_22"]
version = "0.3.0" version = "0.3.0"

View File

@ -7,7 +7,6 @@ extern crate glib;
extern crate gtk; extern crate gtk;
extern crate chrono; extern crate chrono;
extern crate diesel;
extern crate dissolve; extern crate dissolve;
extern crate hammond_data; extern crate hammond_data;
extern crate hammond_downloader; extern crate hammond_downloader;

View File

@ -117,7 +117,6 @@ pub fn add(id: i32, directory: &str, sender: Sender<Action>) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use diesel::Identifiable;
use hammond_data::database; use hammond_data::database;
use hammond_data::utils::get_download_folder; use hammond_data::utils::get_download_folder;
@ -141,7 +140,7 @@ mod tests {
// Create and index a source // Create and index a source
let mut source = Source::from_url(url).unwrap(); let mut source = Source::from_url(url).unwrap();
// Copy it's id // Copy it's id
let sid = source.id().clone(); let sid = source.id();
// Convert Source it into a Feed and index it // Convert Source it into a Feed and index it
let feed = source.into_feed(true).unwrap(); let feed = source.into_feed(true).unwrap();
@ -152,7 +151,7 @@ mod tests {
// Get an episode // Get an episode
let episode: Episode = { let episode: Episode = {
let con = database::connection(); let con = database::connection();
dbqueries::get_episode_from_pk(&*con.get().unwrap(), "e000: Hello, world!", *pd.id()) dbqueries::get_episode_from_pk(&*con.get().unwrap(), "e000: Hello, world!", pd.id())
.unwrap() .unwrap()
}; };

View File

@ -70,7 +70,6 @@ mod tests {
use hammond_data::Source; use hammond_data::Source;
use hammond_data::feed::index; use hammond_data::feed::index;
use hammond_data::dbqueries; use hammond_data::dbqueries;
use diesel::associations::Identifiable;
use super::*; use super::*;
#[test] #[test]
@ -83,7 +82,7 @@ mod tests {
// Create and index a source // Create and index a source
let mut source = Source::from_url(url).unwrap(); let mut source = Source::from_url(url).unwrap();
// Copy it's id // Copy it's id
let sid = source.id().clone(); let sid = source.id();
// Convert Source it into a Feed and index it // Convert Source it into a Feed and index it
let feed = source.into_feed(true).unwrap(); let feed = source.into_feed(true).unwrap();

View File

@ -1,6 +1,5 @@
use gtk; use gtk;
use gtk::prelude::*; use gtk::prelude::*;
use diesel::associations::Identifiable;
use hammond_data::dbqueries; use hammond_data::dbqueries;
use hammond_data::Podcast; use hammond_data::Podcast;

View File

@ -1,6 +1,5 @@
use gtk::prelude::*; use gtk::prelude::*;
use gtk; use gtk;
use diesel::Identifiable;
use open; use open;
use dissolve; use dissolve;