From 5fdb7198bc73035cf5621ded8183000c3fcbbe29 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sun, 24 Dec 2017 02:00:00 +0100 Subject: Add 'remove' command to Factoids and make the Database trait public --- src/plugins/factoids/database.rs | 57 +++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 16 deletions(-) (limited to 'src/plugins/factoids/database.rs') diff --git a/src/plugins/factoids/database.rs b/src/plugins/factoids/database.rs index 522c9d0..1af586e 100644 --- a/src/plugins/factoids/database.rs +++ b/src/plugins/factoids/database.rs @@ -26,6 +26,8 @@ pub struct Factoid { pub created: NaiveDateTime, } +#[cfg(feature = "mysql")] +use self::mysql::factoids; #[cfg_attr(feature = "mysql", derive(Insertable))] #[cfg_attr(feature = "mysql", table_name="factoids")] pub struct NewFactoid<'a> { @@ -40,6 +42,7 @@ pub struct NewFactoid<'a> { pub trait Database: Send { fn insert(&mut self, factoid: &NewFactoid) -> DbResponse; fn get(&self, name: &str, idx: i32) -> Option; + fn delete(&mut self, name: &str, idx: i32) -> DbResponse; fn count(&self, name: &str) -> Result; } @@ -65,6 +68,13 @@ impl Database for HashMap<(String, i32), Factoid> { self.get(&(String::from(name), idx)).map(|f| f.clone()) } + fn delete(&mut self, name: &str, idx: i32) -> DbResponse { + match self.remove(&(String::from(name), idx)) { + Some(_) => DbResponse::Success, + None => DbResponse::Failed("Factoid not found"), + } + } + fn count(&self, name: &str) -> Result { Ok(self.iter() .filter(|&(&(ref n, _), _)| n == name) @@ -72,15 +82,18 @@ impl Database for HashMap<(String, i32), Factoid> { } } -// MySql +// Diesel automatically define the factoids module as public. +// For now this is how we keep it private. #[cfg(feature = "mysql")] -table! { - factoids (name, idx) { - name -> Varchar, - idx -> Integer, - content -> Text, - author -> Varchar, - created -> Timestamp, +mod mysql { + table! { + factoids (name, idx) { + name -> Varchar, + idx -> Integer, + content -> Text, + author -> Varchar, + created -> Timestamp, + } } } @@ -88,22 +101,34 @@ table! { impl Database for MysqlConnection { fn insert(&mut self, factoid: &NewFactoid) -> DbResponse { use diesel; - match diesel::insert_into(factoids::table) .values(factoid) .execute(self) { Ok(_) => DbResponse::Success, - Err(_) => DbResponse::Failed("Database error - possible duplicate"), + Err(e) => { + debug!("DB Insertion Error: {:?}", e); + DbResponse::Failed("Database error - possible duplicate") + } } } fn get(&self, name: &str, idx: i32) -> Option { - factoids::table - .find((name, idx)) - .limit(1) - .load::(self) - .ok() - .and_then(|v| v.into_iter().next()) + factoids::table.find((name, idx)).first(self).ok() + } + + fn delete(&mut self, name: &str, idx: i32) -> DbResponse { + use diesel; + use self::factoids::columns; + match diesel::delete(factoids::table + .filter(columns::name.eq(name)) + .filter(columns::idx.eq(idx))) + .execute(self) { + Ok(_) => DbResponse::Success, + Err(e) => { + debug!("DB Deletion Error: {:?}", e); + DbResponse::Failed("Failed to delete factoid") + } + } } fn count(&self, name: &str) -> Result { -- cgit v1.2.3-70-g09d2