Moved insert functions into an Insert trait and implemented it for each NewModel.
This commit is contained in:
parent
ce3a76aee1
commit
39dff5e27a
@ -206,24 +206,6 @@ pub fn update_none_to_played_now(parent: &Podcast) -> Result<usize> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_new_source(con: &SqliteConnection, s: &NewSource) -> QueryResult<usize> {
|
|
||||||
use schema::source::dsl::*;
|
|
||||||
|
|
||||||
diesel::insert_into(source).values(s).execute(&*con)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn insert_new_podcast(con: &SqliteConnection, pd: &NewPodcast) -> QueryResult<usize> {
|
|
||||||
use schema::podcast::dsl::*;
|
|
||||||
|
|
||||||
diesel::insert_into(podcast).values(pd).execute(&*con)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn insert_new_episode(con: &SqliteConnection, ep: &NewEpisode) -> QueryResult<usize> {
|
|
||||||
use schema::episode::dsl::*;
|
|
||||||
|
|
||||||
diesel::insert_into(episode).values(ep).execute(&*con)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_podcast(con: &SqliteConnection, pd_id: i32, pd: &NewPodcast) -> QueryResult<usize> {
|
pub fn update_podcast(con: &SqliteConnection, pd_id: i32, pd: &NewPodcast) -> QueryResult<usize> {
|
||||||
use schema::podcast::dsl::*;
|
use schema::podcast::dsl::*;
|
||||||
|
|
||||||
|
|||||||
@ -7,8 +7,13 @@ use utils::url_cleaner;
|
|||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
use dbqueries;
|
use dbqueries;
|
||||||
|
use diesel;
|
||||||
use database::connection;
|
use database::connection;
|
||||||
|
|
||||||
|
trait Insert {
|
||||||
|
fn insert(&self, &SqliteConnection) -> QueryResult<usize>;
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable)]
|
||||||
#[table_name = "source"]
|
#[table_name = "source"]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -18,6 +23,13 @@ pub struct NewSource {
|
|||||||
http_etag: Option<String>,
|
http_etag: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Insert for NewSource {
|
||||||
|
fn insert(&self, con: &SqliteConnection) -> QueryResult<usize> {
|
||||||
|
use schema::source::dsl::*;
|
||||||
|
diesel::insert_into(source).values(self).execute(&*con)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl NewSource {
|
impl NewSource {
|
||||||
pub fn new_with_uri(uri: &str) -> NewSource {
|
pub fn new_with_uri(uri: &str) -> NewSource {
|
||||||
let uri = url_cleaner(uri);
|
let uri = url_cleaner(uri);
|
||||||
@ -34,7 +46,7 @@ impl NewSource {
|
|||||||
|
|
||||||
// Throw away the result like `insert or ignore`
|
// Throw away the result like `insert or ignore`
|
||||||
// Diesel deos not support `insert or ignore` yet.
|
// Diesel deos not support `insert or ignore` yet.
|
||||||
let _ = dbqueries::insert_new_source(&con, self);
|
let _ = self.insert(&con);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,6 +71,13 @@ pub struct NewEpisode {
|
|||||||
pub podcast_id: i32,
|
pub podcast_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Insert for NewEpisode {
|
||||||
|
fn insert(&self, con: &SqliteConnection) -> QueryResult<usize> {
|
||||||
|
use schema::episode::dsl::*;
|
||||||
|
diesel::insert_into(episode).values(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.
|
||||||
@ -84,7 +103,7 @@ impl NewEpisode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
dbqueries::insert_new_episode(con, self)?;
|
self.insert(con)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -102,6 +121,13 @@ pub struct NewPodcast {
|
|||||||
pub source_id: i32,
|
pub source_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Insert for NewPodcast {
|
||||||
|
fn insert(&self, con: &SqliteConnection) -> QueryResult<usize> {
|
||||||
|
use schema::podcast::dsl::*;
|
||||||
|
diesel::insert_into(podcast).values(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> {
|
||||||
@ -127,7 +153,7 @@ impl NewPodcast {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
dbqueries::insert_new_podcast(&con, self)?;
|
self.insert(&con)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user