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 } pub fn make_data() -> Map { 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: " hvor er jantho?".to_owned(), } ))); data } type Result = std::result::Result>; 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(()) }