-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: init leaderboard for all users before server starts
- Loading branch information
1 parent
de3e82f
commit b9a720a
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use log::{error, info}; | ||
|
||
use crate::service::database::{ | ||
conn::DbPool, | ||
models::{Leaderboard, User}, | ||
}; | ||
|
||
pub async fn initialize_leaderboards(pool: &DbPool) -> Result<(), Box<dyn std::error::Error>> { | ||
let mut conn = pool.get()?; | ||
|
||
// Get all users | ||
let users = User::get_all_users(&mut conn)?; | ||
|
||
if users.is_empty() { | ||
info!("No users found in database - skipping leaderboard initialization"); | ||
return Ok(()); | ||
} | ||
|
||
let mut initialized_count = 0; | ||
for user in users { | ||
// Check if user has a leaderboard entry | ||
match Leaderboard::get_leaderboard(&mut conn, Some(&user.id)) { | ||
Ok(leaderboard) => { | ||
if leaderboard.is_empty() { | ||
// Create new leaderboard for user | ||
let new_leaderboard = Leaderboard::new(&user.id, None, 0, 0); | ||
Leaderboard::create(&mut conn, new_leaderboard)?; | ||
initialized_count += 1; | ||
} | ||
} | ||
Err(e) => { | ||
error!("Error checking leaderboard for user {}: {}", user.id, e); | ||
// Create new leaderboard for user | ||
let new_leaderboard = Leaderboard::new(&user.id, None, 0, 0); | ||
Leaderboard::create(&mut conn, new_leaderboard)?; | ||
initialized_count += 1; | ||
} | ||
} | ||
} | ||
|
||
if initialized_count > 0 { | ||
info!( | ||
"Initialized {} missing leaderboard records", | ||
initialized_count | ||
); | ||
} else { | ||
info!("All users have leaderboard records - no initialization needed"); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod auth; | ||
pub mod init; | ||
pub mod routes; | ||
pub mod progress; | ||
pub mod repo; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters