aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/quote/database.rs
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2019-12-15 02:16:36 +0100
committerJokler <jokler.contact@gmail.com>2019-12-15 02:16:36 +0100
commitb1648f896702b6d45fa1099eaf26ac51c5dc053f (patch)
tree2d8e1ddc19c9cc40030c0422db9a9ce5e8fa6cfb /src/plugins/quote/database.rs
parent2e2ffe53d19c03d5653b824dad4b63fe7aa87f3a (diff)
downloadfrippy-b1648f896702b6d45fa1099eaf26ac51c5dc053f.tar.gz
frippy-b1648f896702b6d45fa1099eaf26ac51c5dc053f.zip
Quote: Add search subcommand
Diffstat (limited to 'src/plugins/quote/database.rs')
-rw-r--r--src/plugins/quote/database.rs125
1 files changed, 112 insertions, 13 deletions
diff --git a/src/plugins/quote/database.rs b/src/plugins/quote/database.rs
index 3c904df..9e6bb66 100644
--- a/src/plugins/quote/database.rs
+++ b/src/plugins/quote/database.rs
@@ -45,6 +45,20 @@ pub trait Database: Send + Sync {
fn get_channel_quote(&self, channel: &str, idx: i32) -> Result<Quote, QuoteError>;
fn count_user_quotes(&self, quotee: &str, channel: &str) -> Result<i32, QuoteError>;
fn count_channel_quotes(&self, channel: &str) -> Result<i32, QuoteError>;
+ fn search_user_quote(
+ &self,
+ query: &str,
+ quotee: &str,
+ channel: &str,
+ offset: i32,
+ ) -> Result<Quote, QuoteError>;
+
+ fn search_channel_quote(
+ &self,
+ query: &str,
+ channel: &str,
+ offset: i32,
+ ) -> Result<Quote, QuoteError>;
}
// HashMap
@@ -70,34 +84,79 @@ impl<S: ::std::hash::BuildHasher + Send + Sync> Database
}
fn get_user_quote(&self, quotee: &str, channel: &str, idx: i32) -> Result<Quote, QuoteError> {
- Ok(self
+ let quote = self
.get(&(quotee.to_owned(), channel.to_owned(), idx))
.cloned()
- .ok_or(ErrorKind::NotFound)?)
+ .ok_or(ErrorKind::NotFound)?;
+
+ Ok(quote)
}
fn get_channel_quote(&self, channel: &str, idx: i32) -> Result<Quote, QuoteError> {
- Ok(self
+ let quote = self
.iter()
.filter(|&(&(_, ref c, _), _)| c == channel)
.nth(idx as usize - 1)
.ok_or(ErrorKind::NotFound)?
.1
- .clone())
+ .clone();
+
+ Ok(quote)
}
fn count_user_quotes(&self, quotee: &str, channel: &str) -> Result<i32, QuoteError> {
- Ok(self
+ let count = self
.iter()
- .filter(|&(&(ref n, ref c, _), _)| n == quotee && c == channel)
- .count() as i32)
+ .filter(|&(&(ref q, ref c, _), _)| q == quotee && c == channel)
+ .count();
+
+ Ok(count as i32)
}
fn count_channel_quotes(&self, channel: &str) -> Result<i32, QuoteError> {
- Ok(self
+ let count = self
.iter()
.filter(|&(&(_, ref c, _), _)| c == channel)
- .count() as i32)
+ .count();
+
+ Ok(count as i32)
+ }
+
+ fn search_user_quote(
+ &self,
+ query: &str,
+ quotee: &str,
+ channel: &str,
+ offset: i32,
+ ) -> Result<Quote, QuoteError> {
+ let quote = self
+ .iter()
+ .filter(|&(&(ref q, ref c, _), _)| q == quotee && c == channel)
+ .filter(|&(&(_, _, _), q)| q.content.to_lowercase().contains(&query.to_lowercase()))
+ .nth(offset as usize)
+ .ok_or(ErrorKind::NotFound)?
+ .1
+ .clone();
+
+ Ok(quote)
+ }
+
+ fn search_channel_quote(
+ &self,
+ query: &str,
+ channel: &str,
+ offset: i32,
+ ) -> Result<Quote, QuoteError> {
+ let quote = self
+ .iter()
+ .filter(|&(&(_, ref c, _), _)| c == channel)
+ .filter(|&(&(_, _, _), q)| q.content.contains(query))
+ .nth(offset as usize)
+ .ok_or(ErrorKind::NotFound)?
+ .1
+ .clone();
+
+ Ok(quote)
}
}
@@ -134,19 +193,23 @@ impl Database for Arc<Pool<ConnectionManager<MysqlConnection>>> {
fn get_user_quote(&self, quotee: &str, channel: &str, idx: i32) -> Result<Quote, QuoteError> {
let conn = &*self.get().context(ErrorKind::NoConnection)?;
- Ok(quotes::table
+ let quote = quotes::table
.find((quotee, channel, idx))
.first(conn)
- .context(ErrorKind::MysqlError)?)
+ .context(ErrorKind::MysqlError)?;
+
+ Ok(quote)
}
fn get_channel_quote(&self, channel: &str, idx: i32) -> Result<Quote, QuoteError> {
let conn = &*self.get().context(ErrorKind::NoConnection)?;
- Ok(quotes::table
+ let quote = quotes::table
.filter(quotes::columns::channel.eq(channel))
.offset(idx as i64 - 1)
.first(conn)
- .context(ErrorKind::MysqlError)?)
+ .context(ErrorKind::MysqlError)?;
+
+ Ok(quote)
}
fn count_user_quotes(&self, quotee: &str, channel: &str) -> Result<i32, QuoteError> {
@@ -177,4 +240,40 @@ impl Database for Arc<Pool<ConnectionManager<MysqlConnection>>> {
Err(e) => Err(e).context(ErrorKind::MysqlError)?,
}
}
+
+ fn search_user_quote(
+ &self,
+ query: &str,
+ quotee: &str,
+ channel: &str,
+ offset: i32,
+ ) -> Result<Quote, QuoteError> {
+ let conn = &*self.get().context(ErrorKind::NoConnection)?;
+ let quote = quotes::table
+ .filter(quotes::columns::channel.eq(channel))
+ .filter(quotes::columns::quotee.eq(quotee))
+ .filter(quotes::columns::content.like(&format!("%{}%", query)))
+ .offset(offset as i64)
+ .first(conn)
+ .context(ErrorKind::MysqlError)?;
+
+ Ok(quote)
+ }
+
+ fn search_channel_quote(
+ &self,
+ query: &str,
+ channel: &str,
+ offset: i32,
+ ) -> Result<Quote, QuoteError> {
+ let conn = &*self.get().context(ErrorKind::NoConnection)?;
+ let quote = quotes::table
+ .filter(quotes::columns::channel.eq(channel))
+ .filter(quotes::columns::content.like(&format!("%{}%", query)))
+ .offset(offset as i64)
+ .first(conn)
+ .context(ErrorKind::MysqlError)?;
+
+ Ok(quote)
+ }
}