cleanup database helper
This commit is contained in:
parent
0bcfa4665c
commit
48a2bd21de
@ -68,35 +68,6 @@ public static class DatabaseHelper
|
|||||||
return users;
|
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
|
#region Customers
|
||||||
public static List<Customer> RetrieveCustomers()
|
public static List<Customer> RetrieveCustomers()
|
||||||
{
|
{
|
||||||
@ -311,45 +282,6 @@ public static class DatabaseHelper
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Address> 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<Address>();
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Address> addresses = new List<Address>();
|
|
||||||
|
|
||||||
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)
|
public static int AddAddress(Address address)
|
||||||
{
|
{
|
||||||
int addressId = 0;
|
int addressId = 0;
|
||||||
@ -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
|
#endregion
|
||||||
|
|
||||||
#region Appointments
|
#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<Appointment> RetrieveAppointments(int? userId = null)
|
public static List<Appointment> RetrieveAppointments(int? userId = null)
|
||||||
{
|
{
|
||||||
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
|
||||||
@ -696,42 +556,6 @@ public static class DatabaseHelper
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<City> 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<City>();
|
|
||||||
}
|
|
||||||
|
|
||||||
List<City> cities = new List<City>();
|
|
||||||
|
|
||||||
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)
|
public static int GetCityId(string cityName, int countryId)
|
||||||
{
|
{
|
||||||
int cityId = 0;
|
int cityId = 0;
|
||||||
@ -798,135 +622,9 @@ public static class DatabaseHelper
|
|||||||
return cityId;
|
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
|
#endregion
|
||||||
|
|
||||||
#region Countries
|
#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<Country> 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<Country>();
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Country> countries = new List<Country>();
|
|
||||||
|
|
||||||
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)
|
public static int GetCountryId(string countryName)
|
||||||
{
|
{
|
||||||
@ -992,60 +690,6 @@ public static class DatabaseHelper
|
|||||||
return countryId;
|
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
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user