Function get_list_champions

Source
pub async fn get_list_champions(
    ctx: ApplicationContext<'_, Data, Box<dyn Error + Send + Sync>>,
    role: Option<Role>,
) -> Result<Vec<ChampionData>, Box<dyn Error + Send + Sync>>
Expand description

⚙️ Retrieves a list of champions from the database, optionally filtered by role.

This asynchronous function queries the MongoDB collection of champions to retrieve either all champions or those matching a specific role. If a role is provided, the function will filter the list accordingly. If no role is specified, all champions will be retrieved.

§Parameters:

  • ctx: The command context, providing access to the bot’s data and other utilities.
  • role: An optional Role enum to filter the champions by role. If None, all champions are retrieved.

§Returns:

  • Result<Vec<ChampionData>, Error>: On success, returns a vector of ChampionData objects representing the champions that match the criteria. On failure, returns an Error indicating what went wrong during the database query.

§Example:

let champions_list = get_list_champions(ctx, Some(Role::MIDLANE)).await?;
for champion in champions_list {
    println!("Champion: {}", champion.name);
}

§⚠️ Notes:

  • If role is None, the function calls get_champions_with_no_role to retrieve all champions.
  • If role is provided, match_role_with_database_roles is used to convert the Role enum to a string, and get_champions_by_role is called to perform the filtered search.
  • This function interacts with MongoDB asynchronously and relies on other helper functions for specific queries.
  • get_champions_with_no_role: Retrieves all champions without filtering by role.
  • get_champions_by_role: Retrieves champions filtered by a specific role from the collection.
  • match_role_with_database_roles: Converts a Role enum into the corresponding string representation.

§Dependencies:

  • Requires access to a MongoDB collection (champions_data) for champion information.
  • The function depends on other helper functions for querying the MongoDB collection.