Some poll code cleanup
This commit is contained in:
parent
ce6060b0fa
commit
0904f50567
1 changed files with 4 additions and 11 deletions
15
src/main.rs
15
src/main.rs
|
@ -6,7 +6,6 @@ use axum::{
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use axum::extract::rejection::JsonRejection;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
|
||||
|
@ -111,11 +110,9 @@ impl AppState {
|
|||
fn get_poll_config_by_id(id: i32) -> Option<PollConfig> {
|
||||
if let Ok(data) = std::fs::read_to_string("polls.json") {
|
||||
if let Ok(config) = serde_json::from_str::<PollsConfig>(&data) {
|
||||
if let Some(current_poll) = config.current_poll {
|
||||
let current_poll: Vec<&PollConfig> = config.polls.iter().filter(|poll| poll.id == current_poll).collect();
|
||||
if !current_poll.is_empty() {
|
||||
return Some(<&PollConfig>::clone(current_poll.first().unwrap()).clone());
|
||||
}
|
||||
let current_poll: Vec<&PollConfig> = config.polls.iter().filter(|poll| poll.id == id).collect();
|
||||
if !current_poll.is_empty() {
|
||||
return Some(<&PollConfig>::clone(current_poll.first().unwrap()).clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -124,11 +121,7 @@ impl AppState {
|
|||
}
|
||||
|
||||
fn get_poll_votes_by_poll_id(&self, poll_id: i32) -> Option<Poll> {
|
||||
let mut poll_votes = self.poll_votes.lock().unwrap();
|
||||
|
||||
let Some(poll_config) = AppState::get_poll_config_by_id(poll_id) else {
|
||||
return None;
|
||||
};
|
||||
let poll_votes = self.poll_votes.lock().unwrap();
|
||||
|
||||
for existing_poll in &poll_votes.polls {
|
||||
if existing_poll.id == poll_id {
|
||||
|
|
Loading…
Add table
Reference in a new issue