summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2017-07-09 12:07:41 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2017-07-09 12:07:41 -0400
commitec7e01f576ef6a07301424aeebc20d554fe7202e (patch)
tree79a29713f7f18a2bb9a2cb69b5af5a6fb934387a
parent444faa5936d9729a5ef270ce676bfdaf5210bb47 (diff)
add: Quote ordering
-rw-r--r--src/data.rs22
-rw-r--r--src/data/templates/quotes.hbs2
-rw-r--r--src/server.rs21
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<i64>) -> Result<Vec<Quote>> {
+fn internal_get_quotes(c: &Connection, id: Option<i64>, ordering: &str) -> Result<Vec<Quote>> {
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<i64>) -> Result<Vec<Quote>> {
author: row.get(2),
content: row.get(3),
score: format!("{:.2}", row.get::<i32, f64>(4) / row.get::<i32, f64>(5)),
+ points: row.get(4),
votes: row.get(5),
}
})?;
@@ -83,17 +85,21 @@ fn internal_get_quotes(c: &Connection, id: Option<i64>) -> Result<Vec<Quote>> {
.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);
+ 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<Vec<Quote>> {
- internal_get_quotes(c, None)
+pub fn get_quotes(c: &Connection, ordering: &str) -> Result<Vec<Quote>> {
+ internal_get_quotes(c, None, ordering)
}
pub fn get_quote(c: &Connection, id: i64) -> Result<Quote> {
- 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 <a href="add.jsp">her</a> for å legge til en quote
- Sortér etter <a href="quotes.jsp?order=date">quote-dato</a> | <a href="quotes.jsp?order=score">score</a> | <a href="quotes.jsp?order=timestamp">timestamp</a>
+ Sortér etter <a href="quotes.jsp?order=date">dato</a> | <a href="quotes.jsp?order=score">score</a>
</pre>
<hr>
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<Response> {
let mut result = Map::new();
- let quote_id = {
- let map = itry!(r.get_ref::<params::Params>());
- match map.get("id") {
- Some(&params::Value::String(ref id)) => Some(itry!(id.parse::<i64>())),
- _ => None,
- }
+ let (quote_id, ordering) = {
+ let map = itry!(r.get_ref::<params::Params>());
+ let quote_id = match map.get("id") {
+ Some(&params::Value::String(ref id)) => Some(itry!(id.parse::<i64>())),
+ _ => None,
+ };
+ let ordering = match map.get("order") {
+ Some(&params::Value::String(ref ordering)) => ordering.clone(),
+ _ => "".to_string(),
+ };
+ (quote_id, ordering)
};
let quotes = {
let mu = r.get::<Write<State>>().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(&quotes));