コード例 #1
0
        public async Task InsertingDataBase(string AirlineNo, string CustomerNo, string FlightsPerCmpnyNo, string TicketsPerCustNo, string CountriesNo)
        {
            _bgWorker.DoWork += (s, e) =>
            {
                for (int i = 0; i < 101; i++)
                {
                    Thread.Sleep(100);
                    ProgressStatus = i;
                }
            };
            _bgWorker.RunWorkerAsync();
            int numberOfAirLines       = Int32.Parse(AirlineNo);
            int numberOfCustomers      = Int32.Parse(CustomerNo);
            int numberOfFlightsPerCmp  = Int32.Parse(FlightsPerCmpnyNo);
            int numberOfTicketsPerCust = Int32.Parse(TicketsPerCustNo);
            int numberOfCountries      = Int32.Parse(CountriesNo);

            try
            {
                await Task.Run(() =>
                {
                    restWorking = new WorkWithAPI();
                    List <Country> countriesFromRest = restWorking.CountriesApiWebWork();
                    int countryNoParsed  = Convert.ToInt32(CountriesNo);
                    int customerNoParsed = Convert.ToInt32(CustomerNo);
                    int flightNoParsed   = Convert.ToInt32(FlightsPerCmpnyNo);
                    int airlineNoParsed  = Convert.ToInt32(AirlineNo);
                    int ticketNoParsed   = Convert.ToInt32(TicketsPerCustNo);
                    ProggressBarMaxVal   = countryNoParsed + customerNoParsed + flightNoParsed + airlineNoParsed + ticketNoParsed;
                    for (int i = 0; i < countryNoParsed; i++)
                    {
                        afc.CreateNewCountry(new LoginToken <Administrator>(), countriesFromRest[i]);
                        ProggressBarVal += i;
                    }

                    List <AirlineCompany> airlines = restWorking.AirlineCompnyApiWork();
                    for (int i = 0; i < airlineNoParsed; i++)
                    {
                        afc.CreateNewAirline(new LoginToken <Administrator>(), airlines[i]);
                        ProggressBarVal += i;
                    }

                    List <Customer> customersREST = restWorking.CustomerApiWork();
                    for (int i = 0; i < customerNoParsed; i++)
                    {
                        afc.CreateNewCustomer(new LoginToken <Administrator>(), customersREST[i]);
                        ProggressBarVal += i;
                    }
                    flightsPerCompany       = new List <Flight>();
                    long airlineId          = 0;
                    long countryCode        = 0;
                    string airlineName      = null;
                    long secondCountryCode  = 0;
                    DateTime firstDate      = new DateTime();
                    DateTime secondDate     = new DateTime();
                    numberOfFlightsToCreate = RandomFlightToBeCreated();
                    for (int j = 0; j < airlineNoParsed; j++)
                    {
                        for (int i = 0; i < flightNoParsed; i++)
                        {
                            airlineId         = alf.GetRandomAirlineId();
                            airlineName       = alf.GetAirlineNameByID((int)airlineId);
                            countryCode       = alf.GetRandomCountryId();
                            secondCountryCode = alf.GetRandomCountryId();
                            firstDate         = RandomDate();
                            Thread.Sleep(10);
                            secondDate = RandomDate2(firstDate);
                            flightsPerCompany.Add(new Flight(airlineId, countryCode, secondCountryCode, firstDate, secondDate, RandomTickets(), airlineName, RandomString()));
                            ProggressBarVal += i;
                        }
                    }

                    foreach (var flight in flightsPerCompany)
                    {
                        alf.CreateFlight(airlineToken, flight);
                    }
                    Random rnd                 = new Random();
                    int numberOfIds            = 0, flightID = 0;
                    List <long> customerIds    = cfc.GetAllCustomerID();
                    LoginService customerLogin = new LoginService();

                    for (int i = 0; i < customerNoParsed; i++)
                    {
                        for (int j = 0; j < ticketNoParsed; j++)
                        {
                            numberOfIds    = rnd.Next(flightNoParsed);
                            Tickets ticket = new Tickets();
                            customerLogin.TryCustomerLogin(restWorking.CustomerApiWork()[i].USER_NAME, restWorking.CustomerApiWork()[i].PASSWORD, out customerToken);
                            flightID           = (int)alf.GetRandomFlightID();
                            ticket             = cfc.PurchaseTicket(customerToken, afc.GetFlightById(flightID));
                            ticket.CUSTOMER_ID = customerIds[i];
                            ticket.FLIGHT_ID   = flightID;
                            afc.AddTicket(ticket);
                            ProggressBarVal += j;
                        }
                    }
                });
            }
            catch (Exception)
            {
                throw new Exception("Something went wrong while trying to insert data...");
            }
        }