diff --git a/src/main.rs b/src/main.rs index be3204a..640a3bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use axum::extract::{Path, State}; +use axum::http::header::SET_COOKIE; use axum::http::StatusCode; use axum::response::{AppendHeaders, Html, IntoResponse, Redirect, Response}; use axum::Form; @@ -6,11 +7,10 @@ use axum::{ routing::{get, post}, Json, Router, }; +use axum_extra::extract::CookieJar; use serde::{Deserialize, Serialize}; use std::net::SocketAddr; use std::sync::{Arc, Mutex}; -use axum::http::header::SET_COOKIE; -use axum_extra::extract::CookieJar; #[derive(Debug, Clone, Serialize, Deserialize)] struct Page { @@ -212,44 +212,44 @@ async fn view_current_poll(State(state): State) -> Response { .filter(|poll| poll.id == current_poll) .collect(); if !current_poll.is_empty() { - if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) { - let poll_config = current_poll.first().unwrap(); - let mut choices = vec![]; + let poll_config = current_poll.first().unwrap(); + let mut choices = vec![]; - let find_vote_choice = |id: i32| { + let find_vote_choice = |id: i32| { + if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) { for choice in &poll.choices { if choice.id == id { - return Some(choice); + return Some(choice.clone()); } } - - return None; - }; - - for choice in &poll_config.choices { - if let Some(vote_choice) = find_vote_choice(choice.id) { - choices.push(PollChoiceResponse { - id: choice.id, - name: choice.name.clone(), - url: choice.url.clone(), - votes: vote_choice.votes, - }); - } else { - choices.push(PollChoiceResponse { - id: choice.id, - name: choice.name.clone(), - url: choice.url.clone(), - votes: 0, - }); - } } - return Json(PollResponse { - title: current_poll.first().unwrap().title.clone(), - choices, - }) - .into_response(); + return None; + }; + + for choice in &poll_config.choices { + if let Some(vote_choice) = find_vote_choice(choice.id) { + choices.push(PollChoiceResponse { + id: choice.id, + name: choice.name.clone(), + url: choice.url.clone(), + votes: vote_choice.votes, + }); + } else { + choices.push(PollChoiceResponse { + id: choice.id, + name: choice.name.clone(), + url: choice.url.clone(), + votes: 0, + }); + } } + + return Json(PollResponse { + title: current_poll.first().unwrap().title.clone(), + choices, + }) + .into_response(); } } } @@ -263,7 +263,10 @@ struct SubmitForm { choice: i32, } -async fn vote_current_poll(State(state): State, Form(form): Form) -> Response { +async fn vote_current_poll( + State(state): State, + Form(form): Form, +) -> Response { let id = form.choice; tracing::info!("Submitting vote for choice {id}"); @@ -279,7 +282,10 @@ async fn vote_current_poll(State(state): State, Form(form): Form, jar: CookieJar) -> Html< .filter(|poll| poll.id == current_poll) .collect(); if !current_poll.is_empty() { - if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) { - let poll_config = current_poll.first().unwrap(); - let mut choices = vec![]; + let poll_config = current_poll.first().unwrap(); + let mut choices = vec![]; - let find_vote_choice = |id: i32| { + let find_vote_choice = |id: i32| { + if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) { for choice in &poll.choices { if choice.id == id { - return Some(choice); + return Some(choice.clone()); } } - - return None; - }; - - for choice in &poll_config.choices { - if let Some(vote_choice) = find_vote_choice(choice.id) { - choices.push(PollChoiceResponse { - id: choice.id, - name: choice.name.clone(), - url: choice.url.clone(), - votes: vote_choice.votes, - }); - } else { - choices.push(PollChoiceResponse { - id: choice.id, - name: choice.name.clone(), - url: choice.url.clone(), - votes: 0, - }); - } } - let mut html = String::new(); - html.push_str(&format!( - "

{}

", - current_poll.first().unwrap().title.clone() - )); + return None; + }; - let mut existing_choice = None; - for cookie in jar.iter() { - if cookie.name().contains("poll_") { - let id = cookie.name().replace("poll_", ""); - if let Ok(id) = id.parse::() { - if id == poll_config.id { - if let Ok(value) = cookie.value().parse::() { - existing_choice = Some(value); - } + for choice in &poll_config.choices { + if let Some(vote_choice) = find_vote_choice(choice.id) { + choices.push(PollChoiceResponse { + id: choice.id, + name: choice.name.clone(), + url: choice.url.clone(), + votes: vote_choice.votes, + }); + } else { + choices.push(PollChoiceResponse { + id: choice.id, + name: choice.name.clone(), + url: choice.url.clone(), + votes: 0, + }); + } + } + + let mut html = String::new(); + html.push_str(&format!( + "

{}

