EpisodesView: Custom Diesel model not really necessary.

This commit is contained in:
Jordan Petridis
2017-12-19 20:45:40 +02:00
parent 895591f628
commit 914cad72f5
5 changed files with 17 additions and 67 deletions
+4 -9
View File
@@ -2,7 +2,7 @@
use diesel::prelude::*;
use diesel;
use models::queryables::{Episode, EpisodeViewWidgetQuery, EpisodeWidgetQuery, Podcast, Source};
use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, Source};
use chrono::prelude::*;
use errors::*;
@@ -84,17 +84,13 @@ pub fn get_episodes_with_limit(limit: u32) -> Result<Vec<Episode>> {
.load::<Episode>(&*con)?)
}
pub fn get_episodes_view_widgets_with_limit(limit: u32) -> Result<Vec<EpisodeViewWidgetQuery>> {
use schema::{episode, podcast};
joinable!(episode -> podcast (podcast_id));
allow_tables_to_appear_in_same_query!(episode, podcast);
pub fn get_episodes_widgets_with_limit(limit: u32) -> Result<Vec<EpisodeWidgetQuery>> {
use schema::episode;
let db = connection();
let con = db.get()?;
Ok(episode::table
.left_join(podcast::table)
.select((
episode::rowid,
episode::title,
@@ -104,11 +100,10 @@ pub fn get_episodes_view_widgets_with_limit(limit: u32) -> Result<Vec<EpisodeVie
episode::length,
episode::played,
episode::podcast_id,
(podcast::image_uri).nullable(),
))
.order(episode::epoch.desc())
.limit(i64::from(limit))
.load::<EpisodeViewWidgetQuery>(&*con)?)
.load::<EpisodeWidgetQuery>(&*con)?)
}
pub fn get_podcast_from_id(pid: i32) -> Result<Podcast> {
+1 -1
View File
@@ -61,7 +61,7 @@ pub(crate) mod models;
mod parser;
mod schema;
pub use models::queryables::{Episode, EpisodeViewWidgetQuery, EpisodeWidgetQuery, Podcast, Source};
pub use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, Source};
/// [XDG Base Direcotory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) Paths.
#[allow(missing_debug_implementations)]
-47
View File
@@ -320,53 +320,6 @@ impl EpisodeWidgetQuery {
}
}
impl From<EpisodeViewWidgetQuery> for EpisodeWidgetQuery {
fn from(view: EpisodeViewWidgetQuery) -> EpisodeWidgetQuery {
EpisodeWidgetQuery {
rowid: view.rowid,
title: view.title,
uri: view.uri,
local_uri: view.local_uri,
epoch: view.epoch,
length: view.length,
played: view.played,
// favorite: view.favorite,
// archive: view.archive,
podcast_id: view.podcast_id,
}
}
}
#[derive(Queryable, Debug, Clone)]
/// Diesel Model to be used for constructing `EpisodeWidgets`.
pub struct EpisodeViewWidgetQuery {
rowid: i32,
title: String,
uri: Option<String>,
local_uri: Option<String>,
epoch: i32,
length: Option<i32>,
played: Option<i32>,
// favorite: bool,
// archive: bool,
podcast_id: i32,
image_uri: Option<String>,
}
impl EpisodeViewWidgetQuery {
/// Get the `image_uri`.
///
/// Represents the uri(url usually) that the Feed cover image is located at.
pub fn image_uri(&self) -> Option<&str> {
self.image_uri.as_ref().map(|s| s.as_str())
}
/// `Podcast` table foreign key.
pub fn podcast_id(&self) -> i32 {
self.podcast_id
}
}
#[derive(Queryable, Identifiable, AsChangeset, Associations, PartialEq)]
#[belongs_to(Source, foreign_key = "source_id")]
#[changeset_options(treat_none_as_null = "true")]