diff options
| author | Jokler <jokler.contact@gmail.com> | 2017-12-24 02:00:00 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2017-12-24 02:00:00 +0100 |
| commit | 5fdb7198bc73035cf5621ded8183000c3fcbbe29 (patch) | |
| tree | 1979b49951737c521573949f5b16a7963a8ee2d2 /src/plugins/factoids/database.rs | |
| parent | 4b5693d3c6781a5ca4ef32f43f3994a65020c933 (diff) | |
| download | frippy-5fdb7198bc73035cf5621ded8183000c3fcbbe29.tar.gz frippy-5fdb7198bc73035cf5621ded8183000c3fcbbe29.zip | |
Add 'remove' command to Factoids and make the Database trait public
Diffstat (limited to 'src/plugins/factoids/database.rs')
| -rw-r--r-- | src/plugins/factoids/database.rs | 57 |
1 files changed, 41 insertions, 16 deletions
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<Factoid>; + fn delete(&mut self, name: &str, idx: i32) -> DbResponse; fn count(&self, name: &str) -> Result<i32, &'static str>; } @@ -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<i32, &'static str> { 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<Factoid> { - factoids::table - .find((name, idx)) - .limit(1) - .load::<Factoid>(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<i32, &'static str> { |
