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/lib.rs | 5 ---- src/plugins/factoids/database.rs | 57 +++++++++++++++++++++++++++++----------- src/plugins/factoids/mod.rs | 20 +++++++++++++- src/plugins/mod.rs | 1 + 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4407e6a..8b55dab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,11 +34,6 @@ #[cfg(feature = "mysql")] #[macro_use] extern crate diesel; -#[cfg(feature = "mysql")] -extern crate diesel_infer_schema; -#[cfg(feature = "mysql")] -#[macro_use] -extern crate diesel_migrations; #[macro_use] extern crate log; 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 { diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs index bb83700..fcbbb79 100644 --- a/src/plugins/factoids/mod.rs +++ b/src/plugins/factoids/mod.rs @@ -11,7 +11,7 @@ use time; use chrono::NaiveDateTime; use plugin::*; -mod database; +pub mod database; use self::database::{Database, DbResponse}; static LUA_SANDBOX: &'static str = include_str!("sandbox.lua"); @@ -63,6 +63,23 @@ impl Factoids { } } + fn remove(&self, server: &IrcServer, command: &mut PluginCommand) -> Result<(), IrcError> { + if command.tokens.len() < 1 { + return self.invalid_command(server, command); + } + + let name = command.tokens.remove(0); + let count = match try_lock!(self.factoids).count(&name) { + Ok(c) => c, + Err(e) => return server.send_notice(&command.source, e), + }; + + match try_lock!(self.factoids).delete(&name, count - 1) { + DbResponse::Success => server.send_notice(&command.source, "Successfully removed"), + DbResponse::Failed(e) => server.send_notice(&command.source, &e), + } + } + fn get(&self, server: &IrcServer, command: &PluginCommand) -> Result<(), IrcError> { let (name, idx) = match command.tokens.len() { @@ -257,6 +274,7 @@ impl Plugin for Factoids { let sub_command = command.tokens.remove(0); match sub_command.as_ref() { "add" => self.add(server, &mut command), + "remove" => self.remove(server, &mut command), "get" => self.get(server, &command), "info" => self.info(server, &command), "exec" => self.exec(server, command, true), diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index f860d88..2e85932 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -11,4 +11,5 @@ pub use self::url::Url; pub use self::emoji::Emoji; pub use self::currency::Currency; pub use self::factoids::Factoids; +pub use self::factoids::database; pub use self::keepnick::KeepNick; -- cgit v1.2.3-70-g09d2