Skip to content

Commit

Permalink
Search Engine Improvements (#112)
Browse files Browse the repository at this point in the history
* Adding id and slug search to CLI

Signed-off-by: Carlos R <[email protected]>

* Formatting changes

Signed-off-by: Carlos R <[email protected]>

* Add test for get_player_from_slug

Signed-off-by: Carlos R <[email protected]>

* Improving Search (imcomplete)

Signed-off-by: Carlos R <[email protected]>

* Slug, ID, and better Tag search

Signed-off-by: Carlos R <[email protected]>

* Formatting

Signed-off-by: Carlos R <[email protected]>

* Prevent Bad searches

Signed-off-by: Carlos R <[email protected]>

---------

Signed-off-by: Carlos R <[email protected]>
  • Loading branch information
CERLgit authored Feb 26, 2024
1 parent 0052958 commit 6d3ada3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
4 changes: 1 addition & 3 deletions frontend/src/components/player_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ pub fn player_list(props: &Props) -> Html {
loading.set(true);

let endpoint = format!("{}/players/{}", env!("SERVER_ADDRESS"), gamer_tag);
let mut fetched_players: Vec<Player> = Request::get(&endpoint)
let fetched_players: Vec<Player> = Request::get(&endpoint)
.send()
.await
.unwrap()
.json()
.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());

Expand Down
45 changes: 39 additions & 6 deletions lib/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
Expand Down Expand Up @@ -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<Vec<Player>> {
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<Player> = players
.filter(gamer_tag.ilike(format!("%{}%", processed_tag))) // case-insensitive like
.get_results::<Player>(&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::<Text, _>(format!("%{}%", processed_tag));
let mut matching_players: Vec<Player> = query.get_results::<Player>(&mut db_connection).await?;

match tag.parse::<i32>() {
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)
}
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions src/smithe/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Player> = get_all_like(tag).await?;
matching_players.sort_by_key(|e| e.player_id);
let matching_players: Vec<Player> = get_all_like(tag).await?;

// cli display
let selection = Select::with_theme(&ColorfulTheme::default())
Expand Down

0 comments on commit 6d3ada3

Please sign in to comment.