From ec7e01f576ef6a07301424aeebc20d554fe7202e Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Sun, 9 Jul 2017 12:07:41 -0400 Subject: add: Quote ordering --- src/data.rs | 22 ++++++++++++++-------- src/data/templates/quotes.hbs | 2 +- src/server.rs | 21 +++++++++++++-------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/data.rs b/src/data.rs index 574612a..396e932 100644 --- a/src/data.rs +++ b/src/data.rs @@ -7,7 +7,8 @@ pub struct Quote { date: String, author: String, score: String, - votes: u32, + points: i32, + votes: i32, content: String, } @@ -48,14 +49,14 @@ pub fn populate_test_db(c: &Connection) -> Result<()> { (1, 2), (1, 3), (1, 1), - (2, 3), - (2, 4); + (2, 1), + (2, 2); "#, )); Ok(()) } -fn internal_get_quotes(c: &Connection, id: Option) -> Result> { +fn internal_get_quotes(c: &Connection, id: Option, ordering: &str) -> Result> { let mut stmt = c.prepare( r#" SELECT q.id, q.timestamp, q.author, q.content, @@ -75,6 +76,7 @@ fn internal_get_quotes(c: &Connection, id: Option) -> Result> { author: row.get(2), content: row.get(3), score: format!("{:.2}", row.get::(4) / row.get::(5)), + points: row.get(4), votes: row.get(5), } })?; @@ -83,17 +85,21 @@ fn internal_get_quotes(c: &Connection, id: Option) -> Result> { .collect::>>(); // For some reason, the ordering from sqlite doesn't work. result.map(|mut queries| { - queries.sort_by_key(|q| -q.id); + if ordering == "score" { + queries.sort_by_key(|q| -(q.points / q.votes)); + } else { + queries.sort_by_key(|q| -q.id); + } queries }) } -pub fn get_quotes(c: &Connection) -> Result> { - internal_get_quotes(c, None) +pub fn get_quotes(c: &Connection, ordering: &str) -> Result> { + internal_get_quotes(c, None, ordering) } pub fn get_quote(c: &Connection, id: i64) -> Result { - let quotes = internal_get_quotes(c, Some(id))?; + let quotes = internal_get_quotes(c, Some(id), "")?; quotes.into_iter().next().ok_or(LinoError::NotFound( format!("quote with id {}", id), )) diff --git a/src/data/templates/quotes.hbs b/src/data/templates/quotes.hbs index 68f0a5b..6e9ca48 100644 --- a/src/data/templates/quotes.hbs +++ b/src/data/templates/quotes.hbs @@ -44,7 +44,7 @@ Quotes fra #linux.no på freenode Klikk her for å legge til en quote - Sortér etter quote-dato | score | timestamp + Sortér etter dato | score
diff --git a/src/server.rs b/src/server.rs index 2b0cff8..12ffedb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -42,20 +42,25 @@ fn make_renderer() -> HandlebarsEngine { fn quotes(r: &mut Request) -> IronResult { let mut result = Map::new(); - let quote_id = { - let map = itry!(r.get_ref::()); - match map.get("id") { - Some(¶ms::Value::String(ref id)) => Some(itry!(id.parse::())), - _ => None, - } + let (quote_id, ordering) = { + let map = itry!(r.get_ref::()); + let quote_id = match map.get("id") { + Some(¶ms::Value::String(ref id)) => Some(itry!(id.parse::())), + _ => None, + }; + let ordering = match map.get("order") { + Some(¶ms::Value::String(ref ordering)) => ordering.clone(), + _ => "".to_string(), + }; + (quote_id, ordering) }; let quotes = { let mu = r.get::>().unwrap(); let state = mu.lock().unwrap(); match quote_id { - Some(id) => vec!(data::get_quote(&state.connection, id)?), - None => data::get_quotes(&state.connection)?, + Some(id) => vec![data::get_quote(&state.connection, id)?], + None => data::get_quotes(&state.connection, &ordering)?, } }; result.insert("quotes".to_string(), to_json("es)); -- cgit v1.2.3