Finish Add/Update Customers Form

This commit is contained in:
Spudnut2000 2025-06-19 12:26:24 -05:00
parent d26498cd3c
commit 8b504a9c79
6 changed files with 55 additions and 64 deletions

View File

@ -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;

View File

@ -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)

View File

@ -13,7 +13,7 @@ public partial class DashboardForm : Form
CustomersButton.Click += (sender, args) =>
{
_recordsForm.ShowDialog();
_recordsForm.UpdateCustomersList();
_recordsForm.UpdateCustomersList(null, EventArgs.Empty);
};

View File

@ -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<Customer> RetrieveCustomers()
{
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
@ -83,12 +79,7 @@ namespace C969Project.Data
return customers;
}
/// <summary>
/// Adds a new customer record to the database and returns the generated customerId.
/// </summary>
/// <param name="customer">The customer object to add.</param>
/// <returns>The ID of the newly added customer; otherwise, 0 if an error occurs.</returns>
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;
}
/// <summary>
/// Adds a new address to the database and returns the generated addressId.
/// </summary>
/// <param name="address">The address object to add.</param>
/// <returns>The ID of the newly added address; otherwise, 0 if an error occurs.</returns>
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
}
}

View File

@ -4,18 +4,12 @@ namespace C969Project
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[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());
}

View File

@ -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();