Moved update funtions into a Trait and impl for NewModels.

This commit is contained in:
Jordan Petridis 2017-11-26 00:56:38 +02:00
parent 39dff5e27a
commit 6336f7cac1
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 27 additions and 19 deletions

View File

@ -1,6 +1,6 @@
use diesel::prelude::*; use diesel::prelude::*;
use diesel; use diesel;
use models::{Episode, NewEpisode, NewPodcast, NewSource, Podcast, Source}; use models::{Episode, Podcast, Source};
use chrono::prelude::*; use chrono::prelude::*;
use errors::*; use errors::*;
@ -205,19 +205,3 @@ pub fn update_none_to_played_now(parent: &Podcast) -> Result<usize> {
.execute(&*con)?) .execute(&*con)?)
}) })
} }
pub fn update_podcast(con: &SqliteConnection, pd_id: i32, pd: &NewPodcast) -> QueryResult<usize> {
use schema::podcast::dsl::*;
diesel::update(podcast.filter(id.eq(pd_id)))
.set(pd)
.execute(&*con)
}
pub fn update_episode(con: &SqliteConnection, ep_id: i32, ep: &NewEpisode) -> QueryResult<usize> {
use schema::episode::dsl::*;
diesel::update(episode.filter(id.eq(ep_id)))
.set(ep)
.execute(&*con)
}

View File

@ -14,6 +14,10 @@ trait Insert {
fn insert(&self, &SqliteConnection) -> QueryResult<usize>; fn insert(&self, &SqliteConnection) -> QueryResult<usize>;
} }
trait Update {
fn update(&self, &SqliteConnection, i32) -> QueryResult<usize>;
}
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "source"] #[table_name = "source"]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -78,6 +82,16 @@ impl Insert for NewEpisode {
} }
} }
impl Update for NewEpisode {
fn update(&self, con: &SqliteConnection, episode_id: i32) -> QueryResult<usize> {
use schema::episode::dsl::*;
diesel::update(episode.filter(id.eq(episode_id)))
.set(self)
.execute(&*con)
}
}
impl NewEpisode { impl NewEpisode {
// TODO: Currently using diesel from master git. // TODO: Currently using diesel from master git.
// Watch out for v0.99.0 beta and change the toml. // Watch out for v0.99.0 beta and change the toml.
@ -99,7 +113,7 @@ impl NewEpisode {
if foo.title() != self.title.as_ref().map(|x| x.as_str()) if foo.title() != self.title.as_ref().map(|x| x.as_str())
|| foo.published_date() != self.published_date.as_ref().map(|x| x.as_str()) || foo.published_date() != self.published_date.as_ref().map(|x| x.as_str())
{ {
dbqueries::update_episode(con, *foo.id(), self)?; self.update(con, *foo.id())?;
} }
} }
Err(_) => { Err(_) => {
@ -128,6 +142,16 @@ impl Insert for NewPodcast {
} }
} }
impl Update for NewPodcast {
fn update(&self, con: &SqliteConnection, podcast_id: i32) -> QueryResult<usize> {
use schema::podcast::dsl::*;
diesel::update(podcast.filter(id.eq(podcast_id)))
.set(self)
.execute(&*con)
}
}
impl NewPodcast { 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> {
@ -149,7 +173,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()))
{ {
dbqueries::update_podcast(&con, *foo.id(), self)?; self.update(&con, *foo.id())?;
} }
} }
Err(_) => { Err(_) => {