示例#1
0
        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 Should_Return_Expected_Data_When_Data_Exits()
        {
            var search       = "metwo";
            var expectedData = await SetUpData();

            _dataProvider.Setup(x => x.GetData(search))
            .Returns(expectedData);

            var expectedResponse = new PokemonResponse
            {
                Name         = "metwo",
                Description  = "It was created by a scientists after years of...",
                HabitantType = HabitantType.Rare.ToString(),
                IsLegendary  = true
            };

            _mapper.Setup(m => m.Map <PokemonResponse>(expectedData)).Returns(expectedResponse);

            var result = _subject.GetDetails(search) as PokemonResponse;

            result.ShouldNotBeNull();
            result.ShouldBeOfType <PokemonResponse>();

            result.Name.ShouldBe(expectedResponse.Name);
            result.HabitantType.ShouldBe(expectedResponse.HabitantType);
        }
示例#3
0
        public async Task <IActionResult> GetTranslation([FromRoute] string pokemonName)
        {
            if (string.IsNullOrWhiteSpace(pokemonName))
            {
                var errorResponse = new ErrorResponse(Status400BadRequest, "Pokemon's name cannot be null, empty nor white space.");
                return(BadRequest(errorResponse));
            }
            try
            {
                var shakespeareDescriptionTranslation = await _pokemonService.GetPokemonDescriptionAsShakespeareAsync(pokemonName);

                var pokemonResponse = new PokemonResponse
                {
                    Name        = pokemonName,
                    Description = shakespeareDescriptionTranslation
                };
                return(Ok(pokemonResponse));
            }
            catch (BrokenCircuitException)
            {
                var errorResponse = new ErrorResponse(Status403Forbidden, "Shakespeare API is inoperative, please try later on. Circuit is now open.");
                return(StatusCode(Status403Forbidden, errorResponse));
            }
            catch (SimpleHttpResponseException simpleHttpResponseException)
            {
                var errorResponse = new ErrorResponse(simpleHttpResponseException.StatusCode, simpleHttpResponseException.Message);
                return(StatusCode(simpleHttpResponseException.StatusCodeValue, errorResponse));
            }
            catch (Exception exception)
            {
                var errorResponse = new ErrorResponse(Status500InternalServerError, exception.Message);
                return(StatusCode(Status500InternalServerError, errorResponse));
            }
        }
示例#4
0
        public static Attachment CreatePokemonAttachment(PokemonResponse pokemon)
        {
            // combine path for cross platform support
            var paths                     = new[] { ".", "Resources", "pokemonCard.json" };
            var pokemonCardJson           = File.ReadAllText(Path.Combine(paths));
            AdaptiveCardTemplate template = new AdaptiveCardTemplate(pokemonCardJson);

            pokemon.name = pokemon.name.ToUpper();
            pokemon.types[0].type.name = pokemon.types[0].type.name.ToUpper();
            var myData = new
            {
                pokemon    = pokemon,
                secondType = pokemon.types.Count > 1 ? pokemon.types[1].type.name.ToUpper() : "",
                // esta feo esto :(
                hp        = pokemon.stats[0].base_stat,
                attack    = pokemon.stats[1].base_stat,
                defense   = pokemon.stats[2].base_stat,
                spAttack  = pokemon.stats[3].base_stat,
                spDefense = pokemon.stats[4].base_stat,
                speed     = pokemon.stats[5].base_stat,
            };
            string cardJson = template.Expand(myData);


            var pokemonCardAttachment = new Attachment()
            {
                ContentType = "application/vnd.microsoft.card.adaptive",
                Content     = JsonConvert.DeserializeObject(cardJson),
            };

            return(pokemonCardAttachment);
        }
示例#5
0
        internal void IRequestInformationForThePokemon()
        {
            apiClientBuilder.ForPokemonName(_pokemonName);
            var(pokeApiClient, request) = apiClientBuilder.Build();

            Pokemon = pokeApiClient.GetPokemonAsync(request).GetAwaiter().GetResult();
        }
        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());
        }
        public void  Should_Return_Null_When_Data_Not_Exists()
        {
            var             search       = "DFGRTYR";
            PokemonResponse expectedData = null;

            _dataProvider.Setup(x => x.GetData(search))
            .Returns((Persistency.Pokemon)null);

            var result = _subject.GetDetails(search);

            result.ShouldBeNull();
        }
示例#8
0
        public async Task <PokemonResponse> MapPokemonInfo(int id)
        {
            var apiPokemon = await _pokemonApi.GetPokemon(id);

            var pokemon = new PokemonResponse();

            pokemon.PokemonName     = apiPokemon.name;
            pokemon.PokemonId       = apiPokemon.id;
            pokemon.PokemonImageUrl = apiPokemon.sprites.front_default;

            return(pokemon);
        }
