summaryrefslogtreecommitdiff
path: root/src/data.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/data.rs')
-rw-r--r--src/data.rs71
1 files changed, 53 insertions, 18 deletions
diff --git a/src/data.rs b/src/data.rs
index ee39613..83f5360 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -1,27 +1,62 @@
use serde_json::{Value, Map};
use handlebars_iron::handlebars::to_json;
+use rusqlite::Connection;
+use std;
#[derive(Serialize, Debug)]
pub struct Quote {
- id: u64,
- author: String,
- date: String,
- score: String,
- votes: u32,
- content: String
+ id: u64,
+ author: String,
+ date: String,
+ score: String,
+ votes: u32,
+ content: String
}
pub fn make_data() -> Map<String, Value> {
- let mut data = Map::new();
- data.insert("quotes".to_string(), to_json(&vec!(
- &Quote {
- id: 1,
- author: "panda_man".to_owned(),
- date: "2017-07-01".to_owned(),
- score: format!("{:.2}", 450.0 / 96.0),
- votes: 99,
- content: "<orbekk> hvor er jantho?".to_owned(),
- }
- )));
- data
+ let mut data = Map::new();
+ data.insert("quotes".to_string(), to_json(&vec!(
+ &Quote {
+ id: 1,
+ author: "panda_man".to_owned(),
+ date: "2017-07-01".to_owned(),
+ score: format!("{:.2}", 450.0 / 96.0),
+ votes: 99,
+ content: "<orbekk> hvor er jantho?".to_owned(),
+ }
+ )));
+ data
+}
+
+type Result<T> = std::result::Result<T, Box<std::error::Error>>;
+pub fn init(c: &Connection) -> Result<()> {
+ info!("Initializing db");
+ try!(c.execute_batch(r#"
+ CREATE TABLE quotes (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ approved BOOL DEFAULT false,
+ timestamp DATETIME NOT NULL,
+ author TEXT NOT NULL,
+ content TEXT NOT NULL,
+ CONSTRAINT unique_content UNIQUE (timestamp, content)
+ );
+
+ CREATE TABLE votes (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ quote_id INTEGER NOT NULL,
+ score INTEGER NOT NULL,
+ FOREIGN KEY(quote_id) REFERENCES quotes(id)
+ );
+ "#));
+ Ok(())
+}
+
+pub fn populate_test_db(c: &Connection) -> Result<()> {
+ info!("Populating test db");
+ try!(c.execute_batch(r#"
+ INSERT INTO quotes (timestamp, author, content) VALUES
+ ('2017-07-09', 'orbekk', 'test quote'),
+ ('2017-07-09', 'orbekk', 'test quote2');
+ "#));
+ Ok(())
}