summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2017-07-09 02:16:57 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2017-07-09 02:16:57 -0400
commitda3cfff817b0eaead9be11d945e8f7914c8e801c (patch)
treedc7d6ba1292ffb44fe531451b9221768b64b2783
parentc6d6702cf838fa8d43a511d455f45a385b74e427 (diff)
add: Sqlite schema and test database.
-rw-r--r--src/data.rs71
-rw-r--r--src/lib.rs3
-rw-r--r--src/main.rs30
3 files changed, 83 insertions, 21 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(())
}
diff --git a/src/lib.rs b/src/lib.rs
index dd4cf29..6d73a2f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -7,6 +7,7 @@ extern crate handlebars_iron;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
+extern crate rusqlite;
pub mod server;
-mod data;
+pub mod data;
diff --git a/src/main.rs b/src/main.rs
index 2c7905f..68d83f7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,16 @@
+extern crate linoquotes_gamma;
+
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate clap;
-extern crate linoquotes_gamma;
+extern crate rusqlite;
use clap::{App, Arg};
fn main() {
+ env_logger::init().unwrap();
+
let matches = App::new("linoquotes")
.version("3.0.0")
.author("Kjetil Ørbekk")
@@ -15,6 +19,13 @@ fn main() {
.arg(Arg::with_name("port")
.short("p").long("port").takes_value(true)
.help("Port to serve on"))
+ .arg(Arg::with_name("db_file")
+ .long("db_file").takes_value(true)
+ .help("Path to sqlite database (use in-memory test \
+ database if unset)"))
+ .arg(Arg::with_name("use_test_db")
+ .long("use_test_db")
+ .help("Use in-memory test database"))
.get_matches();
let port = matches
@@ -23,7 +34,22 @@ fn main() {
.parse::<u16>()
.expect("port number");
- env_logger::init().unwrap();
+ let use_test_db = matches.is_present("use_test_db");
+ let db_file = matches.value_of("db_file");
+ let connection = match db_file {
+ None => {
+ assert!(use_test_db, "--db_file or --use_test_db must be set");
+ rusqlite::Connection::open_in_memory().unwrap()
+ },
+ Some(ref path) => rusqlite::Connection::open(path).unwrap()
+ };
+
+ linoquotes_gamma::data::init(&connection).unwrap();
+
+ if use_test_db {
+ linoquotes_gamma::data::populate_test_db(&connection).unwrap();
+ }
+
info!("Starting");
linoquotes_gamma::server::serve(port);
}