Exemple #1
0
        static async Task RunAsyncMovesRelations(string parameter)
        {
            PokedexEntities context = new PokedexEntities();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://pokeapi.co/api/v2/pokemon/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // New code:
                HttpResponseMessage response = await client.GetAsync(parameter);

                if (response.IsSuccessStatusCode)
                {
                    ParserPokemon poke = await response.Content.ReadAsAsync <ParserPokemon>();

                    Regex regex = new Regex(@"/\d+/");
                    for (int i = 0; i < poke.moves.Count; i++)
                    {
                        Match match = regex.Match(poke.moves[i].move.url);
                        context.CreateMoveRelation(poke.id, int.Parse(match.Value.Replace('/', ' ')));
                        Console.WriteLine("Creando relacion entre {0}  y {1} \n", poke.name, poke.moves[i].move.name);
                    }
                }
            }
        }
Exemple #2
0
        static async Task RunAsyncPokemon(string parameter, int param)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://pokeapi.co/api/v2/pokemon/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // New code:
                HttpResponseMessage response = await client.GetAsync(parameter);

                if (response.IsSuccessStatusCode)
                {
                    ParserPokemon poke = await response.Content.ReadAsAsync <ParserPokemon>();

                    Pokemon temp;
                    using (PokedexEntities context = new PokedexEntities())
                    {
                        temp = context.Pokemons.Where(p => p.PokemonID == param).FirstOrDefault();
                    }
                    if (temp != null)
                    {
                        temp.pathImg = poke.sprites.front_default;
                    }
                    using (PokedexEntities pkcontext = new PokedexEntities())
                    {
                        pkcontext.Entry(temp).State = System.Data.Entity.EntityState.Modified;
                        pkcontext.SaveChanges();
                    }



                    Console.WriteLine("foto agregada {0} ha sido agregado", poke.name);
                }
            }
        }