Пример #1
0
        public async Task SeedsAsync(int initialIndex)
        {
            int pokemonNationalNumb = initialIndex;

            try {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(_apiUrl);
                    var allTypes = _typeRepository.GetAll();

                    for (int i = initialIndex; i <= 807; i++)
                    {
                        pokemonNationalNumb = i;
                        HttpResponseMessage results;
                        bool responseSuccess = false;
                        int  errorCount      = 0;
                        do
                        {
                            var responseTask = client.GetAsync(string.Concat("pokemon/", i));
                            responseTask.Wait();
                            results         = responseTask.Result;
                            responseSuccess = results.IsSuccessStatusCode;
                            if (!responseSuccess)
                            {
                                errorCount++;
                            }
                            if (errorCount == 3)
                            {
                                throw new PokeApiException();
                            }
                        } while(!responseSuccess);
                        string apiResponse = await results.Content.ReadAsStringAsync();

                        var json       = JObject.Parse(apiResponse);
                        var name       = json["forms"][0]["name"].ToString();
                        var stats      = json["stats"];
                        var types      = json["types"];
                        var firstType  = allTypes.FirstOrDefault(x => x.Name.ToLower() == types[0]["type"]["name"].ToString().ToLower());
                        var secondType = new Models.Type();

                        var pokemonSpecies = new Pokemon_Species()
                        {
                            National_Numb   = i,
                            Name            = name,
                            Base_Hp         = (int)stats[5]["base_stat"],
                            Base_Attack     = (int)stats[4]["base_stat"],
                            Base_Defense    = (int)stats[3]["base_stat"],
                            Base_Sp_Attack  = (int)stats[2]["base_stat"],
                            Base_Sp_Defense = (int)stats[1]["base_stat"],
                            Base_Speed      = (int)stats[0]["base_stat"],
                            First_Type_Id   = firstType.Id,
                        };

                        if (types.Count() > 1)
                        {
                            secondType = allTypes.FirstOrDefault(x => x.Name.ToLower() == types[1]["type"]["name"].ToString().ToLower());
                            pokemonSpecies.Second_Type_Id = secondType.Id;
                        }
                        _pokemonRepository.Create(pokemonSpecies);
                    }
                }
            }
            catch (Exception ex)
            {
                using (var logger = LoggerDomain.GetLog(_connectionString))
                {
                    logger.Error(string.Format("Error at pokemonSpecies {0} - Exception Message: ", pokemonNationalNumb, ex.Message));
                    throw ex;
                }
            }
        }
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json");
            var config           = builder.Build();
            var connectionString = config.GetConnectionString("DefaultConnection");

            int pokemonSpeciesIndex;
            int moveIndex;
            int tableStartInt;

            try {
                tableStartInt       = Int32.Parse(args[0]);
                pokemonSpeciesIndex = Int32.Parse(args[1]);
                moveIndex           = Int32.Parse(args[2]);
            }
            catch (Exception)
            {
                using (var logger = LoggerDomain.GetLog(connectionString))
                {
                    logger.Error("Arguments must be set and be a integer");
                    throw new Exception("Arguments must be set and be a integer");
                }
            }

            var dbSession = new DbSession(config.GetConnectionString("DefaultConnection"));

            var typesRepository          = new TypeRepository(dbSession);
            var typesAdvantageRepository = new TypeAdvantageRepository(dbSession);
            var natureRepository         = new NatureRepository(dbSession);
            var pokemonSpeciesRepository = new PokemonSpeciesRepository(dbSession);
            var moveRepository           = new MoveRepository(dbSession);
            var apiUrl = config["apiURL"];

            var typeSeeds           = new TypesSeeds(typesRepository);
            var typeAdvantagesSeeds = new TypeAdvantagesSeeds(typesRepository, typesAdvantageRepository, apiUrl);
            var natureSeeds         = new NatureSeeds(natureRepository, apiUrl);
            var pokemonSpeciesSeeds = new PokemonSpeciesSeeds(pokemonSpeciesRepository, typesRepository, apiUrl, connectionString);
            var moveSeeds           = new MovesSeeds(moveRepository, typesRepository, apiUrl, connectionString);

            if (tableStartInt <= (int)TableEnum.Type)
            {
                typeSeeds.Seeds();
            }
            if (tableStartInt <= (int)TableEnum.TypeAdvantage)
            {
                await typeAdvantagesSeeds.SeedsAsync();
            }
            if (tableStartInt <= (int)TableEnum.Nature)
            {
                await natureSeeds.SeedsAsync();
            }
            if (tableStartInt <= (int)TableEnum.PokemonSpecies)
            {
                await pokemonSpeciesSeeds.SeedsAsync(pokemonSpeciesIndex);
            }
            if (tableStartInt <= (int)TableEnum.Move)
            {
                await moveSeeds.SeedsAsync(moveIndex);
            }
        }
        public async Task SeedsAsync(int initialIndex)
        {
            var typeLists = _typeRepository.GetAll();
            int moveIndex = initialIndex;

            try {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(_apiUrl);
                    for (int i = initialIndex; i <= 728; i++)
                    {
                        moveIndex = i;
                        HttpResponseMessage results;
                        bool responseSuccess = false;
                        int  errorCount      = 0;
                        do
                        {
                            var responseTask = client.GetAsync(string.Concat("move/", i));
                            responseTask.Wait();
                            results         = responseTask.Result;
                            responseSuccess = results.IsSuccessStatusCode;
                            if (!responseSuccess)
                            {
                                errorCount++;
                            }
                            if (errorCount == 3)
                            {
                                throw new PokeApiException();
                            }
                        } while (responseSuccess == false);

                        string apiResponse = await results.Content.ReadAsStringAsync();

                        var json               = JObject.Parse(apiResponse);
                        var name               = json["names"][2]["name"].ToString();
                        var basePowerString    = json["power"].ToString();
                        var moveTypeString     = json["type"]["name"].ToString();
                        var moveCategoryString = json["damage_class"]["name"].ToString();

                        var basePower    = basePowerString.ToDecimal();
                        var moveType     = typeLists.FirstOrDefault(x => x.Name.ToLower() == moveTypeString);
                        var moveCategory = (MoveCategoryEnum)typeof(MoveCategoryEnum).GetEnumByDescription(moveCategoryString);

                        if (moveCategory == MoveCategoryEnum.Status)
                        {
                            continue;
                        }

                        var move = new Moves()
                        {
                            Name          = name,
                            Base_Power    = basePower,
                            Move_Category = moveCategory,
                            Id_Type       = moveType.Id
                        };

                        _moveRepository.Create(move);
                    }
                }
            }
            catch (Exception ex) {
                using (var logger = LoggerDomain.GetLog(_connectionString))
                {
                    logger.Error(string.Format("Error at pokemonSpecies {0} - Exception Message: ", moveIndex, ex.Message));
                    throw ex;
                }
            }
        }