public override bool Equals(object obj) { if (ReferenceEquals(obj, null)) { return(false); } AirlineCompany otherAirlineComapny = obj as AirlineCompany; return(this.Id == otherAirlineComapny.Id); }
public override bool Equals(object obj) { AirlineCompany airlineCompany = obj as AirlineCompany; if (airlineCompany == null) { return(false); } return(this == airlineCompany); }
public IList <AirlineCompany> GetAirlinesByCountryId(int countryId) { List <AirlineCompany> airlineCompanies = new List <AirlineCompany>(); TryCatchDatabaseFunction((conn) => { // Checking if a country with that ID exist: string query = $"SELECT * FROM Countries WHERE Countries.ID = {countryId}"; MySqlCommand cmd = new MySqlCommand(query, conn); MySqlDataReader reader = cmd.ExecuteReader(); Country country = new Country(); while (reader.Read()) { country.Id = Convert.ToInt32(reader[0]); country.Name = Convert.ToString(reader[1]); } reader.Close(); if (country.Id == 0) { throw new CountryNotFoundException("Country with ID: {id} doesn't exist"); } // Getting the airlines: query = $"SELECT * FROM AirlineCompanies WHERE AirlineCompanies.COUNTRY_CODE = {countryId}"; cmd = new MySqlCommand(query, conn); reader = cmd.ExecuteReader(); while (reader.Read()) { AirlineCompany airlineCompany = new AirlineCompany(); airlineCompany.Id = Convert.ToInt32(reader[0]); airlineCompany.Name = Convert.ToString(reader[1]); airlineCompany.UserName = Convert.ToString(reader[2]); airlineCompany.Email = Convert.ToString(reader[3]); airlineCompany.Password = Convert.ToString(reader[4]); airlineCompany.CountryCode = Convert.ToInt32(reader[5]); airlineCompanies.Add(airlineCompany); } reader.Close(); }); return(airlineCompanies); }
public AirlineCompany Get(int key) { AirlineCompany airline = new AirlineCompany(); TryCatchDatabaseFunction(() => { string airlineJson = redisClient.Get <string>(key.ToString()); airline = JsonConvert.DeserializeObject <AirlineCompany>(airlineJson); }); return(airline); }
// Basic CRUD methods: public void Add(AirlineCompany t) { TryCatchDatabaseFunction((conn) => { if (!IsValidUser(t)) { return; } string query = $"INSERT INTO AirlineCompanies (AIRLINE_NAME, USER_NAME, EMAIL, PASSWORD, COUNTRY_CODE) VALUES " + $"('{t.Name}', '{t.UserName}', '{t.Email}', '{t.Password}', '{t.CountryCode}')"; MySqlCommand cmd = new MySqlCommand(query, conn); cmd.ExecuteNonQuery(); }); }
public void Add(AirlineCompany t) { TryCatchDatabaseFunction(() => { string airlineJson = JsonConvert.SerializeObject(t); // Searching for the first available key to store the value: int key = 0; while (!String.IsNullOrEmpty(redisClient.Get <string>(key.ToString()))) { key++; } // Storing the value: redisClient.Set(key.ToString(), airlineJson); }); }
public IList <AirlineCompany> GetAll() { List <AirlineCompany> airlines = new List <AirlineCompany>(); TryCatchDatabaseFunction(() => { List <string> keys = redisClient.GetAllKeys(); foreach (string key in keys) { string airlineJson = redisClient.Get <string>(key); AirlineCompany airline = JsonConvert.DeserializeObject <AirlineCompany>(airlineJson); airlines.Add(airline); } }); return(airlines); }
public bool TryAirlineLogin(string userName, string password, out LoginToken <AirlineCompany> token) { AirlineCompany airlineCompany = airlineDAOMSSQL.GetAirlineByUsername(userName); if (airlineCompany == null) { token = null; return(false); } else if (airlineCompany.Password != password) { throw new WrongPasswordException(); } else { token = new LoginToken <AirlineCompany>(); token.User = airlineCompany; return(true); } }
public void Remove(AirlineCompany t) { TryCatchDatabaseFunction(() => { string thisAirline = JsonConvert.SerializeObject(t); List <string> keys = redisClient.GetAllKeys(); foreach (string key in keys) { string airlineJson = redisClient.Get <string>(key); if (airlineJson == thisAirline) { bool success = redisClient.Remove(key); if (!success) { throw new DataConnectorException($"An error occurred while trying to remove key '{key}' from Redis cache."); } } } }); }
public void Update(AirlineCompany t) { TryCatchDatabaseFunction((conn) => { if (!IsValidUser(t)) { return; } // Checks if an airline company with that ID exists: string query = $"SELECT * FROM AirlineCompanies WHERE AirlineCompanies.ID = {t.Id}"; MySqlCommand cmd = new MySqlCommand(query, conn); MySqlDataReader reader = cmd.ExecuteReader(); AirlineCompany airlineCompany = new AirlineCompany(); while (reader.Read()) { airlineCompany.Id = Convert.ToInt32(reader[0]); } reader.Close(); if (airlineCompany.Id == 0) { throw new AirlineCompanyNotFoundException($"Airline Company with ID: {t.Id} doesn't exist"); } // Updates the airline details: query = $"UPDATE AirlineCompanies SET AIRLINE_NAME = '{t.Name}', USER_NAME = '{t.UserName}', EMAIL = '{t.Email}', PASSWORD = '******', " + $"COUNTRY_CODE = '{t.CountryCode}' WHERE AirlineCompanies.ID = {t.Id}"; cmd = new MySqlCommand(query, conn); cmd.ExecuteNonQuery(); }); }
public void ModifyAirlineDetails(LoginToken <AirlineCompany> token, AirlineCompany airline) { CheckToken(token); airlineDAO.Update(airline); Logger.Log(LogLevel.Info, $"The airline company '{token.User.Name}' has updated its details."); }
public void AddAirlineToRegisterQueue(AirlineCompany airline) { Logger.Log(LogLevel.Info, $"A new airline company ('{airline.Name}') has completed the registration and added to the approval queue."); airlineRedisDAO.Add(airline); }
public void UpdateAirlineDetails(LoginToken <Administrator> token, AirlineCompany airline) { CheckToken(token); airlineDAO.Update(airline); Logger.Log(LogLevel.Info, $"The administrator updated the airline account of '{airline.Name}'."); }
public void RemoveAirlineFromRegisterQueue(LoginToken <Administrator> token, AirlineCompany airline) { CheckToken(token); airlineRedisDAO.Remove(airline); }
public void RemoveAirline(LoginToken <Administrator> token, AirlineCompany airline) { CheckToken(token); airlineDAO.Remove(airline.Id); Logger.Log(LogLevel.Info, $"The administrator removed the airline company account of '{airline.Name}'."); }
public void CreateNewAirline(LoginToken <Administrator> token, AirlineCompany airline) { CheckToken(token); airlineDAO.Add(airline); Logger.Log(LogLevel.Info, $"The airline company '{airline.Name}' was aprroved by the administrator and an account was created for it"); }
/// <summary> /// This method checks if the given flight is valid and has all the required properties. /// </summary> public bool IsValidFlight(Flight t) { if (t.AirlineCompanyId == 0) { throw new InvalidFlightException("A flight must have an airline company"); } if (t.OriginCountryCode == 0) { throw new InvalidFlightException("A flight must have an origin country"); } if (t.DestinationCountryCode == 0) { throw new InvalidFlightException("A flight must have a destination country"); } if (t.DepartureTime.Year == 1) { throw new InvalidFlightException("A flight must have a departure time and date"); } if (t.LandingTime.Year == 1) { throw new InvalidFlightException("A flight must have a landing time and date"); } // Checking if the airline company exists: MySqlConnection conn = new MySqlConnection(connectionString); conn.Open(); string query = $"SELECT * FROM AirlineCompanies WHERE AirlineCompanies.ID = {t.AirlineCompanyId}"; MySqlCommand cmd = new MySqlCommand(query, conn); MySqlDataReader reader = cmd.ExecuteReader(); AirlineCompany airlineCompany = new AirlineCompany(); while (reader.Read()) { airlineCompany.Id = Convert.ToInt32(reader[0]); } reader.Close(); if (airlineCompany.Id == 0) { throw new AirlineCompanyNotFoundException($"Airline Company with ID: {t.AirlineCompanyId} doesn't exist"); } // Checking if the origin country exists: query = $"SELECT * FROM Countries WHERE Countries.ID = {t.OriginCountryCode}"; cmd = new MySqlCommand(query, conn); reader = cmd.ExecuteReader(); Country country = new Country(); while (reader.Read()) { country.Id = Convert.ToInt32(reader[0]); } reader.Close(); if (country.Id == 0) { throw new CountryNotFoundException($"Country with ID: {t.OriginCountryCode} doesn't exist"); } // Checking if the destination country exists: query = $"SELECT * FROM Countries WHERE Countries.ID = {t.DestinationCountryCode}"; cmd = new MySqlCommand(query, conn); reader = cmd.ExecuteReader(); country = new Country(); while (reader.Read()) { country.Id = Convert.ToInt32(reader[0]); } reader.Close(); if (country.Id == 0) { throw new CountryNotFoundException($"Country with ID: {t.DestinationCountryCode} doesn't exist"); } conn.Close(); return(true); }
/// <summary> /// This method checks if the given airline company is valid and has all the required properties. /// </summary> public bool IsValidUser(AirlineCompany t) { // Validating the fields: if (String.IsNullOrEmpty(t.Name)) { throw new InvalidAirlineCompanyException("Airline company must have a name."); } if (String.IsNullOrEmpty(t.UserName)) { throw new InvalidAirlineCompanyException("Airline company user must have a user name."); } if (String.IsNullOrEmpty(t.Email)) { throw new InvalidAirlineCompanyException("Airline company user must have an email"); } if (String.IsNullOrEmpty(t.Password)) { throw new InvalidAirlineCompanyException("Airline company user must have a password."); } if (t.CountryCode == 0) { throw new InvalidAirlineCompanyException("Airline company must have an origin country"); } if (t.UserName == GlobalConfig.adminUserName) { throw new UserNameAlreadyExistException("This user name is already taken."); } // Checking if the user name is taken or not: MySqlConnection conn = new MySqlConnection(connectionString); conn.Open(); int customerId = 0; int airlineId = 0; string query = $"SELECT Customers.ID from Customers WHERE Customers.USER_NAME = '{t.UserName}'"; MySqlCommand cmd = new MySqlCommand(query, conn); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { customerId = Convert.ToInt32(reader[0]); } reader.Close(); query = $"SELECT AirlineCompanies.ID from AirlineCompanies WHERE AirlineCompanies.USER_NAME = '{t.UserName}'"; cmd = new MySqlCommand(query, conn); reader = cmd.ExecuteReader(); while (reader.Read()) { if (Convert.ToInt32(reader[0]) != t.Id) { airlineId = Convert.ToInt32(reader[0]); } } reader.Close(); if (customerId != 0) { throw new UserNameAlreadyExistException("This user name is already taken."); } if (airlineId != 0 && airlineId != t.Id) { throw new UserNameAlreadyExistException("This user name is already taken."); } // Checking if the company name is taken or not: query = $"SELECT AirlineCompanies.ID from AirlineCompanies WHERE AirlineCompanies.AIRLINE_NAME = '{t.Name}'"; cmd = new MySqlCommand(query, conn); reader = cmd.ExecuteReader(); while (reader.Read()) { if (Convert.ToInt32(reader[0]) != t.Id) { airlineId = Convert.ToInt32(reader[0]); } } reader.Close(); if (airlineId != 0) { throw new AirlineCompanyNameAlreadyExistException("This airline comapny name already exist."); } // Checking if the origin country exists: query = $"SELECT * FROM Countries WHERE Countries.ID = {t.CountryCode}"; cmd = new MySqlCommand(query, conn); reader = cmd.ExecuteReader(); Country country = new Country(); while (reader.Read()) { country.Id = Convert.ToInt32(reader[0]); } reader.Close(); if (country.Id == 0) { throw new CountryNotFoundException($"Country with ID: {t.CountryCode} doesn't exist"); } conn.Close(); return(true); }
public void Remove(int id) { TryCatchDatabaseFunction((conn) => { // Checks if an airline company with that ID exists: string query = $"SELECT * FROM AirlineCompanies WHERE AirlineCompanies.ID = {id}"; MySqlCommand cmd = new MySqlCommand(query, conn); MySqlDataReader reader = cmd.ExecuteReader(); AirlineCompany airlineCompany = new AirlineCompany(); while (reader.Read()) { airlineCompany.Id = Convert.ToInt32(reader[0]); } reader.Close(); if (airlineCompany.Id == 0) { throw new AirlineCompanyNotFoundException($"Airline Company with ID: {id} doesn't exist"); } // Deleting all airline's flights: query = $"SELECT * FROM Flights WHERE Flights.AIRLINE_COMPANY_ID = {id}"; cmd = new MySqlCommand(query, conn); reader = cmd.ExecuteReader(); List <Flight> flights = new List <Flight>(); while (reader.Read()) { Flight flight = new Flight(); flight.Id = Convert.ToInt32(reader[0]); flights.Add(flight); } reader.Close(); foreach (Flight flight in flights) { query = $"DELETE FROM Tickets WHERE Tickets.FLIGHT_ID = {flight.Id}"; cmd = new MySqlCommand(query, conn); cmd.ExecuteNonQuery(); query = $"DELETE FROM Flights WHERE Flights.ID = {flight.Id}"; cmd = new MySqlCommand(query, conn); cmd.ExecuteNonQuery(); } // Deleting the airline: query = $"DELETE FROM AirlineCompanies WHERE AirlineCompanies.ID = {id}"; cmd = new MySqlCommand(query, conn); cmd.ExecuteNonQuery(); }); }