From f177dcb7b98300634fdef408150a83e9f476d66d Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Wed, 12 Jul 2017 06:35:41 -0400 Subject: add: Voting function --- src/data.rs | 11 +++++++++++ src/data/templates/quotes.hbs | 15 +++++++++++++-- src/data/templates/vote.hbs | 3 +++ src/main.rs | 7 ++++++- src/server.rs | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/data/templates/vote.hbs diff --git a/src/data.rs b/src/data.rs index ba821cc..c12db94 100644 --- a/src/data.rs +++ b/src/data.rs @@ -68,6 +68,17 @@ pub fn new_quote(c: &Connection, date: &str, author: &str, content: &str) -> Res Ok(()) } +pub fn new_vote(c: &Connection, quote_id: i64, score: i32) -> Result<()>{ + c.execute( + r#" + INSERT INTO votes (quote_id, score) VALUES + (?1, ?2) + "#, + &["e_id, &score])?; + info!("New vote: quote_id({}) score({})", quote_id, score); + Ok(()) +} + pub fn approve_quote(c: &Connection, quote_id: i64) -> Result<()> { c.execute( r#" diff --git a/src/data/templates/quotes.hbs b/src/data/templates/quotes.hbs index 0e38d22..45fa711 100644 --- a/src/data/templates/quotes.hbs +++ b/src/data/templates/quotes.hbs @@ -31,6 +31,18 @@ -moz-user-select: text; } + @@ -59,8 +71,7 @@ Dato: {{date}}, Score: 2 3 4 - 5 - , + 5,



diff --git a/src/data/templates/vote.hbs b/src/data/templates/vote.hbs new file mode 100644 index 0000000..135fa2f --- /dev/null +++ b/src/data/templates/vote.hbs @@ -0,0 +1,3 @@ +{{#with quote}} +{{score}} (fra {{votes}}) +{{/with}} diff --git a/src/main.rs b/src/main.rs index 49f110e..9d9ac05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,10 +58,15 @@ fn main() { }; linoquotes_gamma::data::init(&state.connection).unwrap(); - if use_test_db { linoquotes_gamma::data::populate_test_db(&state.connection).unwrap(); } + linoquotes_gamma::data::new_quote(&state.connection, "2017-07-10", "orbekk", "another test") + .unwrap(); + let qid = state.connection.last_insert_rowid(); + info!("Last inserted quote: {}", qid); + linoquotes_gamma::data::approve_quote(&state.connection, qid).unwrap(); + linoquotes_gamma::server::serve(state, port); } diff --git a/src/server.rs b/src/server.rs index e20bb96..4c8240b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -48,6 +48,10 @@ fn make_renderer() -> HandlebarsEngine { "approve".to_string(), include_str!("data/templates/approve.hbs").to_string(), ); + templates.insert( + "vote".to_string(), + include_str!("data/templates/vote.hbs").to_string(), + ); e.add(Box::new(MemorySource(templates))); if let Err(r) = e.reload() { @@ -149,6 +153,38 @@ fn approve(r: &mut Request) -> IronResult { ))) } +pub fn vote(r: &mut Request) -> IronResult { + let quote_id = get_param(r, "id").and_then(|id| { + id.parse::().map_err(|e| { + From::from(LinoError::BadRequest(format!("id: {}", e))) + }) + })?; + let vote = get_param(r, "vote").and_then(|id| { + id.parse::().map_err(|e| { + From::from(LinoError::BadRequest(format!("vote: {}", e))) + }) + })?; + if vote < 1 || vote > 5 { + return Err(From::from(LinoError::BadRequest("bad vote".to_string()))); + } + + let quote = { + let mu = r.get::>().unwrap(); + let state = mu.lock().unwrap(); + data::new_vote(&state.connection, quote_id, vote); + data::get_quote(&state.connection, quote_id)? + }; + + let mut result = Map::new(); + result.insert("quote".to_string(), to_json("e)); + + Ok(Response::with(( + status::Ok, + Header(ContentType::html()), + Template::new("vote", result), + ))) +} + pub fn serve(state: State, port: u16) { let router = router!( @@ -158,6 +194,7 @@ pub fn serve(state: State, port: u16) { quotes_jsp: get "/quotes.jsp" => quotes, view_quote: get "/view_quote" => quotes, approve: get "/approve.jsp" => approve, + vote: get "/vote" => vote, ); let mut chain = Chain::new(router); chain.link_after(make_renderer()); -- cgit v1.2.3