diff --git a/frontend/src/components/player_list.rs b/frontend/src/components/player_list.rs index 2471beb..da5575a 100644 --- a/frontend/src/components/player_list.rs +++ b/frontend/src/components/player_list.rs @@ -42,7 +42,7 @@ pub fn player_list(props: &Props) -> Html { loading.set(true); let endpoint = format!("{}/players/{}", env!("SERVER_ADDRESS"), gamer_tag); - let mut fetched_players: Vec = Request::get(&endpoint) + let fetched_players: Vec = Request::get(&endpoint) .send() .await .unwrap() @@ -50,8 +50,6 @@ pub fn player_list(props: &Props) -> Html { .await .unwrap(); - fetched_players.sort_by_key(|e| e.player_id); - total_pages.set((fetched_players.len() as f32 / PAGE_SIZE as f32).ceil() as usize); search_results.set(fetched_players.clone()); diff --git a/lib/src/player.rs b/lib/src/player.rs index 9816d2b..ec244db 100644 --- a/lib/src/player.rs +++ b/lib/src/player.rs @@ -18,7 +18,9 @@ use diesel::{ insert_into, prelude::*, result::{DatabaseErrorKind, Error as DieselError}, - sql_query, update, + sql_query, + sql_types::Text, + update, }; use smithe_database::{ db_models::empty_player_ids::EmptyPlayerId, schema::empty_player_ids::dsl::*, @@ -62,12 +64,24 @@ pub async fn get_highest_id_with_sets_between(start_id: i32, end_id: i32) -> Res pub async fn get_all_like(tag: &str) -> Result> { let processed_tag = tag.replace(' ', "%"); // ^^^ transform spaces into wildcards to make search more inclusive - let mut db_connection = smithe_database::connect().await?; - let matching_players: Vec = players - .filter(gamer_tag.ilike(format!("%{}%", processed_tag))) // case-insensitive like - .get_results::(&mut db_connection) - .await?; + + let query = diesel::sql_query("SELECT * FROM players WHERE gamer_tag ILIKE $1 ORDER BY LENGTH(gamer_tag) ASC, player_id ASC") + .bind::(format!("%{}%", processed_tag)); + let mut matching_players: Vec = query.get_results::(&mut db_connection).await?; + + match tag.parse::() { + Ok(pid) => { + if let Ok(exact_id_match) = get_player(pid).await { + matching_players.insert(0, exact_id_match); + } + } + Err(_) => { + if let Ok(exact_slug_match) = get_player_from_slug(&processed_tag).await { + matching_players.insert(0, exact_slug_match); + } + } + } Ok(matching_players) } @@ -708,6 +722,25 @@ mod tests { assert_eq!(res.unwrap().len(), 1); } + #[tokio::test] + #[cfg(feature = "skip_db_tests")] + async fn test_get_all_like_id() { + let res = get_all_like("1178271").await; + assert!(res.is_ok()); + assert_eq!( + res.unwrap()[0].user_slug, + format!("user/{}", DANTOTTO_PLAYER_SLUG) + ); + } + + #[tokio::test] + #[cfg(feature = "skip_db_tests")] + async fn test_get_all_like_slug() { + let res = get_all_like(DANTOTTO_PLAYER_SLUG).await; + assert!(res.is_ok()); + assert_eq!(res.unwrap()[0].player_id, DANTOTTO_PLAYER_ID); + } + #[tokio::test] #[cfg(feature = "skip_db_tests")] async fn test_get_player() { diff --git a/src/smithe/player.rs b/src/smithe/player.rs index f0630c6..d3ec5e4 100644 --- a/src/smithe/player.rs +++ b/src/smithe/player.rs @@ -59,8 +59,7 @@ pub async fn handle_id(id: &i32) -> Result<()> { pub async fn handle_player(tag: &str) -> Result<()> { tracing::info!("🔍 looking for players with tags similar to the provided one..."); - let mut matching_players: Vec = get_all_like(tag).await?; - matching_players.sort_by_key(|e| e.player_id); + let matching_players: Vec = get_all_like(tag).await?; // cli display let selection = Select::with_theme(&ColorfulTheme::default())