Use source_id as the unique identifieble field for the podcast table.

This commit is contained in:
Jordan Petridis 2017-11-25 01:56:10 +02:00
parent 12ffe5c231
commit 62da8bbb52
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
6 changed files with 65 additions and 45 deletions

3
.gitignore vendored
View File

@ -7,3 +7,6 @@ Cargo.lock
_build _build
vendor/ vendor/
.flatpak-builder/ .flatpak-builder/
flatpak-repo/
Makefile
hammond-repo/

View File

@ -4,36 +4,36 @@
-- in order to change the db schema. -- in order to change the db schema.
CREATE TABLE `source` ( CREATE TABLE `source` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`uri` TEXT NOT NULL UNIQUE, `uri` TEXT NOT NULL UNIQUE,
`last_modified` TEXT, `last_modified` TEXT,
`http_etag` TEXT `http_etag` TEXT
); );
CREATE TABLE `episode` ( CREATE TABLE `episode` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`title` TEXT, `title` TEXT,
`uri` TEXT NOT NULL UNIQUE, `uri` TEXT NOT NULL UNIQUE,
`local_uri` TEXT, `local_uri` TEXT,
`description` TEXT, `description` TEXT,
`published_date` TEXT, `published_date` TEXT,
`epoch` INTEGER NOT NULL DEFAULT 0, `epoch` INTEGER NOT NULL DEFAULT 0,
`length` INTEGER, `length` INTEGER,
`guid` TEXT, `guid` TEXT,
`played` INTEGER, `played` INTEGER,
`favorite` INTEGER NOT NULL DEFAULT 0, `favorite` INTEGER NOT NULL DEFAULT 0,
`archive` INTEGER NOT NULL DEFAULT 0, `archive` INTEGER NOT NULL DEFAULT 0,
`podcast_id` INTEGER NOT NULL `podcast_id` INTEGER NOT NULL
); );
CREATE TABLE `podcast` ( CREATE TABLE `podcast` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`title` TEXT NOT NULL UNIQUE, `title` TEXT NOT NULL,
`link` TEXT NOT NULL, `link` TEXT NOT NULL,
`description` TEXT NOT NULL, `description` TEXT NOT NULL,
`image_uri` TEXT, `image_uri` TEXT,
`favorite` INTEGER NOT NULL DEFAULT 0, `favorite` INTEGER NOT NULL DEFAULT 0,
`archive` INTEGER NOT NULL DEFAULT 0, `archive` INTEGER NOT NULL DEFAULT 0,
`always_dl` INTEGER NOT NULL DEFAULT 0, `always_dl` INTEGER NOT NULL DEFAULT 0,
`source_id` INTEGER NOT NULL `source_id` INTEGER NOT NULL UNIQUE
); );

View File

@ -133,13 +133,23 @@ pub fn get_source_from_uri(uri_: &str) -> QueryResult<Source> {
source.filter(uri.eq(uri_)).get_result::<Source>(&*con) source.filter(uri.eq(uri_)).get_result::<Source>(&*con)
} }
pub fn get_podcast_from_title(title_: &str) -> QueryResult<Podcast> { // pub fn get_podcast_from_title(title_: &str) -> QueryResult<Podcast> {
// use schema::podcast::dsl::*;
// let db = connection();
// let con = db.get().unwrap();
// podcast
// .filter(title.eq(title_))
// .get_result::<Podcast>(&*con)
// }
pub fn get_podcast_from_source_id(sid: i32) -> QueryResult<Podcast> {
use schema::podcast::dsl::*; use schema::podcast::dsl::*;
let db = connection(); let db = connection();
let con = db.get().unwrap(); let con = db.get().unwrap();
podcast podcast
.filter(title.eq(title_)) .filter(source_id.eq(sid))
.get_result::<Podcast>(&*con) .get_result::<Podcast>(&*con)
} }

View File

@ -105,11 +105,11 @@ impl NewPodcast {
// Look out for when tryinto lands into stable. // Look out for when tryinto lands into stable.
pub fn into_podcast(self) -> Result<Podcast> { pub fn into_podcast(self) -> Result<Podcast> {
self.index()?; self.index()?;
Ok(dbqueries::get_podcast_from_title(&self.title)?) Ok(dbqueries::get_podcast_from_source_id(self.source_id)?)
} }
pub fn index(&self) -> QueryResult<()> { pub fn index(&self) -> QueryResult<()> {
let pd = dbqueries::get_podcast_from_title(&self.title); let pd = dbqueries::get_podcast_from_source_id(self.source_id);
let db = connection(); let db = connection();
let con = db.get().unwrap(); let con = db.get().unwrap();
@ -119,7 +119,9 @@ impl NewPodcast {
error!("NPD sid: {}, PD sid: {}", self.source_id, foo.source_id()); error!("NPD sid: {}, PD sid: {}", self.source_id, foo.source_id());
}; };
if foo.link() != self.link { if (foo.link() != self.link) || (foo.title() != self.title)
|| (foo.image_uri() != self.image_uri.as_ref().map(|x| x.as_str()))
{
dbqueries::replace_podcast(&con, self)?; dbqueries::replace_podcast(&con, self)?;
} }
} }

View File

@ -194,6 +194,7 @@ mod tests {
use super::*; use super::*;
use hammond_data::Source; use hammond_data::Source;
use hammond_data::dbqueries; use hammond_data::dbqueries;
use diesel::Identifiable;
use std::fs; use std::fs;
@ -208,14 +209,16 @@ mod tests {
fn test_cache_image() { fn test_cache_image() {
let url = "http://www.newrustacean.com/feed.xml"; let url = "http://www.newrustacean.com/feed.xml";
Source::from_url(url) // Create and index a source
.unwrap() let source = Source::from_url(url).unwrap();
.into_feed() // Copy it's id
.unwrap() let sid = source.id().clone();
.index()
.unwrap();
let pd = dbqueries::get_podcast_from_title("New Rustacean").unwrap(); // Convert Source it into a Feed and index it
source.into_feed().unwrap().index().unwrap();
// Get the Podcast
let pd = dbqueries::get_podcast_from_source_id(sid).unwrap();
let img_path = cache_image(&pd); let img_path = cache_image(&pd);
let foo_ = format!( let foo_ = format!(

View File

@ -119,21 +119,23 @@ pub fn update_podcast_widget(stack: &gtk::Stack, pd: &Podcast) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use hammond_data::Source; use hammond_data::Source;
use diesel::Identifiable;
use super::*; use super::*;
#[test] #[test]
fn test_get_pixbuf_from_path() { fn test_get_pixbuf_from_path() {
let url = "http://www.newrustacean.com/feed.xml"; let url = "http://www.newrustacean.com/feed.xml";
Source::from_url(url) // Create and index a source
.unwrap() let source = Source::from_url(url).unwrap();
.into_feed() // Copy it's id
.unwrap() let sid = source.id().clone();
.index()
.unwrap();
let pd = dbqueries::get_podcast_from_title("New Rustacean").unwrap(); // Convert Source it into a Feed and index it
source.into_feed().unwrap().index().unwrap();
// Get the Podcast
let pd = dbqueries::get_podcast_from_source_id(sid).unwrap();
let pxbuf = get_pixbuf_from_path(&pd); let pxbuf = get_pixbuf_from_path(&pd);
assert!(pxbuf.is_some()); assert!(pxbuf.is_some());
} }