|
|
|
@@ -6,17 +6,16 @@ use diesel::prelude::*;
|
|
|
|
|
use diesel;
|
|
|
|
|
use diesel::dsl::exists;
|
|
|
|
|
use diesel::select;
|
|
|
|
|
use failure::Error;
|
|
|
|
|
|
|
|
|
|
use database::connection;
|
|
|
|
|
use errors::DataError;
|
|
|
|
|
use models::*;
|
|
|
|
|
// use errors::DatabaseError;
|
|
|
|
|
|
|
|
|
|
// Feel free to open a Merge request that manually replaces Result<T> if you feel bored.
|
|
|
|
|
use std::result;
|
|
|
|
|
type Result<T> = result::Result<T, Error>;
|
|
|
|
|
type DatabaseResult<T> = result::Result<T, DataError>;
|
|
|
|
|
|
|
|
|
|
pub fn get_sources() -> Result<Vec<Source>> {
|
|
|
|
|
pub fn get_sources() -> DatabaseResult<Vec<Source>> {
|
|
|
|
|
use schema::source::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -27,7 +26,7 @@ pub fn get_sources() -> Result<Vec<Source>> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_podcasts() -> Result<Vec<Podcast>> {
|
|
|
|
|
pub fn get_podcasts() -> DatabaseResult<Vec<Podcast>> {
|
|
|
|
|
use schema::podcast::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -38,7 +37,7 @@ pub fn get_podcasts() -> Result<Vec<Podcast>> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_episodes() -> Result<Vec<Episode>> {
|
|
|
|
|
pub fn get_episodes() -> DatabaseResult<Vec<Episode>> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -49,7 +48,7 @@ pub fn get_episodes() -> Result<Vec<Episode>> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn get_downloaded_episodes() -> Result<Vec<EpisodeCleanerQuery>> {
|
|
|
|
|
pub(crate) fn get_downloaded_episodes() -> DatabaseResult<Vec<EpisodeCleanerQuery>> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -61,7 +60,7 @@ pub(crate) fn get_downloaded_episodes() -> Result<Vec<EpisodeCleanerQuery>> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pub(crate) fn get_played_episodes() -> Result<Vec<Episode>> {
|
|
|
|
|
// pub(crate) fn get_played_episodes() -> DatabaseResult<Vec<Episode>> {
|
|
|
|
|
// use schema::episode::dsl::*;
|
|
|
|
|
|
|
|
|
|
// let db = connection();
|
|
|
|
@@ -72,7 +71,7 @@ pub(crate) fn get_downloaded_episodes() -> Result<Vec<EpisodeCleanerQuery>> {
|
|
|
|
|
// .map_err(From::from)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
pub(crate) fn get_played_cleaner_episodes() -> Result<Vec<EpisodeCleanerQuery>> {
|
|
|
|
|
pub(crate) fn get_played_cleaner_episodes() -> DatabaseResult<Vec<EpisodeCleanerQuery>> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -84,7 +83,7 @@ pub(crate) fn get_played_cleaner_episodes() -> Result<Vec<EpisodeCleanerQuery>>
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_episode_from_rowid(ep_id: i32) -> Result<Episode> {
|
|
|
|
|
pub fn get_episode_from_rowid(ep_id: i32) -> DatabaseResult<Episode> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -95,7 +94,7 @@ pub fn get_episode_from_rowid(ep_id: i32) -> Result<Episode> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_episode_local_uri_from_id(ep_id: i32) -> Result<Option<String>> {
|
|
|
|
|
pub fn get_episode_local_uri_from_id(ep_id: i32) -> DatabaseResult<Option<String>> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -107,7 +106,7 @@ pub fn get_episode_local_uri_from_id(ep_id: i32) -> Result<Option<String>> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_episodes_widgets_with_limit(limit: u32) -> Result<Vec<EpisodeWidgetQuery>> {
|
|
|
|
|
pub fn get_episodes_widgets_with_limit(limit: u32) -> DatabaseResult<Vec<EpisodeWidgetQuery>> {
|
|
|
|
|
use schema::episode;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -130,7 +129,7 @@ pub fn get_episodes_widgets_with_limit(limit: u32) -> Result<Vec<EpisodeWidgetQu
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_podcast_from_id(pid: i32) -> Result<Podcast> {
|
|
|
|
|
pub fn get_podcast_from_id(pid: i32) -> DatabaseResult<Podcast> {
|
|
|
|
|
use schema::podcast::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -141,7 +140,7 @@ pub fn get_podcast_from_id(pid: i32) -> Result<Podcast> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_podcast_cover_from_id(pid: i32) -> Result<PodcastCoverQuery> {
|
|
|
|
|
pub fn get_podcast_cover_from_id(pid: i32) -> DatabaseResult<PodcastCoverQuery> {
|
|
|
|
|
use schema::podcast::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -153,7 +152,7 @@ pub fn get_podcast_cover_from_id(pid: i32) -> Result<PodcastCoverQuery> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_pd_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
|
|
|
|
|
pub fn get_pd_episodes(parent: &Podcast) -> DatabaseResult<Vec<Episode>> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -164,7 +163,7 @@ pub fn get_pd_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_pd_episodeswidgets(parent: &Podcast) -> Result<Vec<EpisodeWidgetQuery>> {
|
|
|
|
|
pub fn get_pd_episodeswidgets(parent: &Podcast) -> DatabaseResult<Vec<EpisodeWidgetQuery>> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -177,7 +176,7 @@ pub fn get_pd_episodeswidgets(parent: &Podcast) -> Result<Vec<EpisodeWidgetQuery
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_pd_unplayed_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
|
|
|
|
|
pub fn get_pd_unplayed_episodes(parent: &Podcast) -> DatabaseResult<Vec<Episode>> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -189,8 +188,8 @@ pub fn get_pd_unplayed_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pub(crate) fn get_pd_episodes_limit(parent: &Podcast, limit: u32) -> Result<Vec<Episode>> {
|
|
|
|
|
// use schema::episode::dsl::*;
|
|
|
|
|
// pub(crate) fn get_pd_episodes_limit(parent: &Podcast, limit: u32) ->
|
|
|
|
|
// DatabaseResult<Vec<Episode>> { use schema::episode::dsl::*;
|
|
|
|
|
|
|
|
|
|
// let db = connection();
|
|
|
|
|
// let con = db.get()?;
|
|
|
|
@@ -202,7 +201,7 @@ pub fn get_pd_unplayed_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
|
|
|
|
|
// .map_err(From::from)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
pub fn get_source_from_uri(uri_: &str) -> Result<Source> {
|
|
|
|
|
pub fn get_source_from_uri(uri_: &str) -> DatabaseResult<Source> {
|
|
|
|
|
use schema::source::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -213,7 +212,7 @@ pub fn get_source_from_uri(uri_: &str) -> Result<Source> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_source_from_id(id_: i32) -> Result<Source> {
|
|
|
|
|
pub fn get_source_from_id(id_: i32) -> DatabaseResult<Source> {
|
|
|
|
|
use schema::source::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -224,7 +223,7 @@ pub fn get_source_from_id(id_: i32) -> Result<Source> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_podcast_from_source_id(sid: i32) -> Result<Podcast> {
|
|
|
|
|
pub fn get_podcast_from_source_id(sid: i32) -> DatabaseResult<Podcast> {
|
|
|
|
|
use schema::podcast::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -235,7 +234,7 @@ pub fn get_podcast_from_source_id(sid: i32) -> Result<Podcast> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_episode_from_pk(title_: &str, pid: i32) -> Result<Episode> {
|
|
|
|
|
pub fn get_episode_from_pk(title_: &str, pid: i32) -> DatabaseResult<Episode> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -247,7 +246,10 @@ pub fn get_episode_from_pk(title_: &str, pid: i32) -> Result<Episode> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn get_episode_minimal_from_pk(title_: &str, pid: i32) -> Result<EpisodeMinimal> {
|
|
|
|
|
pub(crate) fn get_episode_minimal_from_pk(
|
|
|
|
|
title_: &str,
|
|
|
|
|
pid: i32,
|
|
|
|
|
) -> DatabaseResult<EpisodeMinimal> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -260,11 +262,11 @@ pub(crate) fn get_episode_minimal_from_pk(title_: &str, pid: i32) -> Result<Epis
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn remove_feed(pd: &Podcast) -> Result<()> {
|
|
|
|
|
pub(crate) fn remove_feed(pd: &Podcast) -> DatabaseResult<()> {
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
|
|
|
|
|
|
con.transaction(|| -> Result<()> {
|
|
|
|
|
con.transaction(|| {
|
|
|
|
|
delete_source(&con, pd.source_id())?;
|
|
|
|
|
delete_podcast(&con, pd.id())?;
|
|
|
|
|
delete_podcast_episodes(&con, pd.id())?;
|
|
|
|
@@ -291,7 +293,7 @@ fn delete_podcast_episodes(con: &SqliteConnection, parent_id: i32) -> QueryResul
|
|
|
|
|
diesel::delete(episode.filter(podcast_id.eq(parent_id))).execute(con)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn source_exists(url: &str) -> Result<bool> {
|
|
|
|
|
pub fn source_exists(url: &str) -> DatabaseResult<bool> {
|
|
|
|
|
use schema::source::dsl::*;
|
|
|
|
|
|
|
|
|
|
let db = connection();
|
|
|
|
@@ -302,7 +304,7 @@ pub fn source_exists(url: &str) -> Result<bool> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn podcast_exists(source_id_: i32) -> Result<bool> {
|
|
|
|
|
pub(crate) fn podcast_exists(source_id_: i32) -> DatabaseResult<bool> {
|
|
|
|
|
use schema::podcast::dsl::*;
|
|
|
|
|
|
|
|
|
|
let db = connection();
|
|
|
|
@@ -314,7 +316,7 @@ pub(crate) fn podcast_exists(source_id_: i32) -> Result<bool> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
|
|
|
|
pub(crate) fn episode_exists(title_: &str, podcast_id_: i32) -> Result<bool> {
|
|
|
|
|
pub(crate) fn episode_exists(title_: &str, podcast_id_: i32) -> DatabaseResult<bool> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
|
|
|
|
|
let db = connection();
|
|
|
|
@@ -325,7 +327,7 @@ pub(crate) fn episode_exists(title_: &str, podcast_id_: i32) -> Result<bool> {
|
|
|
|
|
.map_err(From::from)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn index_new_episodes(eps: &[NewEpisode]) -> Result<()> {
|
|
|
|
|
pub(crate) fn index_new_episodes(eps: &[NewEpisode]) -> DatabaseResult<()> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
@@ -337,13 +339,13 @@ pub(crate) fn index_new_episodes(eps: &[NewEpisode]) -> Result<()> {
|
|
|
|
|
.map(|_| ())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn update_none_to_played_now(parent: &Podcast) -> Result<usize> {
|
|
|
|
|
pub fn update_none_to_played_now(parent: &Podcast) -> DatabaseResult<usize> {
|
|
|
|
|
use schema::episode::dsl::*;
|
|
|
|
|
let db = connection();
|
|
|
|
|
let con = db.get()?;
|
|
|
|
|
|
|
|
|
|
let epoch_now = Utc::now().timestamp() as i32;
|
|
|
|
|
con.transaction(|| -> Result<usize> {
|
|
|
|
|
con.transaction(|| {
|
|
|
|
|
diesel::update(Episode::belonging_to(parent).filter(played.is_null()))
|
|
|
|
|
.set(played.eq(Some(epoch_now)))
|
|
|
|
|
.execute(&con)
|
|
|
|
|