diff options
author | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2017-07-09 02:16:57 -0400 |
---|---|---|
committer | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2017-07-09 02:16:57 -0400 |
commit | da3cfff817b0eaead9be11d945e8f7914c8e801c (patch) | |
tree | dc7d6ba1292ffb44fe531451b9221768b64b2783 /src/data.rs | |
parent | c6d6702cf838fa8d43a511d455f45a385b74e427 (diff) |
add: Sqlite schema and test database.
Diffstat (limited to 'src/data.rs')
-rw-r--r-- | src/data.rs | 71 |
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(()) } |