38 lines
1.0 KiB
C#
38 lines
1.0 KiB
C#
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<WebResult> GetPokemonDescription(string pokemonName)
|
|
{
|
|
var result = new WebResult();
|
|
try
|
|
{
|
|
var pokemonSpecies = await pokeApiClient.GetResourceAsync<PokemonSpecies>(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);
|
|
}
|
|
}
|
|
}
|
|
} |