104 lines
2.2 KiB
Rust
104 lines
2.2 KiB
Rust
//!
|
|
//!
|
|
use super::{models, schema::api_kgon_synchronizations};
|
|
use diesel::prelude::*;
|
|
use diesel::result::Error;
|
|
|
|
///
|
|
pub struct Repository {}
|
|
|
|
impl std::fmt::Debug for Repository {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
f.debug_struct("Repository of api_kgon_synchronizations")
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
impl Default for Repository {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl Repository {
|
|
///
|
|
pub fn new() -> Repository {
|
|
Repository {}
|
|
}
|
|
|
|
///
|
|
pub fn insert(
|
|
&self,
|
|
conn: &diesel::PgConnection,
|
|
new_member: &models::NewSynchronization,
|
|
) -> Result<models::Synchronization, Error> {
|
|
let inserted = diesel::insert_into(api_kgon_synchronizations::table)
|
|
.values(new_member)
|
|
.get_result::<models::Synchronization>(conn)?;
|
|
|
|
Ok(inserted)
|
|
}
|
|
|
|
///
|
|
pub fn select(
|
|
&self,
|
|
conn: &diesel::PgConnection,
|
|
id: i32,
|
|
) -> Result<Option<models::Synchronization>, Error> {
|
|
match api_kgon_synchronizations::table
|
|
.find(id)
|
|
.first::<models::Synchronization>(conn)
|
|
{
|
|
Ok(m) => Ok(Some(m)),
|
|
Err(e) => match e {
|
|
diesel::result::Error::NotFound => Ok(None),
|
|
_ => Err(e),
|
|
},
|
|
}
|
|
}
|
|
|
|
///
|
|
pub fn select_by_item(
|
|
&self,
|
|
conn: &diesel::PgConnection,
|
|
item: String,
|
|
) -> Result<Option<models::Synchronization>, Error> {
|
|
use api_kgon_synchronizations::dsl;
|
|
|
|
match api_kgon_synchronizations::table
|
|
.filter(dsl::item.eq(item))
|
|
.first::<models::Synchronization>(conn)
|
|
{
|
|
Ok(m) => Ok(Some(m)),
|
|
Err(e) => match e {
|
|
diesel::result::Error::NotFound => Ok(None),
|
|
_ => Err(e),
|
|
},
|
|
}
|
|
}
|
|
|
|
///
|
|
pub fn update(
|
|
&self,
|
|
conn: &diesel::PgConnection,
|
|
id: i32,
|
|
modify: &models::ModifySynchronization,
|
|
) -> Result<u64, Error> {
|
|
use api_kgon_synchronizations::dsl;
|
|
|
|
diesel::update(dsl::api_kgon_synchronizations.filter(dsl::id.eq(id)))
|
|
.set(modify)
|
|
.execute(conn)
|
|
.map(|c| c as u64)
|
|
}
|
|
|
|
///
|
|
pub fn delete(&self, conn: &diesel::PgConnection, id: i32) -> Result<u64, Error> {
|
|
use api_kgon_synchronizations::dsl;
|
|
|
|
diesel::delete(api_kgon_synchronizations::table.filter(dsl::id.eq(id)))
|
|
.execute(conn)
|
|
.map(|c| c as u64)
|
|
}
|
|
}
|