", + current_poll.first().unwrap().title.clone() + )); + + let mut existing_choice = None; + for cookie in jar.iter() { + if cookie.name().contains("poll_") { + let id = cookie.name().replace("poll_", ""); + if let Ok(id) = id.parse::() { + if id == poll_config.id { + if let Ok(value) = cookie.value().parse::() { + existing_choice = Some(value); } } } } - - html.push_str("
"); - html.push_str("
"); - - html.push_str("
"); - html.push_str("
"); - let inert = if existing_choice.is_some() { "inert" } else { "" }; - html.push_str(&format!("
", inert)); - for choice in choices { - let checked = if Some(choice.id) == existing_choice { "checked" } else { "" }; - html.push_str(&format!("", choice.id, choice.id, checked)); - html.push_str(&format!( - "", - choice.id, - choice.name.clone() - )); - } - html.push_str("
"); - html.push_str(&format!("", inert)); - html.push_str(""); - html.push_str("
"); - html.push_str("
"); - - return Html(html); } + + html.push_str("
"); + html.push_str("
"); + + html.push_str("
"); + html.push_str("
"); + let inert = if existing_choice.is_some() { + "inert" + } else { + "" + }; + html.push_str(&format!("
", inert)); + for choice in choices { + let checked = if Some(choice.id) == existing_choice { + "checked" + } else { + "" + }; + html.push_str(&format!("", choice.id, choice.id, checked)); + html.push_str(&format!( + "", + choice.id, + choice.name.clone() + )); + } + html.push_str("
"); + html.push_str(&format!("", inert)); + html.push_str( + "", + ); + html.push_str("
"); + html.push_str("
"); + + return Html(html); } } } @@ -396,82 +412,86 @@ async fn poll_frame_results(State(state): State, jar: CookieJar) -> Ht .filter(|poll| poll.id == current_poll) .collect(); if !current_poll.is_empty() { - if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) { - let poll_config = current_poll.first().unwrap(); - let mut choices = vec![]; + let poll_config = current_poll.first().unwrap(); + let mut choices = vec![]; - let find_vote_choice = |id: i32| { + let find_vote_choice = |id: i32| { + if let Some(poll) = state.get_poll_votes_by_poll_id(current_poll[0].id) { for choice in &poll.choices { if choice.id == id { - return Some(choice); + return Some(choice.clone()); } } - - return None; - }; - - for choice in &poll_config.choices { - if let Some(vote_choice) = find_vote_choice(choice.id) { - choices.push(PollChoiceResponse { - id: choice.id, - name: choice.name.clone(), - url: choice.url.clone(), - votes: vote_choice.votes, - }); - } else { - choices.push(PollChoiceResponse { - id: choice.id, - name: choice.name.clone(), - url: choice.url.clone(), - votes: 0, - }); - } } - let mut html = String::new(); - html.push_str(&format!( - "

{}

", - current_poll.first().unwrap().title.clone() - )); + return None; + }; - html.push_str("
"); - html.push_str("
"); + for choice in &poll_config.choices { + if let Some(vote_choice) = find_vote_choice(choice.id) { + choices.push(PollChoiceResponse { + id: choice.id, + name: choice.name.clone(), + url: choice.url.clone(), + votes: vote_choice.votes, + }); + } else { + choices.push(PollChoiceResponse { + id: choice.id, + name: choice.name.clone(), + url: choice.url.clone(), + votes: 0, + }); + } + } - let mut existing_choice = None; - for cookie in jar.iter() { - if cookie.name().contains("poll_") { - let id = cookie.name().replace("poll_", ""); - if let Ok(id) = id.parse::() { - if id == poll_config.id { - if let Ok(value) = cookie.value().parse::() { - existing_choice = Some(value); - } + let mut html = String::new(); + html.push_str(&format!( + "

{}

", + current_poll.first().unwrap().title.clone() + )); + + html.push_str("
"); + html.push_str("
"); + + let mut existing_choice = None; + for cookie in jar.iter() { + if cookie.name().contains("poll_") { + let id = cookie.name().replace("poll_", ""); + if let Ok(id) = id.parse::() { + if id == poll_config.id { + if let Ok(value) = cookie.value().parse::() { + existing_choice = Some(value); } } } } - - html.push_str("
"); - html.push_str("
"); - html.push_str("
"); - for choice in choices { - let checked = if Some(choice.id) == existing_choice { "checked" } else { "" }; - - html.push_str(&format!("", choice.id, choice.id, checked)); - html.push_str(&format!( - "", - choice.id, - choice.name.clone(), - choice.votes - )); - } - html.push_str("
"); - html.push_str(""); - html.push_str("
"); - html.push_str("
"); - - return Html(html); } + + html.push_str("
"); + html.push_str("
"); + html.push_str("
"); + for choice in choices { + let checked = if Some(choice.id) == existing_choice { + "checked" + } else { + "" + }; + + html.push_str(&format!("", choice.id, choice.id, checked)); + html.push_str(&format!( + "", + choice.id, + choice.name.clone(), + choice.votes + )); + } + html.push_str("
"); + html.push_str(""); + html.push_str("
"); + html.push_str("
"); + + return Html(html); } } }