vote: Change graph display condition, don't fetch for admin

Now, the graph will only show when everyone has voted and the week is no longer
the last one active.
This is to minimize information gathering by looking during voting.

Don't fetch the vote data for the admin : it is not used, so no need to
query the database.

Update README with new TODOs and clean up main includes a bit.
This commit is contained in:
trotFunky 2024-07-26 00:45:53 +01:00
parent f41f5142c9
commit 635716c04b
4 changed files with 15 additions and 5 deletions

View file

@ -10,12 +10,14 @@ A list of things that could be implemented/added to the application, some of the
- [ ] Being able to change from one week to the next
- [ ] Create new weeks for the admin
- [ ] Proper week redirection
- [ ] Correctly handle non-existing week number
- [x] Add introduction to the weekly truths
- [ ] Bundle static assets in the binary
- [ ] Move the database queries to their own functions
- [ ] Cache those results
- [ ] Centralize Markdown parsing ?
- [ ] Use fairings for the different elements ?
- [ ] Use guards for User calls ?
# Dependencies

View file

@ -86,7 +86,8 @@ pub async fn get_user(week: u8, db: &mut Connection<Db>, cookies: &CookieJar<'_>
(String::new(), false)
};
let votes: Vec<Vote> = if logged_in {
// TODO: Move to src/vote.rs
let votes: Vec<Vote> = if logged_in && !is_admin {
sqlx::query_as("SELECT Votes.* FROM Votes JOIN Truths ON Votes.truth_id == Truths.id AND Truths.week == $1 WHERE voter_id == $2 ORDER BY Truths.number;")
.bind(week)
.bind(&id_str)

View file

@ -12,6 +12,8 @@ use rocket_db_pools::{sqlx, sqlx::Row, Database, Connection};
use sqlx::Error;
mod auth;
use auth::User;
mod truth;
mod vote;
mod week;
@ -19,9 +21,8 @@ mod week;
mod database;
mod database_records;
use database::Db;
use database_records::*;
use auth::User;
use database::Db;
#[get("/")]
async fn index(mut db: Connection<Db>) -> Redirect {

View file

@ -2,6 +2,7 @@ use std::collections::hash_map::Entry;
use std::collections::HashMap;
use rocket::fairing::AdHoc;
use rocket::form::Form;
use rocket::futures::TryFutureExt;
use rocket::http::CookieJar;
use rocket::response::Redirect;
use rocket::serde::{Serialize, Deserialize};
@ -61,7 +62,7 @@ pub async fn vote(week: u8, form: Form<VoteForm>,
}
None => {
debug!("Player {:?} voting {voted_id} for truth {truth_id}", user.id);
// TODO: Find a way to use only one statement ?
// TODO: Find a way to use only one statement ? --> query_many
// Cannot all launch and await because all connect to DB
match sqlx::query("INSERT INTO Votes (truth_id, voter_id, voted_id) VALUES ($1, $2, $3);")
.bind(truth_id)
@ -110,7 +111,12 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection<database::Db>) -> Opti
});
let vote_count = raw_votes.iter().fold(0, |count, votes| {count + votes.votes});
if vote_count < 17 {
// Only show the graph if we have all the votes and this is not the last week.
// FIXME: Make the 42 not hardcoded
if vote_count < 42 || week != sqlx::query_scalar("SELECT number from Weeks WHERE number == $1 AND is_last_week == 1;")
.bind(week)
.fetch_optional(&mut **db)
.await.unwrap_or(Some(0)).unwrap_or(0) {
return None;
}