/// <summary> /// Delete all the database. /// </summary> public void DeleteFromDB() { FlightCenterProject.CustomerDAOMSSQL customerDAOMSSQL = new FlightCenterProject.CustomerDAOMSSQL(); customerDAOMSSQL.RemoveAll(); FlightCenterProject.FlightDAOMSSQL flightDAOMSSQL = new FlightCenterProject.FlightDAOMSSQL(); flightDAOMSSQL.RemoveAll(); FlightCenterProject.AirlineCompanyDAOMSSQL airlineCompanyDAOMSSQL = new FlightCenterProject.AirlineCompanyDAOMSSQL(); airlineCompanyDAOMSSQL.RemoveAll(); FlightCenterProject.CountryDAOMSSQL countryDAOMSSQL = new FlightCenterProject.CountryDAOMSSQL(); countryDAOMSSQL.RemoveAll(); Message = "The entire database has been deleted"; }
/// <summary> /// Add random countries to the database. /// </summary> /// <param name="countries"></param> public void AddCountriesToDB(int countries) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(URL2); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync("").Result; if (response.IsSuccessStatusCode) { var dataObjects = response.Content.ReadAsAsync <IEnumerable <Country> >().Result; int counter = 1; foreach (var d in dataObjects) { FlightCenterProject.CountryDAOMSSQL countryDAOMSSQL = new FlightCenterProject.CountryDAOMSSQL(); string country = d.name; FlightCenterProject.Country c = new FlightCenterProject.Country(country); countryDAOMSSQL.Add(c); if (Total == 0) { Percent = 100; counter = 0; } else { Percent += Convert.ToInt32(1.0 / Total * 100); } Message = $"{counter}/{countries} Countries created"; if (++counter > countries) { break; } } } else { Debug.Write((int)response.StatusCode, response.ReasonPhrase); } client.Dispose(); }
/// <summary> /// Add random number of flights for the airline companies that exist in the database. /// </summary> /// <param name="flights"></param> /// <param name="airlinesCom"></param> public void AddFlightsToDB(int flights, int airlinesCom) { FlightCenterProject.AirlineCompanyDAOMSSQL airlineCompany = new FlightCenterProject.AirlineCompanyDAOMSSQL(); IList <FlightCenterProject.AirlineCompany> airlines = airlineCompany.GetAll(); try { if (flights > 0 && airlines.Count == 0) { throw new YouNeedToPutNumberOfAirlinesException("You can not get flights if you dont have airlines"); } else { for (int i = 0; i < (flights * airlinesCom); i++) { long airlineId = airlines[rnd.Next(airlines.Count)].Id; FlightCenterProject.CountryDAOMSSQL cAOMSSQL = new FlightCenterProject.CountryDAOMSSQL(); IList <FlightCenterProject.Country> countries = cAOMSSQL.GetAll(); long originCountryCode = countries[rnd.Next(countries.Count)].Id; long destinationCountryCode = countries[rnd.Next(countries.Count)].Id; HttpClient client = new HttpClient(); client.BaseAddress = new Uri(URL1); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync("").Result; if (response.IsSuccessStatusCode) { WebResult dataObjects = response.Content.ReadAsAsync <WebResult>().Result; DateTime departureTime = dataObjects.results[0].dob.date; DateTime landingTime = dataObjects.results[0].registered.date; DateTime DEPARTURE_TIME = new DateTime(departureTime.Year, departureTime.Month, departureTime.Day, departureTime.Hour, departureTime.Minute, departureTime.Second); DateTime LANDING_TIME = new DateTime(landingTime.Year, landingTime.Month, landingTime.Day, landingTime.Hour, landingTime.Minute, landingTime.Second); FlightCenterProject.FlightDAOMSSQL flightDAOMSSQL = new FlightCenterProject.FlightDAOMSSQL(); FlightCenterProject.Flight flight = new FlightCenterProject.Flight(airlineId, originCountryCode, destinationCountryCode, DEPARTURE_TIME, LANDING_TIME, rnd.Next(200)); flightDAOMSSQL.Add(flight); } else { Debug.Write((int)response.StatusCode, response.ReasonPhrase); } client.Dispose(); Percent += Convert.ToInt32(1.0 / Total * 100); Message = $"{i + 1}/{flights * airlinesCom} Flights created"; } } }catch (YouNeedToPutNumberOfAirlinesException e) { Message = e.Message; } }
/// <summary> /// Add airline companies from a list of companies randomaly to the database. /// </summary> /// <param name="airlines"></param> /// <param name="airlinesCom"></param> public void AddAirlinesToDB(List <string> airlines, int airlinesCom) { try { if (airlinesCom > 100) { throw new ThereAreNoMoreThan100AirlinesException($"you can not get {airlinesCom} airline companies because the information store has names of 100 airlines"); } else { List <string> newList = new List <string>(); for (int i = 0; i < airlinesCom; i++) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(URL1); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync("").Result; int r = rnd.Next(airlines.Count); int sum = 0; foreach (string company in newList) { if (company == airlines[r]) { i = i - 1; break; } else { sum++; } } if (sum == newList.Count) { if (response.IsSuccessStatusCode) { WebResult dataObjects = response.Content.ReadAsAsync <WebResult>().Result; string userName = dataObjects.results[0].login.username; string pass = dataObjects.results[0].login.password; newList.Add(airlines[r]); FlightCenterProject.CountryDAOMSSQL cAOMSSQL = new FlightCenterProject.CountryDAOMSSQL(); IList <FlightCenterProject.Country> countries = cAOMSSQL.GetAll(); long countryCode = countries[rnd.Next(countries.Count)].Id; FlightCenterProject.AirlineCompanyDAOMSSQL companyDAOMSSQL = new FlightCenterProject.AirlineCompanyDAOMSSQL(); FlightCenterProject.AirlineCompany airline = new FlightCenterProject.AirlineCompany(airlines[r], userName, pass, countryCode); companyDAOMSSQL.Add(airline); } else { Debug.Write((int)response.StatusCode, response.ReasonPhrase); } client.Dispose(); Percent += Convert.ToInt32(1.0 / Total * 100); Message = $"{i + 1}/{airlinesCom} Airline companies created"; } } } }catch (ThereAreNoMoreThan100AirlinesException e) { Message = e.Message; } }