/// <summary> /// Generating and adding the data to the database. /// </summary> private void AddItemsToDatabase() { TestFacade facade = new TestFacade(); int customersSum = CustomersField.CurrentItemsNumber + CustomersField.ItemsNum; int airlinesSum = AirlinesField.CurrentItemsNumber + AirlinesField.ItemsNum; int flightsSum = FlightsField.CurrentItemsNumber + FlightsField.ItemsNum; int ticketsSum = TicketsField.CurrentItemsNumber + TicketsField.ItemsNum; if (airlinesSum * flightsSum < TicketsField.ItemsNum) { OnErrorOccured($"Total number of flights ({airlinesSum * flightsSum}) cannot be smaller than tickets per customer number ({TicketsField.ItemsNum})."); return; } else if (customersSum == 0 && TicketsField.ItemsNum > 0) { OnErrorOccured($"Cannot add tickets while there are no customers."); return; } else if (airlinesSum == 0 && FlightsField.ItemsNum > 0) { OnErrorOccured($"Cannot add flights while there are no airline companies."); return; } Task.Run(() => { IsProccessing = true; ProccessingMessageAnimation(); int totalItems = CustomersField.ItemsNum + AirlinesField.ItemsNum + FlightsField.ItemsNum * AirlinesField.ItemsNum + CustomersField.ItemsNum * TicketsField.ItemsNum; int succeeded = 0; if (DeleteDBFirst) { facade.DeleteAllTables(); Log += $"Removed all items from the database.\n"; } try { IList <Customer> customers = DataGenerator.GetRandomCustomersList(CustomersField.ItemsNum); if (CustomersField.ItemsNum > 0) { facade.AddRangeOfCustomers(customers); Log += $"Added {CustomersField.ItemsNum} customers to the database.\n"; succeeded += CustomersField.ItemsNum; CustomersField.CurrentItemsNumber += CustomersField.ItemsNum; } } catch (Exception e) { Log += $"An error has occurred while adding customers to the database. Exception: {e.Message}\n"; } try { IList <AirlineCompany> airlines = DataGenerator.GetRandomAirlineCompaniesList(AirlinesField.ItemsNum); if (AirlinesField.ItemsNum > 0) { facade.AddRangeOfAirlineCompanies(airlines); Log += $"Added {AirlinesField.ItemsNum} airline companies to database.\n"; succeeded += AirlinesField.ItemsNum; AirlinesField.CurrentItemsNumber += AirlinesField.ItemsNum; } } catch (Exception e) { Log += $"An error has occurred while adding airline companies to the database. Exception: {e.Message}\n"; } try { IList <Flight> flights = DataGenerator.GetRandomFlights(FlightsField.ItemsNum); if (FlightsField.ItemsNum > 0) { facade.AddRangeOfFlights(flights); Log += $"Added {FlightsField.ItemsNum} flights for each airline company to the database (Total: {FlightsField.ItemsNum * airlinesSum}).\n"; succeeded += FlightsField.ItemsNum * AirlinesField.ItemsNum; FlightsField.CurrentItemsNumber += FlightsField.ItemsNum; } } catch (Exception e) { Log += $"An error has occurred while adding flights to the database. Exception: {e.Message} # {e.InnerException.Message} # {e.InnerException.InnerException.Message}\n"; } try { IList <Ticket> tickets = DataGenerator.GetRandomTickets(TicketsField.ItemsNum); if (TicketsField.ItemsNum > 0) { facade.AddRangeOfTickets(tickets); Log += $"Added {TicketsField.ItemsNum} tickets for each customer to the database (Total: {TicketsField.ItemsNum * customersSum}).\n"; succeeded += TicketsField.ItemsNum * CustomersField.ItemsNum; TicketsField.CurrentItemsNumber += TicketsField.ItemsNum; } } catch (Exception e) { Log += $"An error has occurred while adding tickets to the database. Exception: {e.Message}\n"; } if (succeeded == totalItems && succeeded > 0) { Log += $"** Added successfully {succeeded} items to the database! (100% success). **\n"; } else if (totalItems > 0) { Log += $"** Added {succeeded} out of {totalItems} items to the database ({(succeeded / totalItems) * 100}% success). **\n"; } else { Log += "No items has been added to the database.\n"; } IsProccessing = false; }); }