using PokeApiNet; using Pokespearean.Models.Generic; using System; using System.Threading.Tasks; using System.Linq; namespace Pokespearean.Services { public class PokemonService : IPokemonService { private readonly PokeApiClient pokeApiClient; public PokemonService() { pokeApiClient = new PokeApiClient(); } public async Task GetPokemonDescription(string pokemonName) { var result = new WebResult(); try { var pokemonSpecies = await pokeApiClient.GetResourceAsync(pokemonName); var hasRubyVersion = pokemonSpecies.FlavorTextEntries.Where(fte => fte.Language.Name == "en" && fte.Version.Name == "ruby").Any(); result.Data = pokemonSpecies.FlavorTextEntries .FirstOrDefault(fte => (hasRubyVersion ? fte.Version.Name == "ruby" : true) && fte.Language.Name == "en")? .FlavorText?.Replace("\n", " ")?.Replace("\f", " "); return result; } catch (Exception ex) { Console.WriteLine(ex.Message); return result.Invalidate(ex.Message, ex); } } } }