ability to add and update customers
This commit is contained in:
parent
a6da14e24f
commit
d26498cd3c
25
C969Project/AddUpdateCustomerForm.Designer.cs
generated
25
C969Project/AddUpdateCustomerForm.Designer.cs
generated
@ -45,6 +45,8 @@ partial class AddUpdateCustomerForm
|
||||
cityTextBox = new System.Windows.Forms.TextBox();
|
||||
label7 = new System.Windows.Forms.Label();
|
||||
zipTextBox = new System.Windows.Forms.TextBox();
|
||||
label8 = new System.Windows.Forms.Label();
|
||||
textBox1 = new System.Windows.Forms.TextBox();
|
||||
SuspendLayout();
|
||||
//
|
||||
// label1
|
||||
@ -170,12 +172,32 @@ partial class AddUpdateCustomerForm
|
||||
zipTextBox.Size = new System.Drawing.Size(218, 23);
|
||||
zipTextBox.TabIndex = 12;
|
||||
//
|
||||
// label8
|
||||
//
|
||||
label8.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)0));
|
||||
label8.Location = new System.Drawing.Point(236, 299);
|
||||
label8.Name = "label8";
|
||||
label8.Size = new System.Drawing.Size(63, 23);
|
||||
label8.TabIndex = 15;
|
||||
label8.Text = "Country";
|
||||
label8.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
||||
//
|
||||
// textBox1
|
||||
//
|
||||
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";
|
||||
//
|
||||
// AddUpdateCustomerForm
|
||||
//
|
||||
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
BackColor = System.Drawing.SystemColors.Control;
|
||||
ClientSize = new System.Drawing.Size(800, 450);
|
||||
Controls.Add(label8);
|
||||
Controls.Add(textBox1);
|
||||
Controls.Add(label7);
|
||||
Controls.Add(zipTextBox);
|
||||
Controls.Add(label6);
|
||||
@ -196,6 +218,9 @@ partial class AddUpdateCustomerForm
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
private System.Windows.Forms.Label label8;
|
||||
private System.Windows.Forms.TextBox textBox1;
|
||||
|
||||
private System.Windows.Forms.Label label6;
|
||||
private System.Windows.Forms.TextBox cityTextBox;
|
||||
private System.Windows.Forms.Label label7;
|
||||
|
@ -1,44 +1,194 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using C969Project.Data;
|
||||
using C969Project.Data.Models;
|
||||
|
||||
namespace C969Project;
|
||||
|
||||
public partial class AddUpdateCustomerForm : Form
|
||||
namespace C969Project
|
||||
{
|
||||
public AddUpdateCustomerForm()
|
||||
public partial class AddUpdateCustomerForm : Form
|
||||
{
|
||||
InitializeComponent();
|
||||
private Customer? _currentCustomer;
|
||||
|
||||
public AddUpdateCustomerForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
saveButton.Click += SaveButton_Click;
|
||||
cancelButton.Click += CancelButton_Click;
|
||||
}
|
||||
|
||||
public void InitAdd()
|
||||
{
|
||||
Text = "Add Customer";
|
||||
label1.Text = "Add Customer";
|
||||
|
||||
nameTextBox.Text = string.Empty;
|
||||
addressTextBox.Text = string.Empty;
|
||||
cityTextBox.Text = string.Empty;
|
||||
zipTextBox.Text = string.Empty;
|
||||
phoneTextBox.Text = string.Empty;
|
||||
_currentCustomer = null;
|
||||
|
||||
ShowDialog();
|
||||
}
|
||||
|
||||
public void InitUpdate(Customer customer)
|
||||
{
|
||||
Text = "Update Customer";
|
||||
label1.Text = "Update Customer";
|
||||
|
||||
_currentCustomer = customer;
|
||||
|
||||
nameTextBox.Text = customer.CustomerName;
|
||||
var addr = DatabaseHelper.RetrieveAddress(customer.AddressId);
|
||||
if (addr != null)
|
||||
{
|
||||
addressTextBox.Text = addr.Address1;
|
||||
var city = DatabaseHelper.RetrieveCity(addr.CityId);
|
||||
if (city != null)
|
||||
{
|
||||
cityTextBox.Text = city.CityName;
|
||||
}
|
||||
zipTextBox.Text = addr.PostalCode;
|
||||
phoneTextBox.Text = addr.Phone;
|
||||
}
|
||||
|
||||
ShowDialog();
|
||||
}
|
||||
|
||||
private void SaveButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(nameTextBox.Text) ||
|
||||
string.IsNullOrWhiteSpace(addressTextBox.Text) ||
|
||||
string.IsNullOrWhiteSpace(cityTextBox.Text) ||
|
||||
string.IsNullOrWhiteSpace(zipTextBox.Text) ||
|
||||
string.IsNullOrWhiteSpace(phoneTextBox.Text))
|
||||
{
|
||||
MessageBox.Show("All fields must be filled out.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!System.Text.RegularExpressions.Regex.IsMatch(phoneTextBox.Text.Trim(), @"^[\d-]+$"))
|
||||
{
|
||||
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";
|
||||
int currentCountryId = DatabaseHelper.GetCountryId(countryName);
|
||||
if (currentCountryId == 0)
|
||||
{
|
||||
Country newCountry = new Country
|
||||
{
|
||||
CountryName = countryName,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
CreatedBy = currentUser,
|
||||
LastUpdate = DateTime.UtcNow,
|
||||
LastUpdateBy = currentUser
|
||||
};
|
||||
currentCountryId = DatabaseHelper.AddCountry(newCountry);
|
||||
}
|
||||
|
||||
int currentCityId = DatabaseHelper.GetCityId(cityTextBox.Text.Trim(), currentCountryId);
|
||||
if (currentCityId == 0)
|
||||
{
|
||||
City newCity = new City
|
||||
{
|
||||
CityName = cityTextBox.Text.Trim(),
|
||||
CountryID = currentCountryId,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
CreatedBy = currentUser,
|
||||
LastUpdate = DateTime.UtcNow,
|
||||
LastUpdateBy = currentUser
|
||||
};
|
||||
currentCityId = DatabaseHelper.AddCity(newCity);
|
||||
}
|
||||
|
||||
Address addressToSave;
|
||||
int finalAddressId;
|
||||
|
||||
if (Text == "Add Customer")
|
||||
{
|
||||
addressToSave = new Address
|
||||
{
|
||||
Address1 = addressTextBox.Text.Trim(),
|
||||
Address2 = "",
|
||||
CityId = currentCityId,
|
||||
PostalCode = zipTextBox.Text.Trim(),
|
||||
Phone = phoneTextBox.Text.Trim(),
|
||||
CreateDate = DateTime.UtcNow,
|
||||
CreatedBy = currentUser,
|
||||
LastUpdate = DateTime.UtcNow,
|
||||
LastUpdateBy = currentUser
|
||||
};
|
||||
finalAddressId = DatabaseHelper.AddAddress(addressToSave);
|
||||
}
|
||||
else
|
||||
{
|
||||
Address? existingAddress = DatabaseHelper.RetrieveAddress(_currentCustomer.AddressId);
|
||||
if (existingAddress == null)
|
||||
{
|
||||
MessageBox.Show("Could not find existing address for update.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
addressToSave = existingAddress;
|
||||
addressToSave.Address1 = addressTextBox.Text.Trim();
|
||||
addressToSave.Address2 = "";
|
||||
addressToSave.CityId = currentCityId;
|
||||
addressToSave.PostalCode = zipTextBox.Text.Trim();
|
||||
addressToSave.Phone = phoneTextBox.Text.Trim();
|
||||
addressToSave.LastUpdateBy = currentUser;
|
||||
|
||||
DatabaseHelper.UpdateAddress(addressToSave);
|
||||
finalAddressId = addressToSave.Id;
|
||||
}
|
||||
|
||||
Customer customerToSave;
|
||||
if (Text == "Add Customer")
|
||||
{
|
||||
customerToSave = new Customer
|
||||
{
|
||||
CustomerName = nameTextBox.Text.Trim(),
|
||||
AddressId = finalAddressId,
|
||||
Active = 1,
|
||||
CreateDate = DateTime.UtcNow,
|
||||
CreatedBy = currentUser,
|
||||
LastUpdate = DateTime.UtcNow,
|
||||
LastUpdateBy = currentUser
|
||||
};
|
||||
DatabaseHelper.AddCustomer(customerToSave);
|
||||
}
|
||||
else
|
||||
{
|
||||
customerToSave = _currentCustomer;
|
||||
customerToSave.CustomerName = nameTextBox.Text.Trim();
|
||||
customerToSave.AddressId = finalAddressId;
|
||||
customerToSave.LastUpdateBy = currentUser;
|
||||
|
||||
DatabaseHelper.UpdateCustomer(customerToSave);
|
||||
}
|
||||
|
||||
MessageBox.Show("Customer saved successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
DialogResult = DialogResult.OK;
|
||||
Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
private void CancelButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
DialogResult = DialogResult.Cancel;
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void InitAdd()
|
||||
{
|
||||
Text = "Add Customer";
|
||||
label1.Text = "Add Customer";
|
||||
|
||||
nameTextBox.Text = string.Empty;
|
||||
addressTextBox.Text = string.Empty;
|
||||
cityTextBox.Text = string.Empty;
|
||||
zipTextBox.Text = string.Empty;
|
||||
phoneTextBox.Text = string.Empty;
|
||||
|
||||
ShowDialog();
|
||||
}
|
||||
|
||||
public void InitUpdate(Customer customer)
|
||||
{
|
||||
Text = "Update Customer";
|
||||
label1.Text = "Update Customer";
|
||||
|
||||
nameTextBox.Text = customer.CustomerName;
|
||||
var addr = DatabaseHelper.RetrieveAddress(customer.AddressId);
|
||||
addressTextBox.Text = addr.Address1;
|
||||
var city = DatabaseHelper.RetrieveCity(addr.CityId);
|
||||
cityTextBox.Text = city.CityName;
|
||||
zipTextBox.Text = addr.PostalCode;
|
||||
phoneTextBox.Text = addr.Phone;
|
||||
|
||||
ShowDialog();
|
||||
}
|
||||
|
||||
// name, {address, phone}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -16,12 +16,14 @@ public partial class RecordsForm : Form
|
||||
AddButton.Click += (sender, args) =>
|
||||
{
|
||||
_form.InitAdd();
|
||||
_form.Closed += (o, args2) => { UpdateCustomersList(); };
|
||||
};
|
||||
|
||||
ModifyButton.Click += (sender, args) =>
|
||||
{
|
||||
var selectedCustomer = _customers[CustomersDataGrid.CurrentCell.RowIndex];
|
||||
_form.InitUpdate(selectedCustomer);
|
||||
_form.Closed += (o, args2) => { UpdateCustomersList(); };
|
||||
};
|
||||
|
||||
DeleteButton.Click += (sender, args) =>
|
||||
|
Loading…
Reference in New Issue
Block a user