示例#9
0
        static async Task Main(string[] args)
        {
            PokemonClient   client = new PokemonClient();
            PokemonResponse result = await client.GetPokemonAsync(1);

            Console.WriteLine($"Name: {result.name}");
            Console.WriteLine($"Weight: {result.weight}");

            Console.WriteLine("\n\n" + "Abilities" + "\n");
            foreach (var ability in result.abilities)
            {
                Console.WriteLine(ability.ability.name);
                Console.WriteLine($"More information at: {ability.ability.url}");
            }

            Console.ReadKey();
        }
示例#10
0
        public async Task ReturnExpectedResult_WhenPokemonExists()
        {
            const string validPokemonName = "mewtwo";
            const string description      = "It was created by a scientist after years of horrific gene splicing and DNA engineering experiments";
            const string habitat          = "rare";
            var          expectedPokemon  = new PokemonResponse(validPokemonName, description, habitat, true, Language.English);

            mockApiClient
            .Setup(api => api.GetPokemonAsync(It.IsAny <GetPokemonRequest>()))
            .Returns(Task.FromResult(expectedPokemon));

            var controller = new PokemonController(mockApiClient.Object, new NullLogger <PokemonController>());
            var result     = await controller.GetPokemonContentInEnglish(validPokemonName).ConfigureAwait(false);

            var okResult        = result.Should().BeOfType <OkObjectResult>();
            var pokemonResponse = okResult.Which.Value.Should().BeAssignableTo <PokemonResponse>();

            pokemonResponse.Which.Should().BeEquivalentTo(expectedPokemon);
        }
示例#11
0
        public static async Task RequestPokemonById()
        {
            PokemonClient client = new PokemonClient();
            int           id;

            Console.Write("What is the Pokedex Number of the pokemon you are attempting to search? ");
            string searchtype = Console.ReadLine();

            try
            {
                id = Convert.ToInt32(searchtype);
                PokemonResponse pr = await client.GetPokemon(id);

                Console.WriteLine($"{pr.name} | Pokedex No: {id}");
                Console.WriteLine($"{pr.name}'s Typing's");
                Console.WriteLine("=====================");
                foreach (var types in pr.types)
                {
                    Console.WriteLine(types.type.name);
                }
                Console.WriteLine();
                Console.WriteLine($"{pr.name}'s Abilites's");
                Console.WriteLine("=======================");
                foreach (var abilities in pr.abilities)
                {
                    if (!abilities.is_hidden)
                    {
                        Console.WriteLine("Normal Ability: " + abilities.ability.name);
                    }
                    else
                    {
                        Console.WriteLine("Hidden Ability: " + abilities.ability.name);
                    }
                }
                Console.ReadKey();
            }
            catch
            {
                Console.WriteLine("Sorry, but this is not a correct id or this pokemon does not exist.");
                Console.Write("Press Any Key To Exit....");
                Console.ReadKey();
            }
        }
示例#12
0
        public static async Task RequestPokemonByName()
        {
            PokemonClient client = new PokemonClient();

            Console.Write("What is the name of the pokemon you are attempting to search for? ");
            string search = Console.ReadLine().ToLower();

            try
            {
                PokemonResponse pr = await client.GetPokemon(search);

                Console.WriteLine($"{pr.name} | Pokedex No: {pr.id}");
                Console.WriteLine($"{pr.name}'s Typing's");
                Console.WriteLine("=====================");
                foreach (var types in pr.types)
                {
                    Console.WriteLine(types.type.name);
                }
                Console.WriteLine();
                Console.WriteLine($"{pr.name}'s Abilites's");
                Console.WriteLine("=======================");
                foreach (var abilities in pr.abilities)
                {
                    if (!abilities.is_hidden)
                    {
                        Console.WriteLine("Normal Ability: " + abilities.ability.name);
                    }
                    else
                    {
                        Console.WriteLine("Hidden Ability: " + abilities.ability.name);
                    }
                }
                Console.ReadKey();
            }
            catch
            {
                Console.WriteLine("Sorry, but the pokemon you attempted to search for does not exist or spelling error's were made.");
                Console.Write("Press Any Key To Exit....");
                Console.ReadKey();
            }
        }
