diff options
author | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2017-07-09 05:42:37 -0400 |
---|---|---|
committer | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2017-07-09 05:42:37 -0400 |
commit | a3db23d802017762fdb77fbb294e57720088576c (patch) | |
tree | 79124f58ef318a1cfa77f4d7a73ce001f51bda1e /src/data.rs | |
parent | 432d3fce601e9ddc2060519053791f832cb98492 (diff) |
fix: Vote counting and sql queries
Diffstat (limited to 'src/data.rs')
-rw-r--r-- | src/data.rs | 35 |
1 files changed, 11 insertions, 24 deletions
diff --git a/src/data.rs b/src/data.rs index 2149a06..b5199e2 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,5 +1,3 @@ -use serde_json::{Value, Map}; -use handlebars_iron::handlebars::to_json; use rusqlite::Connection; use error::Result; @@ -13,24 +11,6 @@ pub struct Quote { 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 -} - pub fn init(c: &Connection) -> Result<()> { info!("Initializing db"); try!(c.execute_batch( @@ -69,7 +49,7 @@ pub fn populate_test_db(c: &Connection) -> Result<()> { (1, 3), (1, 1), (2, 3), - (1, 4); + (2, 4); "#, )); Ok(()) @@ -81,9 +61,10 @@ pub fn get_quotes(c: &Connection) -> Result<Vec<Quote>> { SELECT q.id, q.timestamp, q.author, q.content, sum(v.score), count(v.score) FROM quotes q - JOIN votes v + JOIN votes v ON (q.id = v.quote_id) WHERE q.approved GROUP BY 1, 2, 3, 4; + ORDER BY q.id DESC; "#, )?; @@ -97,6 +78,12 @@ pub fn get_quotes(c: &Connection) -> Result<Vec<Quote>> { votes: row.get(5), } })?; - let result = rows.map(|r| r.map_err(|e| From::from(e))).collect(); - result + + let result = rows.map(|r| r.map_err(|e| From::from(e))) + .collect::<Result<Vec<Quote>>>(); + // For some reason, the ordering from sqlite doesn't work. + result.map(|mut queries| { + queries.sort_by_key(|q| -q.id); + queries + }) } |