General update: site works.
This commit is contained in:
58
src/main.rs
58
src/main.rs
@@ -31,10 +31,12 @@ struct Player {
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Vote {
|
||||
player_id: i64,
|
||||
nickname: String,
|
||||
reason: String,
|
||||
is_plus: bool,
|
||||
plus_player_id: i64,
|
||||
plus_nickname: String,
|
||||
plus_reason: String,
|
||||
minus_player_id: i64,
|
||||
minus_nickname: String,
|
||||
minus_reason: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -109,25 +111,23 @@ async fn get_data(path: &str, req: &Request<Incoming>, db: Arc<Mutex<SqlitePool>
|
||||
}
|
||||
"/data/results" => {
|
||||
let votes = get_votes(req, db).await;
|
||||
let ids: Vec<(i64, bool)> = votes.iter().map(|x| (x.player_id, x.is_plus)).collect();
|
||||
let ids: Vec<(i64, i64)> = votes.iter().map(|x| (x.plus_player_id, x.minus_player_id)).collect();
|
||||
|
||||
let mut plus_results: HashMap<i64, i64> = HashMap::new();
|
||||
let mut minus_results: HashMap<i64, i64> = HashMap::new();
|
||||
|
||||
let _ = ids.iter().filter(|x| x.1).for_each(|x| {
|
||||
let id = x.0;
|
||||
if plus_results.contains_key(&id) {
|
||||
plus_results.insert(id, 0);
|
||||
let _ = ids.iter().for_each(|x| {
|
||||
let plus_id = x.0;
|
||||
if !plus_results.contains_key(&plus_id) {
|
||||
plus_results.insert(plus_id, 0);
|
||||
}
|
||||
*plus_results.get_mut(&id).unwrap() += 1;
|
||||
});
|
||||
*plus_results.get_mut(&plus_id).unwrap() += 1;
|
||||
|
||||
let _ = ids.iter().filter(|x| !x.1).for_each(|x| {
|
||||
let id = x.0;
|
||||
if minus_results.contains_key(&id) {
|
||||
minus_results.insert(id, 0);
|
||||
let minus_id = x.1;
|
||||
if !minus_results.contains_key(&minus_id) {
|
||||
minus_results.insert(minus_id, 0);
|
||||
}
|
||||
*minus_results.get_mut(&id).unwrap() += 1;
|
||||
*minus_results.get_mut(&minus_id).unwrap() += 1;
|
||||
});
|
||||
|
||||
let mut plus_results: Vec<(i64, i64)> = plus_results.into_iter().collect();
|
||||
@@ -164,13 +164,15 @@ async fn get_votes(req: &Request<Incoming>, db: Arc<Mutex<SqlitePool>>) -> Vec<V
|
||||
}
|
||||
let items = futures::executor::block_on(async move {
|
||||
let formatted_date = format!("{}", date.unwrap().format("%d/%m/%Y"));
|
||||
sqlx::query!(r#"SELECT * FROM votes WHERE SUBMIT_DATE = ?1 ORDER BY id"#, formatted_date).fetch_all(&pool).await.unwrap()
|
||||
sqlx::query!(r#"SELECT * FROM votes WHERE submit_date = ?1 ORDER BY id"#, formatted_date).fetch_all(&pool).await.unwrap()
|
||||
});
|
||||
items.iter().map(|x| Vote {
|
||||
player_id: x.player_id,
|
||||
nickname: x.nickname.clone(),
|
||||
reason: x.reason.clone(),
|
||||
is_plus: x.is_plus,
|
||||
plus_player_id: x.plus_player_id,
|
||||
plus_nickname: x.plus_nickname.clone(),
|
||||
plus_reason: x.plus_reason.clone(),
|
||||
minus_player_id: x.minus_player_id,
|
||||
minus_nickname: x.minus_nickname.clone(),
|
||||
minus_reason: x.minus_reason.clone(),
|
||||
}).collect()
|
||||
}
|
||||
|
||||
@@ -189,12 +191,14 @@ async fn post(req: Request<Incoming>, db: Arc<Mutex<SqlitePool>>) -> Result<Resp
|
||||
let formatted = timestamp.format("%d/%m/%Y").to_string();
|
||||
let pool = db.clone().lock().unwrap().clone();
|
||||
let mut conn = pool.acquire().await.unwrap();
|
||||
let result = sqlx::query!(r#"INSERT INTO votes ( PLAYER_ID, NICKNAME, REASON, IS_PLUS, SUBMIT_DATE )
|
||||
VALUES ( ?1, ?2, ?3, ?4, ?5 )"#,
|
||||
vote.player_id,
|
||||
vote.nickname,
|
||||
vote.reason,
|
||||
vote.is_plus,
|
||||
let result = sqlx::query!(r#"INSERT INTO votes ( plus_player_id, plus_nickname, plus_reason, minus_player_id, minus_nickname, minus_reason, submit_date )
|
||||
values ( ?1, ?2, ?3, ?4, ?5, ?6, ?7 )"#,
|
||||
vote.plus_player_id,
|
||||
vote.plus_nickname,
|
||||
vote.plus_reason,
|
||||
vote.minus_player_id,
|
||||
vote.minus_nickname,
|
||||
vote.minus_reason,
|
||||
formatted).execute(&mut *conn).await;
|
||||
if result.is_err() {
|
||||
return Ok(Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR).body(Full::new(Bytes::from("Internet Error"))).unwrap());
|
||||
|
||||
Reference in New Issue
Block a user