示例#13
0
        public async Task Should_Return_Error_Message_When_No_Data_Found()
        {
            var search       = "mefour";
            var expectedData = await SetUpData();

            _dataProvider.Setup(x => x.GetData(search))
            .Returns(expectedData);

            var expectedResponse = new PokemonResponse
            {
                Name         = "metwo",
                Description  = "Lost a planet,  master obiwan has",
                HabitantType = HabitantType.Rare.ToString(),
                IsLegendary  = true
            };

            _mapper.Setup(m => m.Map <PokemonResponse>(expectedData)).Returns(expectedResponse);

            var result = await _subject.GetTranslatedDetails(search);

            result.ShouldNotBeNull();
            //  result.ShouldBeOfType<ErrorResponse>();
            //  result.ShouldBeOfType<ApiResponse>();
        }
示例#14
0
        public async Task <PokemonResponse> Get(int id)
        {
            using (var db = Db)
            {
                var pokemonRepository = new PokemonRepository(db);

                var pokemon = await pokemonRepository.FindOneById(id);

                var pokemonResponse = new PokemonResponse()
                {
                    Id          = pokemon.Id,
                    Name        = pokemon.Name,
                    BaseAttack  = pokemon.BaseAttack,
                    BaseDefense = pokemon.BaseDefense,
                    BaseHP      = pokemon.BaseHP,
                    BaseSpAtk   = pokemon.BaseSpAtk,
                    BaseSpDef   = pokemon.BaseSpDef,
                    BaseSpeed   = pokemon.BaseSpeed,
                    Skills      = await GetPokemonSkills(pokemon.Id, db),
                    Types       = await GetPokemonTypes(pokemon.Id, db)
                };
                return(pokemonResponse);
            }
        }
示例#15
0
        public async Task <SelectPokemonViewModel> GetPokemonByGeneration()
        {
            var generationTracker = 1;
            var pokemonList       = new SelectPokemonViewModel();

            while (generationTracker < 8)
            {
                var api = await _pokemonApi.GetPokemonByGeneration(generationTracker);

                if (generationTracker == 1)
                {
                    foreach (var pokemon in api.pokemon_species)
                    {
                        var pokemonToList = new PokemonResponse
                        {
                            PokemonName = pokemon.name
                        };
                        pokemonList.Gen1.Add(pokemonToList);
                        pokemonList.Gen1.Sort((x, y) => string.Compare(x.PokemonName, y.PokemonName));
                    }
                }
                else if (generationTracker == 2)
                {
                    foreach (var pokemon in api.pokemon_species)
                    {
                        var pokemonToList = new PokemonResponse
                        {
                            PokemonName = pokemon.name
                        };
                        pokemonList.Gen2.Add(pokemonToList);
                        pokemonList.Gen2.Sort((x, y) => string.Compare(x.PokemonName, y.PokemonName));
                    }
                }
                else if (generationTracker == 3)
                {
                    foreach (var pokemon in api.pokemon_species)
                    {
                        var pokemonToList = new PokemonResponse
                        {
                            PokemonName = pokemon.name
                        };
                        pokemonList.Gen3.Add(pokemonToList);
                        pokemonList.Gen3.Sort((x, y) => string.Compare(x.PokemonName, y.PokemonName));
                    }
                }
                else if (generationTracker == 4)
                {
                    foreach (var pokemon in api.pokemon_species)
                    {
                        var pokemonToList = new PokemonResponse
                        {
                            PokemonName = pokemon.name
                        };
                        pokemonList.Gen4.Add(pokemonToList);
                        pokemonList.Gen4.Sort((x, y) => string.Compare(x.PokemonName, y.PokemonName));
                    }
                }
                else if (generationTracker == 5)
                {
                    foreach (var pokemon in api.pokemon_species)
                    {
                        var pokemonToList = new PokemonResponse
                        {
                            PokemonName = pokemon.name
                        };
                        pokemonList.Gen5.Add(pokemonToList);
                        pokemonList.Gen5.Sort((x, y) => string.Compare(x.PokemonName, y.PokemonName));
                    }
                }
                else if (generationTracker == 6)
                {
                    foreach (var pokemon in api.pokemon_species)
                    {
                        var pokemonToList = new PokemonResponse
                        {
                            PokemonName = pokemon.name
                        };
                        pokemonList.Gen6.Add(pokemonToList);
                        pokemonList.Gen6.Sort((x, y) => string.Compare(x.PokemonName, y.PokemonName));
                    }
                }
                else if (generationTracker == 7)
                {
                    foreach (var pokemon in api.pokemon_species)
                    {
                        var pokemonToList = new PokemonResponse
                        {
                            PokemonName = pokemon.name
                        };
                        pokemonList.Gen7.Add(pokemonToList);
                        pokemonList.Gen7.Sort((x, y) => string.Compare(x.PokemonName, y.PokemonName));
                    }
                }
                generationTracker++;
            }
            return(pokemonList);
        }