public async Task <IActionResult> Index(int?id) { // if no pokemon id is passed in then default to first pokemon. // Below is shorter syntax logic to using a if / else statement. // coelesce expression would be == id ?? 1 to shorten below statement even more. int pokemonId = (id.HasValue) ? id.Value : 1; PokemonClient client = new PokemonClient(); PokemonResponse data = await client.GetPokemonAsync(pokemonId); var pokemon = new SinglePokedexEntry { PokedexId = data.id, Weight = data.weight, Height = data.height, ProfileImageUrl = data.sprites.front_default, Name = data.name, Abilities = new List <string>() }; foreach (var currentAbility in data.abilities) { pokemon.Abilities.Add(currentAbility.ability.name); } return(View(pokemon)); }
public async Task <IActionResult> Index(int?id) { // If no PokemonID is passed in, // default to the first pokemon int pokemonId = id ?? 1; PokemonClient client = new PokemonClient(); PokemonResponse response = await client.GetPokemonAsync(pokemonId); var pokemon = new SinglePokedexEntry { PokedexId = response.id, Weight = response.weight, Height = response.height, ProfileImageUrl = response.sprites.front_default, Name = response.name, //Abilities = new List<Ability>(response.abilities) Abilities = new List <string>() }; foreach (var currAbility in response.abilities) { pokemon.Abilities.Add(currAbility.ability.name); } return(View()); }