aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/factoids/database.rs
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2017-12-16 18:13:02 +0100
committerJokler <jokler.contact@gmail.com>2017-12-24 01:24:58 +0100
commit4b5693d3c6781a5ca4ef32f43f3994a65020c933 (patch)
treefcea35b595542661d1f6713d860d647832aabd47 /src/plugins/factoids/database.rs
parent5928afc3bf83661cd3b11130a31a2d97ef135a9e (diff)
downloadfrippy-4b5693d3c6781a5ca4ef32f43f3994a65020c933.tar.gz
frippy-4b5693d3c6781a5ca4ef32f43f3994a65020c933.zip
Add more info to factoids and save old versions
Diffstat (limited to 'src/plugins/factoids/database.rs')
-rw-r--r--src/plugins/factoids/database.rs120
1 files changed, 83 insertions, 37 deletions
diff --git a/src/plugins/factoids/database.rs b/src/plugins/factoids/database.rs
index dbf136c..522c9d0 100644
--- a/src/plugins/factoids/database.rs
+++ b/src/plugins/factoids/database.rs
@@ -9,66 +9,112 @@ use diesel::prelude::*;
#[cfg(feature = "mysql")]
use diesel::mysql::MysqlConnection;
+use chrono::NaiveDateTime;
+
+pub enum DbResponse {
+ Success,
+ Failed(&'static str),
+}
+
+#[cfg_attr(feature = "mysql", derive(Queryable))]
+#[derive(Clone, Debug)]
+pub struct Factoid {
+ pub name: String,
+ pub idx: i32,
+ pub content: String,
+ pub author: String,
+ pub created: NaiveDateTime,
+}
+
+#[cfg_attr(feature = "mysql", derive(Insertable))]
+#[cfg_attr(feature = "mysql", table_name="factoids")]
+pub struct NewFactoid<'a> {
+ pub name: &'a str,
+ pub idx: i32,
+ pub content: &'a str,
+ pub author: &'a str,
+ pub created: NaiveDateTime,
+}
+
+
pub trait Database: Send {
- fn insert(&mut self, name: &str, content: &str) -> Option<()>;
- fn get(&self, name: &str) -> Option<String>;
+ fn insert(&mut self, factoid: &NewFactoid) -> DbResponse;
+ fn get(&self, name: &str, idx: i32) -> Option<Factoid>;
+ fn count(&self, name: &str) -> Result<i32, &'static str>;
}
-impl Database for HashMap<String, String> {
- fn insert(&mut self, name: &str, content: &str) -> Option<()> {
- self.insert(String::from(name), String::from(content)).map(|_| ())
+// HashMap
+impl Database for HashMap<(String, i32), Factoid> {
+ fn insert(&mut self, factoid: &NewFactoid) -> DbResponse {
+ let factoid = Factoid {
+ name: String::from(factoid.name),
+ idx: factoid.idx,
+ content: factoid.content.to_string(),
+ author: factoid.author.to_string(),
+ created: factoid.created,
+ };
+
+ let name = String::from(factoid.name.clone());
+ match self.insert((name, factoid.idx), factoid) {
+ None => DbResponse::Success,
+ Some(_) => DbResponse::Failed("Factoid was overwritten"),
+ }
}
- fn get(&self, name: &str) -> Option<String> {
- self.get(name).cloned()
+ fn get(&self, name: &str, idx: i32) -> Option<Factoid> {
+ self.get(&(String::from(name), idx)).map(|f| f.clone())
}
-}
-#[cfg(feature = "mysql")]
-#[derive(Queryable)]
-struct Factoid {
- pub name: String,
- pub content: String,
+ fn count(&self, name: &str) -> Result<i32, &'static str> {
+ Ok(self.iter()
+ .filter(|&(&(ref n, _), _)| n == name)
+ .count() as i32)
+ }
}
+// MySql
#[cfg(feature = "mysql")]
table! {
- factoids (name) {
+ factoids (name, idx) {
name -> Varchar,
- content -> Varchar,
+ idx -> Integer,
+ content -> Text,
+ author -> Varchar,
+ created -> Timestamp,
}
}
#[cfg(feature = "mysql")]
-#[derive(Insertable)]
-#[table_name="factoids"]
-struct NewFactoid<'a> {
- pub name: &'a str,
- pub content: &'a str,
-}
-
-
-#[cfg(feature = "mysql")]
impl Database for MysqlConnection {
- fn insert(&mut self, name: &str, content: &str) -> Option<()> {
- let factoid = NewFactoid {
- name: name,
- content: content,
- };
+ fn insert(&mut self, factoid: &NewFactoid) -> DbResponse {
+ use diesel;
- ::diesel::insert(&factoid)
- .into(factoids::table)
- .execute(self)
- .ok()
- .map(|_| ())
+ match diesel::insert_into(factoids::table)
+ .values(factoid)
+ .execute(self) {
+ Ok(_) => DbResponse::Success,
+ Err(_) => DbResponse::Failed("Database error - possible duplicate"),
+ }
}
- fn get(&self, name: &str) -> Option<String> {
+ fn get(&self, name: &str, idx: i32) -> Option<Factoid> {
factoids::table
- .filter(factoids::columns::name.eq(name))
+ .find((name, idx))
.limit(1)
.load::<Factoid>(self)
.ok()
- .and_then(|v| v.first().map(|f| f.content.clone()))
+ .and_then(|v| v.into_iter().next())
+ }
+
+ fn count(&self, name: &str) -> Result<i32, &'static str> {
+ let count: Result<i64, _> = factoids::table
+ .filter(factoids::columns::name.eq(name))
+ .count()
+ .first(self);
+
+ match count {
+ Ok(c) => Ok(c as i32),
+ Err(_) => Err("Database Error"),
+ }
}
}