-
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: add leaderboard endpoint (#44)
* feat: add leaderboard endpoint * feat: create new leaderboard on signup * feat: add get leaderboard endpoint * feat: init leaderboard for all users before server starts
- Loading branch information
1 parent
b8f7d70
commit bc83fed
Showing
13 changed files
with
225 additions
and
30 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
migrations/2024-11-21-133933_add-expected-module-total-score/down.sql
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,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
ALTER TABLE leaderboard DROP COLUMN IF EXISTS expected_total_score; |
2 changes: 2 additions & 0 deletions
2
migrations/2024-11-21-133933_add-expected-module-total-score/up.sql
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,2 @@ | ||
-- Your SQL goes here | ||
ALTER TABLE leaderboard ADD COLUMN IF NOT EXISTS expected_total_score INTEGER NOT NULL DEFAULT 0; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use actix_web::{web, HttpResponse, Scope}; | ||
use log::error; | ||
|
||
use crate::{ | ||
service::database::{conn::DbPool, models::Leaderboard}, | ||
shared::errors::RepositoryError, | ||
}; | ||
|
||
pub fn init() -> Scope { | ||
web::scope("/leaderboard").route("", web::get().to(get_leaderboard)) | ||
} | ||
|
||
async fn get_leaderboard(pool: web::Data<DbPool>) -> Result<HttpResponse, RepositoryError> { | ||
let mut conn = pool.get().map_err(|e| { | ||
error!("Error getting db connection from pool: {}", e); | ||
RepositoryError::DatabaseError(e.to_string()) | ||
})?; | ||
|
||
let leaderboard = Leaderboard::get_leaderboard(&mut conn, None).map_err(|e| { | ||
error!("Error getting leaderboard: {}", e); | ||
RepositoryError::DatabaseError(e.to_string()) | ||
})?; | ||
|
||
Ok(HttpResponse::Ok().json(leaderboard)) | ||
} |
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
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
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
Oops, something went wrong.