Fixed leaderboard display error caused by wrond DTO field names in backend

main
oabrivard 1 week ago
parent cdfe76be2a
commit 2083cb0ba0

@ -87,6 +87,32 @@ pub struct StartSessionResponse {
pub question: QuestionResponse, pub question: QuestionResponse,
} }
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LeaderboardResponse {
pub rank: i32,
pub player_name: String,
pub score: i32,
pub questions_answered: i32,
pub correct_answers: i32,
pub success_rate: f64,
pub duration_seconds: i64,
}
impl From<LeaderboardEntry> for LeaderboardResponse {
fn from(entry: LeaderboardEntry) -> Self {
Self {
rank: entry.rank,
player_name: entry.player_name,
score: entry.score,
questions_answered: entry.questions_answered,
correct_answers: entry.correct_answers,
success_rate: entry.success_rate,
duration_seconds: entry.duration_seconds,
}
}
}
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct AdminQuestionResponse { pub struct AdminQuestionResponse {

@ -1,13 +1,13 @@
use axum::{extract::State, Json}; use axum::{extract::State, Json};
use crate::app_state::AppState; use crate::app_state::AppState;
use crate::dto::responses::LeaderboardResponse;
use crate::error::ApiError; use crate::error::ApiError;
use crate::services::game_service::GameService; use crate::services::game_service::GameService;
use knowfoolery_shared::models::LeaderboardEntry;
pub async fn get_leaderboard( pub async fn get_leaderboard(
State(state): State<AppState>, State(state): State<AppState>,
) -> Result<Json<Vec<LeaderboardEntry>>, ApiError> { ) -> Result<Json<Vec<LeaderboardResponse>>, ApiError> {
let svc = GameService::new( let svc = GameService::new(
state.session_repo.clone(), state.session_repo.clone(),
state.session_question_repo.clone(), state.session_question_repo.clone(),
@ -15,5 +15,6 @@ pub async fn get_leaderboard(
state.leaderboard_repo.clone(), state.leaderboard_repo.clone(),
); );
let entries = svc.get_leaderboard().await?; let entries = svc.get_leaderboard().await?;
Ok(Json(entries)) let response = entries.into_iter().map(LeaderboardResponse::from).collect();
Ok(Json(response))
} }

Loading…
Cancel
Save