diff --git a/C969Project/Data/DatabaseHelper.cs b/C969Project/Data/DatabaseHelper.cs index 3445ad0..dbcb29f 100644 --- a/C969Project/Data/DatabaseHelper.cs +++ b/C969Project/Data/DatabaseHelper.cs @@ -68,35 +68,6 @@ public static class DatabaseHelper return users; } - public static User? RetrieveUser(int id) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - } - catch (MySqlException e) - { - MessageBox.Show($"Database connection error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - return null; - } - - string query = "SELECT * FROM client_schedule.user WHERE userId = @userId"; - using MySqlCommand command = new MySqlCommand(query, connection); - command.Parameters.AddWithValue("@userId", id); - - using MySqlDataReader reader = command.ExecuteReader(); - - User? user = null; - - while (reader.Read()) - { - user = new User(reader.GetInt32("userId"), reader.GetString("userName")); - } - - return user ?? null; - } - #region Customers public static List RetrieveCustomers() { @@ -310,45 +281,6 @@ public static class DatabaseHelper return null; } - - public static List
RetrieveAllAddresses() - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - } - catch (MySqlException e) - { - MessageBox.Show($"Database connection error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - return new List
(); - } - - List
addresses = new List
(); - - using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.address", connection); - using MySqlDataReader reader = command.ExecuteReader(); - while (reader.Read()) - { - var addr = new Address() - { - Id = reader.GetInt32("addressId"), - Address1 = reader.GetString("address"), - Address2 = reader.GetString("address2"), - CityId = reader.GetInt32("cityId"), - PostalCode = reader.GetString("postalCode"), - Phone = reader.GetString("phone"), - CreateDate = reader.GetDateTime("createDate"), - CreatedBy = reader.GetString("createdBy"), - LastUpdate = reader.GetDateTime("lastUpdate"), - LastUpdateBy = reader.GetString("lastUpdateBy"), - }; - - addresses.Add(addr); - } - - return addresses; - } public static int AddAddress(Address address) { @@ -424,82 +356,10 @@ public static class DatabaseHelper } } - public static void DeleteAddress(int addressId) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - string query = "DELETE FROM address WHERE addressId = @addressId"; - using MySqlCommand command = new MySqlCommand(query, connection); - command.Parameters.AddWithValue("@addressId", addressId); - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - MessageBox.Show($"MySQL Error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - catch (Exception ex) - { - MessageBox.Show($"General Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - public static void DeleteAddress(Address address) - { - DeleteAddress(address.Id); - } - #endregion #region Appointments - - public static Appointment? RetrieveAppointment(int appointmentId) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - } - catch (MySqlException e) - { - MessageBox.Show($"Database connection error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - return null; - } - - string query = "SELECT * FROM client_schedule.appointment WHERE appointmentId = @appointmentId"; - using MySqlCommand command = new MySqlCommand(query, connection); - command.Parameters.AddWithValue("@appointmentId", appointmentId); - - using MySqlDataReader reader = command.ExecuteReader(); - - while (reader.Read()) - { - var appt = new Appointment() - { - AppointmentId = reader.GetInt32("appointmentId"), - CustomerId = reader.GetInt32("customerId"), - UserId = reader.GetInt32("userId"), - Title = reader.GetString("title"), - Description = reader.GetString("description"), - Location = reader.GetString("location"), - Contact = reader.GetString("contact"), - AppointmentType = reader.GetString("type"), - Url = reader.GetString("url"), - Start = reader.GetDateTime("start"), - End = reader.GetDateTime("end"), - CreateDate = reader.GetDateTime("createDate"), - CreatedBy = reader.GetString("createdBy"), - LastUpdate = reader.GetDateTime("lastUpdate"), - LastUpdateBy = reader.GetString("lastUpdateBy"), - }; - - return appt; - } - - return null; - } - + public static List RetrieveAppointments(int? userId = null) { using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); @@ -695,42 +555,6 @@ public static class DatabaseHelper return null; } - - public static List RetrieveAllCities() - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - } - catch (MySqlException e) - { - MessageBox.Show($"Database connection error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - return new List(); - } - - List cities = new List(); - - using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.city", connection); - using MySqlDataReader reader = command.ExecuteReader(); - while (reader.Read()) - { - var city = new City() - { - CityID = reader.GetInt32("cityId"), - CityName = reader.GetString("city"), - CountryID = reader.GetInt32("countryId"), - CreateDate = reader.GetDateTime("createDate"), - CreatedBy = reader.GetString("createdBy"), - LastUpdate = reader.GetDateTime("lastUpdate"), - LastUpdateBy = reader.GetString("lastUpdateBy"), - }; - - cities.Add(city); - } - - return cities; - } public static int GetCityId(string cityName, int countryId) { @@ -797,136 +621,10 @@ public static class DatabaseHelper } return cityId; } - - public static void UpdateCity(City city) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - - string query = - "UPDATE city SET city = @cityName, countryId = @countryId, " + - "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE cityId = @cityId"; - - using MySqlCommand command = new MySqlCommand(query, connection); - - command.Parameters.AddWithValue("@cityName", city.CityName); - command.Parameters.AddWithValue("@countryId", city.CountryID); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); - command.Parameters.AddWithValue("@cityId", city.CityID); - - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - MessageBox.Show($"MySQL Error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - catch (Exception ex) - { - MessageBox.Show($"General Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - public static void DeleteCity(int cityId) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - string query = "DELETE FROM city WHERE cityId = @cityId"; - using MySqlCommand command = new MySqlCommand(query, connection); - command.Parameters.AddWithValue("@cityId", cityId); - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - MessageBox.Show($"MySQL Error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - catch (Exception ex) - { - MessageBox.Show($"General Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - public static void DeleteCity(City city) - { - DeleteCity(city.CityID); - } + #endregion #region Countries - public static Country? RetrieveCountry(int countryId) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - } - catch (MySqlException e) - { - MessageBox.Show($"Database connection error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - return null; - } - - string query = "SELECT * FROM client_schedule.country WHERE countryId = @countryId"; - using MySqlCommand command = new MySqlCommand(query, connection); - command.Parameters.AddWithValue("@countryId", countryId); - - using MySqlDataReader reader = command.ExecuteReader(); - - while (reader.Read()) - { - var country = new Country() - { - CountryID = reader.GetInt32("countryId"), - CountryName = reader.GetString("country"), - CreateDate = reader.GetDateTime("createDate"), - CreatedBy = reader.GetString("createdBy"), - LastUpdate = reader.GetDateTime("lastUpdate"), - LastUpdateBy = reader.GetString("lastUpdateBy"), - }; - - return country; - } - - return null; - } - - public static List RetrieveAllCountries() - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - } - catch (MySqlException e) - { - MessageBox.Show($"Database connection error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - return new List(); - } - - List countries = new List(); - - using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.country", connection); - using MySqlDataReader reader = command.ExecuteReader(); - while (reader.Read()) - { - var country = new Country() - { - CountryID = reader.GetInt32("countryId"), - CountryName = reader.GetString("country"), - CreateDate = reader.GetDateTime("createDate"), - CreatedBy = reader.GetString("createdBy"), - LastUpdate = reader.GetDateTime("lastUpdate"), - LastUpdateBy = reader.GetString("lastUpdateBy"), - }; - - countries.Add(country); - } - - return countries; - } public static int GetCountryId(string countryName) { @@ -991,61 +689,7 @@ public static class DatabaseHelper } return countryId; } - - public static void UpdateCountry(Country country) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - - string query = - "UPDATE country SET country = @countryName, " + - "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE countryId = @countryId"; - - using MySqlCommand command = new MySqlCommand(query, connection); - - command.Parameters.AddWithValue("@countryName", country.CountryName); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); - command.Parameters.AddWithValue("@countryId", country.CountryID); - - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - MessageBox.Show($"MySQL Error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - catch (Exception ex) - { - MessageBox.Show($"General Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - public static void DeleteCountry(int countryId) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - string query = "DELETE FROM country WHERE countryId = @countryId"; - using MySqlCommand command = new MySqlCommand(query, connection); - command.Parameters.AddWithValue("@countryId", countryId); - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - MessageBox.Show($"MySQL Error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - catch (Exception ex) - { - MessageBox.Show($"General Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - public static void DeleteCountry(Country country) - { - DeleteCountry(country.CountryID); - } + #endregion } \ No newline at end of file