From 8b504a9c7999c390ea82a5667a18eabde2dfd4d5 Mon Sep 17 00:00:00 2001 From: Spudnut2000 Date: Thu, 19 Jun 2025 12:26:24 -0500 Subject: [PATCH] Finish Add/Update Customers Form --- C969Project/AddUpdateCustomerForm.Designer.cs | 18 ++--- C969Project/AddUpdateCustomerForm.cs | 13 ++-- C969Project/DashboardForm.cs | 2 +- C969Project/Data/DatabaseHelper.cs | 69 +++++++++---------- C969Project/Program.cs | 6 -- C969Project/RecordsForm.cs | 11 +-- 6 files changed, 55 insertions(+), 64 deletions(-) diff --git a/C969Project/AddUpdateCustomerForm.Designer.cs b/C969Project/AddUpdateCustomerForm.Designer.cs index 1ee00d8..2c8b484 100644 --- a/C969Project/AddUpdateCustomerForm.Designer.cs +++ b/C969Project/AddUpdateCustomerForm.Designer.cs @@ -46,7 +46,7 @@ partial class AddUpdateCustomerForm label7 = new System.Windows.Forms.Label(); zipTextBox = new System.Windows.Forms.TextBox(); label8 = new System.Windows.Forms.Label(); - textBox1 = new System.Windows.Forms.TextBox(); + countryTextBox = new System.Windows.Forms.TextBox(); SuspendLayout(); // // label1 @@ -182,13 +182,13 @@ partial class AddUpdateCustomerForm label8.Text = "Country"; label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // - // textBox1 + // countryTextBox // - textBox1.Location = new System.Drawing.Point(12, 299); - textBox1.Name = "textBox1"; - textBox1.Size = new System.Drawing.Size(218, 23); - textBox1.TabIndex = 14; - textBox1.Text = "US"; + countryTextBox.Location = new System.Drawing.Point(12, 299); + countryTextBox.Name = "countryTextBox"; + countryTextBox.Size = new System.Drawing.Size(218, 23); + countryTextBox.TabIndex = 14; + countryTextBox.Text = "US"; // // AddUpdateCustomerForm // @@ -197,7 +197,7 @@ partial class AddUpdateCustomerForm BackColor = System.Drawing.SystemColors.Control; ClientSize = new System.Drawing.Size(800, 450); Controls.Add(label8); - Controls.Add(textBox1); + Controls.Add(countryTextBox); Controls.Add(label7); Controls.Add(zipTextBox); Controls.Add(label6); @@ -219,7 +219,7 @@ partial class AddUpdateCustomerForm } private System.Windows.Forms.Label label8; - private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.TextBox countryTextBox; private System.Windows.Forms.Label label6; private System.Windows.Forms.TextBox cityTextBox; diff --git a/C969Project/AddUpdateCustomerForm.cs b/C969Project/AddUpdateCustomerForm.cs index 09c0a8b..6848849 100644 --- a/C969Project/AddUpdateCustomerForm.cs +++ b/C969Project/AddUpdateCustomerForm.cs @@ -8,6 +8,8 @@ namespace C969Project public partial class AddUpdateCustomerForm : Form { private Customer? _currentCustomer; + + public event EventHandler UpdateCustomersList; public AddUpdateCustomerForm() { @@ -61,7 +63,8 @@ namespace C969Project string.IsNullOrWhiteSpace(addressTextBox.Text) || string.IsNullOrWhiteSpace(cityTextBox.Text) || string.IsNullOrWhiteSpace(zipTextBox.Text) || - string.IsNullOrWhiteSpace(phoneTextBox.Text)) + string.IsNullOrWhiteSpace(phoneTextBox.Text) || + string.IsNullOrWhiteSpace(countryTextBox.Text)) { MessageBox.Show("All fields must be filled out.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; @@ -72,14 +75,12 @@ namespace C969Project MessageBox.Show("Phone number can only contain digits and dashes.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } - - + string currentUser = AppState.CurrentUser.Username; try { - - string countryName = "US"; + string countryName = countryTextBox.Text; int currentCountryId = DatabaseHelper.GetCountryId(countryName); if (currentCountryId == 0) { @@ -183,6 +184,8 @@ namespace C969Project // TODO: Implement robust exception handling, logging, and user notification as per assignment A2b MessageBox.Show($"An error occurred while saving the customer: {ex.Message}", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } + + UpdateCustomersList?.Invoke(this, EventArgs.Empty); } private void CancelButton_Click(object sender, EventArgs e) diff --git a/C969Project/DashboardForm.cs b/C969Project/DashboardForm.cs index 21a3e5e..5ea2823 100644 --- a/C969Project/DashboardForm.cs +++ b/C969Project/DashboardForm.cs @@ -13,7 +13,7 @@ public partial class DashboardForm : Form CustomersButton.Click += (sender, args) => { _recordsForm.ShowDialog(); - _recordsForm.UpdateCustomersList(); + _recordsForm.UpdateCustomersList(null, EventArgs.Empty); }; diff --git a/C969Project/Data/DatabaseHelper.cs b/C969Project/Data/DatabaseHelper.cs index 7c812d6..01f3c31 100644 --- a/C969Project/Data/DatabaseHelper.cs +++ b/C969Project/Data/DatabaseHelper.cs @@ -1,5 +1,5 @@ using System; -using System.Data; // For CommandType +using System.Data; using MySql.Data.MySqlClient; using C969Project.Data.Models; @@ -8,11 +8,6 @@ namespace C969Project.Data public static class DatabaseHelper { private static string ConnectionString => AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"); - - public static void Initialize() - { - // Initialization logic if any - } public static User? Login(string username, string password) { @@ -47,6 +42,7 @@ namespace C969Project.Data return null; } + #region Customers public static List RetrieveCustomers() { using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); @@ -83,12 +79,7 @@ namespace C969Project.Data return customers; } - - /// - /// Adds a new customer record to the database and returns the generated customerId. - /// - /// The customer object to add. - /// The ID of the newly added customer; otherwise, 0 if an error occurs. + public static int AddCustomer(Customer customer) { int customerId = 0; @@ -97,11 +88,10 @@ namespace C969Project.Data { connection.Open(); - // Removed customerId from INSERT statement as it's auto-incremented string query = "INSERT INTO customer (customerName, addressId, active, createDate, createdBy, lastUpdate, lastUpdateBy) " + - "VALUES (@customerName, @addressId, @active, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate - "SELECT LAST_INSERT_ID();"; // Get the newly generated ID + "VALUES (@customerName, @addressId, @active, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + + "SELECT LAST_INSERT_ID();"; using MySqlCommand command = new MySqlCommand(query, connection); @@ -110,7 +100,7 @@ namespace C969Project.Data command.Parameters.AddWithValue("@active", customer.Active); command.Parameters.AddWithValue("@createDate", DateTime.UtcNow); command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); // lastUpdateBy is updated manually + command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); object result = command.ExecuteScalar(); if (result != null && result != DBNull.Value) @@ -140,14 +130,14 @@ namespace C969Project.Data string query = "UPDATE customer SET customerName = @customerName, addressId = @addressId, active = @active, " + - "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE customerId = @customerId"; // Use CURRENT_TIMESTAMP for lastUpdate + "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE customerId = @customerId"; using MySqlCommand command = new MySqlCommand(query, connection); command.Parameters.AddWithValue("@customerName", customer.CustomerName); command.Parameters.AddWithValue("@addressId", customer.AddressId); command.Parameters.AddWithValue("@active", customer.Active); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); // lastUpdateBy is updated manually + command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); command.Parameters.AddWithValue("@customerId", customer.CustomerId); command.ExecuteNonQuery(); @@ -191,7 +181,10 @@ namespace C969Project.Data { DeleteCustomer(customer.CustomerId); } + #endregion + #region Addresses + public static Address? RetrieveAddress(int addressId) { using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); @@ -271,12 +264,7 @@ namespace C969Project.Data return addresses; } - - /// - /// Adds a new address to the database and returns the generated addressId. - /// - /// The address object to add. - /// The ID of the newly added address; otherwise, 0 if an error occurs. + public static int AddAddress(Address address) { int addressId = 0; @@ -285,11 +273,10 @@ namespace C969Project.Data { connection.Open(); - // Removed addressId from INSERT statement as it's auto-incremented string query = "INSERT INTO address (address, address2, cityId, postalCode, phone, createDate, createdBy, lastUpdate, lastUpdateBy) " + - "VALUES (@address1, @address2, @cityId, @postalCode, @phone, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate - "SELECT LAST_INSERT_ID();"; // Get the newly generated ID + "VALUES (@address1, @address2, @cityId, @postalCode, @phone, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + + "SELECT LAST_INSERT_ID();"; using MySqlCommand command = new MySqlCommand(query, connection); @@ -330,7 +317,7 @@ namespace C969Project.Data string query = "UPDATE address SET address = @address1, address2 = @address2, cityId = @cityId, postalCode = @postalCode, " + - "phone = @phone, lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId"; // Use CURRENT_TIMESTAMP for lastUpdate + "phone = @phone, lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId"; using MySqlCommand command = new MySqlCommand(query, connection); @@ -384,6 +371,10 @@ namespace C969Project.Data DeleteAddress(address.Id); } + #endregion + + #region Appointments + public static Appointment? RetrieveAppointment(int appointmentId) { using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); @@ -587,6 +578,9 @@ namespace C969Project.Data DeleteAppointment(appointment.AppointmentId); } + #endregion + + #region Cities public static City? RetrieveCity(int cityId) { using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); @@ -684,7 +678,6 @@ namespace C969Project.Data } catch (Exception ex) { - // TODO: Implement robust exception handling, logging, and user notification as per assignment A2b/A3b. Console.WriteLine($"Error getting city ID: {ex.Message}"); } return cityId; @@ -698,11 +691,10 @@ namespace C969Project.Data { connection.Open(); - // Removed cityId from INSERT statement as it's auto-incremented string query = "INSERT INTO city (city, countryId, createDate, createdBy, lastUpdate, lastUpdateBy) " + - "VALUES (@cityName, @countryId, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate - "SELECT LAST_INSERT_ID();"; // Get the newly generated ID + "VALUES (@cityName, @countryId, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + + "SELECT LAST_INSERT_ID();"; using MySqlCommand command = new MySqlCommand(query, connection); @@ -740,7 +732,7 @@ namespace C969Project.Data string query = "UPDATE city SET city = @cityName, countryId = @countryId, " + - "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE cityId = @cityId"; // Use CURRENT_TIMESTAMP for lastUpdate + "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE cityId = @cityId"; using MySqlCommand command = new MySqlCommand(query, connection); @@ -790,7 +782,9 @@ namespace C969Project.Data { DeleteCity(city.CityID); } + #endregion + #region Countries public static Country? RetrieveCountry(int countryId) { using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); @@ -885,7 +879,6 @@ namespace C969Project.Data } catch (Exception ex) { - // TODO: Implement robust exception handling, logging, and user notification as per assignment A2b/A3b. Console.WriteLine($"Error getting country ID: {ex.Message}"); } return countryId; @@ -899,11 +892,10 @@ namespace C969Project.Data { connection.Open(); - // Removed countryId from INSERT statement as it's auto-incremented string query = "INSERT INTO country (country, createDate, createdBy, lastUpdate, lastUpdateBy) " + - "VALUES (@countryName, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + // Use CURRENT_TIMESTAMP for lastUpdate - "SELECT LAST_INSERT_ID();"; // Get the newly generated ID + "VALUES (@countryName, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + + "SELECT LAST_INSERT_ID();"; using MySqlCommand command = new MySqlCommand(query, connection); @@ -940,7 +932,7 @@ namespace C969Project.Data string query = "UPDATE country SET country = @countryName, " + - "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE countryId = @countryId"; // Use CURRENT_TIMESTAMP for lastUpdate + "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE countryId = @countryId"; using MySqlCommand command = new MySqlCommand(query, connection); @@ -989,5 +981,6 @@ namespace C969Project.Data { DeleteCountry(country.CountryID); } + #endregion } } \ No newline at end of file diff --git a/C969Project/Program.cs b/C969Project/Program.cs index 79b06c3..b1e651b 100644 --- a/C969Project/Program.cs +++ b/C969Project/Program.cs @@ -4,18 +4,12 @@ namespace C969Project { internal static class Program { - /// - /// The main entry point for the application. - /// [STAThread] static void Main() { AppSettings.Initialize(); Localization.Initialize(); - DatabaseHelper.Initialize(); - // To customize application configuration such as set high DPI settings or default font, - // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); Application.Run(new LoginForm()); } diff --git a/C969Project/RecordsForm.cs b/C969Project/RecordsForm.cs index 788a7f0..0925dca 100644 --- a/C969Project/RecordsForm.cs +++ b/C969Project/RecordsForm.cs @@ -11,19 +11,20 @@ public partial class RecordsForm : Form public RecordsForm() { InitializeComponent(); - UpdateCustomersList(); + UpdateCustomersList(null, EventArgs.Empty); AddButton.Click += (sender, args) => { _form.InitAdd(); - _form.Closed += (o, args2) => { UpdateCustomersList(); }; + _form.UpdateCustomersList += UpdateCustomersList; }; ModifyButton.Click += (sender, args) => { + if (_customers is null) return; var selectedCustomer = _customers[CustomersDataGrid.CurrentCell.RowIndex]; _form.InitUpdate(selectedCustomer); - _form.Closed += (o, args2) => { UpdateCustomersList(); }; + _form.UpdateCustomersList += UpdateCustomersList; }; DeleteButton.Click += (sender, args) => @@ -34,14 +35,14 @@ public partial class RecordsForm : Form { _customers.Remove(selectedCustomer); DatabaseHelper.DeleteCustomer(selectedCustomer); - UpdateCustomersList(); + UpdateCustomersList(null, EventArgs.Empty); } }; } - public void UpdateCustomersList() + public void UpdateCustomersList(object? sender, EventArgs args) { if (_customers != null && _customers.Count > 0) _customers.Clear(); _customers = DatabaseHelper.RetrieveCustomers();