From 1434e867bdbfad32a32a226b52ebd5de8eaf8f6f Mon Sep 17 00:00:00 2001 From: Spudnut2000 Date: Wed, 25 Jun 2025 22:49:08 -0500 Subject: [PATCH] Adding extra section in appts screen to show details better, added appt notif on login. All it needs now is the records sections --- C969Project/AddOrUpdateAppointmentForm.cs | 8 - C969Project/AppointmentsForm.Designer.cs | 77 + C969Project/AppointmentsForm.cs | 14 + C969Project/C969Project.csproj | 5 +- ....Designer.cs => CustomersForm.Designer.cs} | 2 +- .../{RecordsForm.cs => CustomersForm.cs} | 4 +- .../{RecordsForm.resx => CustomersForm.resx} | 0 C969Project/DashboardForm.cs | 8 +- C969Project/Data/DatabaseHelper.cs | 1809 +++++++++-------- C969Project/LoginForm.cs | 32 + C969Project/ReportsForm.Designer.cs | 41 + C969Project/ReportsForm.cs | 9 + C969Project/todo.md | 75 + 13 files changed, 1166 insertions(+), 918 deletions(-) rename C969Project/{RecordsForm.Designer.cs => CustomersForm.Designer.cs} (99%) rename C969Project/{RecordsForm.cs => CustomersForm.cs} (96%) rename C969Project/{RecordsForm.resx => CustomersForm.resx} (100%) create mode 100644 C969Project/ReportsForm.Designer.cs create mode 100644 C969Project/ReportsForm.cs create mode 100644 C969Project/todo.md diff --git a/C969Project/AddOrUpdateAppointmentForm.cs b/C969Project/AddOrUpdateAppointmentForm.cs index 4c92f8d..08b619c 100644 --- a/C969Project/AddOrUpdateAppointmentForm.cs +++ b/C969Project/AddOrUpdateAppointmentForm.cs @@ -79,7 +79,6 @@ public partial class AddOrUpdateAppointmentForm : Form private bool ValidateForm() { - // Ensure all required fields have values if (string.IsNullOrWhiteSpace(titleTextBox.Text) || string.IsNullOrWhiteSpace(descriptionTextBox.Text) || string.IsNullOrWhiteSpace(locationTextBox.Text) || @@ -92,18 +91,15 @@ public partial class AddOrUpdateAppointmentForm : Form return false; } - // Convert start and end times to UTC for comparison DateTime start = startPickerDate.Value.Date.Add(startPickerTime.Value.TimeOfDay).ToUniversalTime(); DateTime end = endPickerDate.Value.Date.Add(endPickerTime.Value.TimeOfDay).ToUniversalTime(); - // Validate start and end times if (start >= end) { MessageBox.Show("Start time must be before end time.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } - // Validate business hours (9:00 AM - 5:00 PM EST, Monday-Friday) TimeZoneInfo estZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); DateTime startEST = TimeZoneInfo.ConvertTimeFromUtc(start, estZone); DateTime endEST = TimeZoneInfo.ConvertTimeFromUtc(end, estZone); @@ -114,7 +110,6 @@ public partial class AddOrUpdateAppointmentForm : Form return false; } - // Check for overlapping appointments var appointments = DatabaseHelper.RetrieveAppointments(AppState.CurrentUser.UserId); foreach (var appt in appointments) { @@ -138,7 +133,6 @@ public partial class AddOrUpdateAppointmentForm : Form try { - // Gather appointment data from form var selectedCustomer = (Customer)customerComboBox.SelectedItem!; var appointment = new Appointment { @@ -161,13 +155,11 @@ public partial class AddOrUpdateAppointmentForm : Form if (_currentAppointment == null) { - // Add new appointment DatabaseHelper.AddAppointment(appointment); MessageBox.Show("Appointment added successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { - // Update existing appointment DatabaseHelper.UpdateAppointment(appointment); MessageBox.Show("Appointment updated successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } diff --git a/C969Project/AppointmentsForm.Designer.cs b/C969Project/AppointmentsForm.Designer.cs index 429cf67..a3d95fb 100644 --- a/C969Project/AppointmentsForm.Designer.cs +++ b/C969Project/AppointmentsForm.Designer.cs @@ -37,7 +37,14 @@ partial class AppointmentsForm modifyButton = new System.Windows.Forms.Button(); deleteButton = new System.Windows.Forms.Button(); showAllButton = new System.Windows.Forms.Button(); + appointmentDetails = new System.Windows.Forms.GroupBox(); + apptTitleLabel = new System.Windows.Forms.Label(); + apptTypeLabel = new System.Windows.Forms.Label(); + withLabel = new System.Windows.Forms.Label(); + startTimeLabel = new System.Windows.Forms.Label(); + endTimeLabel = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)appointmentDataView).BeginInit(); + appointmentDetails.SuspendLayout(); SuspendLayout(); // // appointmentDataView @@ -96,11 +103,71 @@ partial class AppointmentsForm showAllButton.Text = "Show All Appointments"; showAllButton.UseVisualStyleBackColor = true; // + // appointmentDetails + // + appointmentDetails.Controls.Add(endTimeLabel); + appointmentDetails.Controls.Add(startTimeLabel); + appointmentDetails.Controls.Add(withLabel); + appointmentDetails.Controls.Add(apptTypeLabel); + appointmentDetails.Controls.Add(apptTitleLabel); + appointmentDetails.Location = new System.Drawing.Point(773, 214); + appointmentDetails.Name = "appointmentDetails"; + appointmentDetails.Size = new System.Drawing.Size(223, 320); + appointmentDetails.TabIndex = 6; + appointmentDetails.TabStop = false; + appointmentDetails.Text = "Details"; + // + // apptTitleLabel + // + apptTitleLabel.Location = new System.Drawing.Point(6, 19); + apptTitleLabel.Name = "apptTitleLabel"; + apptTitleLabel.Size = new System.Drawing.Size(211, 23); + apptTitleLabel.TabIndex = 0; + apptTitleLabel.Text = "Title: "; + apptTitleLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + // + // apptTypeLabel + // + apptTypeLabel.Location = new System.Drawing.Point(6, 42); + apptTypeLabel.Name = "apptTypeLabel"; + apptTypeLabel.Size = new System.Drawing.Size(211, 23); + apptTypeLabel.TabIndex = 1; + apptTypeLabel.Text = "Type: "; + apptTypeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + // + // withLabel + // + withLabel.Location = new System.Drawing.Point(6, 65); + withLabel.Name = "withLabel"; + withLabel.Size = new System.Drawing.Size(211, 23); + withLabel.TabIndex = 2; + withLabel.Text = "With: "; + withLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + // + // startTimeLabel + // + startTimeLabel.Location = new System.Drawing.Point(6, 88); + startTimeLabel.Name = "startTimeLabel"; + startTimeLabel.Size = new System.Drawing.Size(211, 23); + startTimeLabel.TabIndex = 3; + startTimeLabel.Text = "Start: "; + startTimeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + // + // endTimeLabel + // + endTimeLabel.Location = new System.Drawing.Point(6, 111); + endTimeLabel.Name = "endTimeLabel"; + endTimeLabel.Size = new System.Drawing.Size(211, 23); + endTimeLabel.TabIndex = 4; + endTimeLabel.Text = "End: "; + endTimeLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + // // AppointmentsForm // AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; ClientSize = new System.Drawing.Size(1008, 575); + Controls.Add(appointmentDetails); Controls.Add(showAllButton); Controls.Add(deleteButton); Controls.Add(modifyButton); @@ -109,9 +176,19 @@ partial class AppointmentsForm Controls.Add(appointmentDataView); Text = "AppointmentsForm"; ((System.ComponentModel.ISupportInitialize)appointmentDataView).EndInit(); + appointmentDetails.ResumeLayout(false); ResumeLayout(false); } + private System.Windows.Forms.Label apptTypeLabel; + private System.Windows.Forms.Label withLabel; + private System.Windows.Forms.Label startTimeLabel; + private System.Windows.Forms.Label endTimeLabel; + + private System.Windows.Forms.Label apptTitleLabel; + + private System.Windows.Forms.GroupBox appointmentDetails; + private System.Windows.Forms.Button showAllButton; private System.Windows.Forms.DataGridView appointmentDataView; diff --git a/C969Project/AppointmentsForm.cs b/C969Project/AppointmentsForm.cs index 89d3595..2278b8f 100644 --- a/C969Project/AppointmentsForm.cs +++ b/C969Project/AppointmentsForm.cs @@ -20,6 +20,8 @@ public partial class AppointmentsForm : Form deleteButton.Click += DeleteButtonOnClick; addButton.Click += AddButtonOnClick; modifyButton.Click += ModifyButtonOnClick; + + appointmentDataView.SelectionChanged += UpdateAppointmentDetails; } private void ModifyButtonOnClick(object? sender, EventArgs e) @@ -74,4 +76,16 @@ public partial class AppointmentsForm : Form List? selectedAppointments = _appointments.Where(a => a.Start == date).ToList(); appointmentDataView.DataSource = selectedAppointments?.Count > 0 ? selectedAppointments : null; } + + private void UpdateAppointmentDetails(object? sender, EventArgs e) + { + if (appointmentDataView.CurrentRow?.DataBoundItem is Appointment appointment) + { + apptTitleLabel.Text = $"Title: {appointment.Title}"; + apptTypeLabel.Text = $"Type: {appointment.AppointmentType}"; + withLabel.Text = $"With: {DatabaseHelper.RetrieveCustomer(appointment.CustomerId)?.CustomerName ?? "Unknown"}"; + startTimeLabel.Text = $"Start: {appointment.Start.ToLocalTime():MM/dd/yyyy hh:mm tt} {TimeZoneInfo.Local.Id}"; + endTimeLabel.Text = $"End: {appointment.End.ToLocalTime():MM/dd/yyyy hh:mm tt} {TimeZoneInfo.Local.Id}"; + } + } } \ No newline at end of file diff --git a/C969Project/C969Project.csproj b/C969Project/C969Project.csproj index 5a2850a..00d61fe 100644 --- a/C969Project/C969Project.csproj +++ b/C969Project/C969Project.csproj @@ -17,7 +17,7 @@ Form - + Form @@ -29,6 +29,9 @@ Form + + Form + diff --git a/C969Project/RecordsForm.Designer.cs b/C969Project/CustomersForm.Designer.cs similarity index 99% rename from C969Project/RecordsForm.Designer.cs rename to C969Project/CustomersForm.Designer.cs index b714e3e..6c78efe 100644 --- a/C969Project/RecordsForm.Designer.cs +++ b/C969Project/CustomersForm.Designer.cs @@ -2,7 +2,7 @@ using System.ComponentModel; namespace C969Project; -partial class RecordsForm +partial class CustomersForm { /// /// Required designer variable. diff --git a/C969Project/RecordsForm.cs b/C969Project/CustomersForm.cs similarity index 96% rename from C969Project/RecordsForm.cs rename to C969Project/CustomersForm.cs index b338371..7ccdc77 100644 --- a/C969Project/RecordsForm.cs +++ b/C969Project/CustomersForm.cs @@ -3,12 +3,12 @@ using C969Project.Data.Models; namespace C969Project; -public partial class RecordsForm : Form +public partial class CustomersForm : Form { private List? _customers; private AddUpdateCustomerForm _form = new(); - public RecordsForm() + public CustomersForm() { InitializeComponent(); UpdateCustomersList(null, EventArgs.Empty); diff --git a/C969Project/RecordsForm.resx b/C969Project/CustomersForm.resx similarity index 100% rename from C969Project/RecordsForm.resx rename to C969Project/CustomersForm.resx diff --git a/C969Project/DashboardForm.cs b/C969Project/DashboardForm.cs index f9a1711..ed98164 100644 --- a/C969Project/DashboardForm.cs +++ b/C969Project/DashboardForm.cs @@ -2,20 +2,20 @@ namespace C969Project; public partial class DashboardForm : Form { - private RecordsForm _recordsForm; + private CustomersForm _customersForm; private AppointmentsForm _appointmentsForm; public DashboardForm() { InitializeComponent(); - _recordsForm = new RecordsForm(); + _customersForm = new CustomersForm(); _appointmentsForm = new AppointmentsForm(); CustomersButton.Click += (sender, args) => { - _recordsForm.ShowDialog(); - _recordsForm.UpdateCustomersList(null, EventArgs.Empty); + _customersForm.ShowDialog(); + _customersForm.UpdateCustomersList(null, EventArgs.Empty); }; AppointmentsButton.Click += (sender, args) => diff --git a/C969Project/Data/DatabaseHelper.cs b/C969Project/Data/DatabaseHelper.cs index d54e5ab..53e1ba5 100644 --- a/C969Project/Data/DatabaseHelper.cs +++ b/C969Project/Data/DatabaseHelper.cs @@ -1,990 +1,995 @@ -using System; -using System.Data; using MySql.Data.MySqlClient; using C969Project.Data.Models; -namespace C969Project.Data +namespace C969Project.Data; + +public static class DatabaseHelper { - public static class DatabaseHelper + private static string ConnectionString => AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"); + + public static User? Login(string username, string password) { - private static string ConnectionString => AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"); - - public static User? Login(string username, string password) + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try + connection.Open(); + } + catch (MySqlException ex) + { + MessageBox.Show($"Database connection error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return null; + } + + string query = "SELECT * FROM client_schedule.user WHERE userName = @username"; + using MySqlCommand command = new MySqlCommand(query, connection); + command.Parameters.AddWithValue("@username", username); + + using MySqlDataReader reader = command.ExecuteReader(); + + while (reader.Read()) + { + string name = reader.GetString("userName"); + string pass = reader.GetString("password"); + + if (name == username && password == pass) { - connection.Open(); + int id = reader.GetInt32("userId"); + return new User(id, name); } - catch (MySqlException ex) + } + + return null; + } + + #region Customers + public static List RetrieveCustomers() + { + 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 customers = new List(); + + using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.customer", connection); + using MySqlDataReader reader = command.ExecuteReader(); + while (reader.Read()) + { + var customer = new Customer() { - Console.WriteLine(ex.Message); - } - - string query = "SELECT * FROM client_schedule.user WHERE userName = @username"; - using MySqlCommand command = new MySqlCommand(query, connection); - command.Parameters.AddWithValue("@username", username); - - using MySqlDataReader reader = command.ExecuteReader(); - - while (reader.Read()) - { - string name = reader.GetString("userName"); - string pass = reader.GetString("password"); - - if (name == username && password == pass) - { - int id = reader.GetInt32("userId"); - return new User(id, name); - } - } + CustomerId = reader.GetInt32("customerId"), + CustomerName = reader.GetString("customerName"), + AddressId = reader.GetInt32("addressId"), + Active = reader.GetInt32("active"), + CreateDate = reader.GetDateTime("createDate"), + CreatedBy = reader.GetString("createdBy"), + LastUpdate = reader.GetDateTime("lastUpdate"), + LastUpdateBy = reader.GetString("lastUpdateBy"), + }; + customers.Add(customer); + } + + return customers; + } + + public static Customer? RetrieveCustomer(int customerId) + { + 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; } - #region Customers - public static List RetrieveCustomers() + string query = "SELECT * FROM client_schedule.customer WHERE customerId = @customerId"; + using MySqlCommand command = new MySqlCommand(query, connection); + command.Parameters.AddWithValue("@customerId", customerId); + + using MySqlDataReader reader = command.ExecuteReader(); + if (reader.Read()) { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try + return new Customer { - connection.Open(); - } - catch (MySqlException e) + CustomerId = reader.GetInt32("customerId"), + CustomerName = reader.GetString("customerName"), + AddressId = reader.GetInt32("addressId"), + Active = reader.GetInt32("active"), + CreateDate = reader.GetDateTime("createDate"), + CreatedBy = reader.GetString("createdBy"), + LastUpdate = reader.GetDateTime("lastUpdate"), + LastUpdateBy = reader.GetString("lastUpdateBy"), + }; + } + + return null; + } + + public static int AddCustomer(Customer customer) + { + int customerId = 0; + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + + string query = + "INSERT INTO customer (customerName, addressId, active, createDate, createdBy, lastUpdate, lastUpdateBy) " + + "VALUES (@customerName, @addressId, @active, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + + "SELECT LAST_INSERT_ID();"; + + 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("@createDate", DateTime.UtcNow); + command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); + command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); + + object result = command.ExecuteScalar(); + if (result != null && result != DBNull.Value) { - Console.WriteLine(e); - throw; + customerId = Convert.ToInt32(result); } + } + 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); + } + return customerId; + } + + public static void UpdateCustomer(Customer customer) + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + + string query = + "UPDATE customer SET customerName = @customerName, addressId = @addressId, active = @active, " + + "lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE customerId = @customerId"; - List customers = new List(); + using MySqlCommand command = new MySqlCommand(query, connection); - using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.customer", connection); - using MySqlDataReader reader = command.ExecuteReader(); - while (reader.Read()) - { - var customer = new Customer() - { - CustomerId = reader.GetInt32("customerId"), - CustomerName = reader.GetString("customerName"), - AddressId = reader.GetInt32("addressId"), - Active = reader.GetInt32("active"), - CreateDate = reader.GetDateTime("createDate"), - CreatedBy = reader.GetString("createdBy"), - LastUpdate = reader.GetDateTime("lastUpdate"), - LastUpdateBy = reader.GetString("lastUpdateBy"), - }; - - customers.Add(customer); - } + 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); + command.Parameters.AddWithValue("@customerId", customer.CustomerId); - return customers; + 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 DeleteCustomer(int customerId) + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + string query = "DELETE FROM customer WHERE customerId = @customerId"; + using MySqlCommand command = new MySqlCommand(query, connection); + command.Parameters.AddWithValue("@customerId", customerId); + 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 DeleteCustomer(Customer customer) + { + DeleteCustomer(customer.CustomerId); + } + #endregion + + #region Addresses + + public static Address? RetrieveAddress(int addressId) + { + 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; } - public static int AddCustomer(Customer customer) - { - int customerId = 0; - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); + string query = "SELECT * FROM client_schedule.address WHERE addressId = @addressId"; + using MySqlCommand command = new MySqlCommand(query, connection); + command.Parameters.AddWithValue("@addressId", addressId); - string query = - "INSERT INTO customer (customerName, addressId, active, createDate, createdBy, lastUpdate, lastUpdateBy) " + - "VALUES (@customerName, @addressId, @active, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + - "SELECT LAST_INSERT_ID();"; - - 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("@createDate", DateTime.UtcNow); - command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); - - object result = command.ExecuteScalar(); - if (result != null && result != DBNull.Value) - { - customerId = Convert.ToInt32(result); - } - } - catch (MySqlException e) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - return customerId; - } - - public static void UpdateCustomer(Customer customer) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - - string query = - "UPDATE customer SET customerName = @customerName, addressId = @addressId, active = @active, " + - "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); - command.Parameters.AddWithValue("@customerId", customer.CustomerId); - - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - } + using MySqlDataReader reader = command.ExecuteReader(); - public static void DeleteCustomer(int customerId) + while (reader.Read()) { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try + var addr = new Address() { - connection.Open(); - string query = "DELETE FROM customer WHERE customerId = @customerId"; - using MySqlCommand command = new MySqlCommand(query, connection); - command.Parameters.AddWithValue("@customerId", customerId); - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - } - - public static void DeleteCustomer(Customer customer) - { - DeleteCustomer(customer.CustomerId); - } - #endregion - - #region Addresses - - public static Address? RetrieveAddress(int addressId) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - } - catch (MySqlException e) - { - Console.WriteLine(e); - throw; - } + 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"), + }; - string query = "SELECT * FROM client_schedule.address WHERE addressId = @addressId"; + return addr; + } + + 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) + { + int addressId = 0; + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + + 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); " + + "SELECT LAST_INSERT_ID();"; + + using MySqlCommand command = new MySqlCommand(query, connection); + + command.Parameters.AddWithValue("@address1", address.Address1); + command.Parameters.AddWithValue("@address2", address.Address2); + command.Parameters.AddWithValue("@cityId", address.CityId); + command.Parameters.AddWithValue("@postalCode", address.PostalCode); + command.Parameters.AddWithValue("@phone", address.Phone); + command.Parameters.AddWithValue("@createDate", DateTime.UtcNow); + command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); + command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); + + object result = command.ExecuteScalar(); + if (result != null && result != DBNull.Value) + { + addressId = Convert.ToInt32(result); + } + } + 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); + } + return addressId; + } + + public static void UpdateAddress(Address address) + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + + string query = + "UPDATE address SET address = @address1, address2 = @address2, cityId = @cityId, postalCode = @postalCode, " + + "phone = @phone, lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId"; + + using MySqlCommand command = new MySqlCommand(query, connection); + + command.Parameters.AddWithValue("@address1", address.Address1); + command.Parameters.AddWithValue("@address2", address.Address2); + command.Parameters.AddWithValue("@cityId", address.CityId); + command.Parameters.AddWithValue("@postalCode", address.PostalCode); + command.Parameters.AddWithValue("@phone", address.Phone); + command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); + command.Parameters.AddWithValue("@addressId", address.Id); + + 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(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); + } + } - 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"), - }; - - return addr; - } - + 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); - public static List
RetrieveAllAddresses() + using MySqlDataReader reader = command.ExecuteReader(); + + while (reader.Read()) { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try + var appt = new Appointment() { - connection.Open(); - } - catch (MySqlException e) - { - Console.WriteLine(e); - throw; - } + 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"), + }; - 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; + return appt; } - public static int AddAddress(Address address) - { - int addressId = 0; - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); + return null; + } - 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); " + - "SELECT LAST_INSERT_ID();"; - - using MySqlCommand command = new MySqlCommand(query, connection); - - command.Parameters.AddWithValue("@address1", address.Address1); - command.Parameters.AddWithValue("@address2", address.Address2); - command.Parameters.AddWithValue("@cityId", address.CityId); - command.Parameters.AddWithValue("@postalCode", address.PostalCode); - command.Parameters.AddWithValue("@phone", address.Phone); - command.Parameters.AddWithValue("@createDate", DateTime.UtcNow); - command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); - - object result = command.ExecuteScalar(); - if (result != null && result != DBNull.Value) - { - addressId = Convert.ToInt32(result); - } - } - catch (MySqlException e) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - return addressId; + public static List RetrieveAppointments(int? userId = null) + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); } - - public static void UpdateAddress(Address address) + catch (MySqlException e) { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - - string query = - "UPDATE address SET address = @address1, address2 = @address2, cityId = @cityId, postalCode = @postalCode, " + - "phone = @phone, lastUpdate = CURRENT_TIMESTAMP, lastUpdateBy = @lastUpdateBy WHERE addressId = @addressId"; - - using MySqlCommand command = new MySqlCommand(query, connection); - - command.Parameters.AddWithValue("@address1", address.Address1); - command.Parameters.AddWithValue("@address2", address.Address2); - command.Parameters.AddWithValue("@cityId", address.CityId); - command.Parameters.AddWithValue("@postalCode", address.PostalCode); - command.Parameters.AddWithValue("@phone", address.Phone); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); - command.Parameters.AddWithValue("@addressId", address.Id); - - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } + MessageBox.Show($"Database connection error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return new List(); } - - public static void DeleteAddress(int addressId) + + List appointments = new List(); + + string query = "SELECT * FROM client_schedule.appointment"; + if (userId.HasValue) query += " WHERE userId = @userId"; + using MySqlCommand command = new MySqlCommand(query, connection); + if (userId.HasValue) command.Parameters.AddWithValue("@userId", userId.Value); + + using MySqlDataReader reader = command.ExecuteReader(); + while (reader.Read()) { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try + var appt = new Appointment() { - 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) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - } - - 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) - { - Console.WriteLine(e); - throw; - } + 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"), + }; - string query = "SELECT * FROM client_schedule.appointment WHERE appointmentId = @appointmentId"; + appointments.Add(appt); + } + + return appointments; + } + + public static void AddAppointment(Appointment appointment) + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + + string query = + "INSERT INTO appointment (appointmentId, customerId, userId, title, description, location, contact, type, url, start, end, createDate, createdBy, lastUpdate, lastUpdateBy) " + + "VALUES (@appointmentId, @customerId, @userId, @title, @description, @location, @contact, @type, @url, @start, @end, @createDate, @createdBy, @lastUpdate, @lastUpdateBy)"; + + using MySqlCommand command = new MySqlCommand(query, connection); + + command.Parameters.AddWithValue("@appointmentId", appointment.AppointmentId); + command.Parameters.AddWithValue("@customerId", appointment.CustomerId); + command.Parameters.AddWithValue("@userId", appointment.UserId); + command.Parameters.AddWithValue("@title", appointment.Title); + command.Parameters.AddWithValue("@description", appointment.Description); + command.Parameters.AddWithValue("@location", appointment.Location); + command.Parameters.AddWithValue("@contact", appointment.Contact); + command.Parameters.AddWithValue("@type", appointment.AppointmentType); + command.Parameters.AddWithValue("@url", appointment.Url); + command.Parameters.AddWithValue("@start", appointment.Start); + command.Parameters.AddWithValue("@end", appointment.End); + command.Parameters.AddWithValue("@createDate", DateTime.UtcNow); + command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); + command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow); + command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); + + 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 UpdateAppointment(Appointment appointment) + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + + string query = + "UPDATE appointment SET customerId = @customerId, userId = @userId, title = @title, description = @description, " + + "location = @location, contact = @contact, type = @type, url = @url, start = @start, end = @end, " + + "lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE appointmentId = @appointmentId"; + + using MySqlCommand command = new MySqlCommand(query, connection); + + command.Parameters.AddWithValue("@customerId", appointment.CustomerId); + command.Parameters.AddWithValue("@userId", appointment.UserId); + command.Parameters.AddWithValue("@title", appointment.Title); + command.Parameters.AddWithValue("@description", appointment.Description); + command.Parameters.AddWithValue("@location", appointment.Location); + command.Parameters.AddWithValue("@contact", appointment.Contact); + command.Parameters.AddWithValue("@type", appointment.AppointmentType); + command.Parameters.AddWithValue("@url", appointment.Url); + command.Parameters.AddWithValue("@start", appointment.Start); + command.Parameters.AddWithValue("@end", appointment.End); + command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow); + command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); + command.Parameters.AddWithValue("@appointmentId", appointment.AppointmentId); + + 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 DeleteAppointment(int appointmentId) + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + string query = "DELETE FROM appointment WHERE appointmentId = @appointmentId"; using MySqlCommand command = new MySqlCommand(query, connection); command.Parameters.AddWithValue("@appointmentId", appointmentId); + 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); + } + } - 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; - } - + public static void DeleteAppointment(Appointment appointment) + { + DeleteAppointment(appointment.AppointmentId); + } + + #endregion + + #region Cities + public static City? RetrieveCity(int cityId) + { + 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; } - - public static List RetrieveAppointments(int? userId = null) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - } - catch (MySqlException e) - { - Console.WriteLine(e); - throw; - } - - List appointments = new List(); - - string query = "SELECT * FROM client_schedule.appointment"; - if (userId.HasValue) query += " WHERE userId = @userId"; - using MySqlCommand command = new MySqlCommand(query, connection); - if (userId.HasValue) command.Parameters.AddWithValue("@userId", userId.Value); - - 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"), - }; - - appointments.Add(appt); - } - - return appointments; - } - - public static void AddAppointment(Appointment appointment) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - - string query = - "INSERT INTO appointment (appointmentId, customerId, userId, title, description, location, contact, type, url, start, end, createDate, createdBy, lastUpdate, lastUpdateBy) " + - "VALUES (@appointmentId, @customerId, @userId, @title, @description, @location, @contact, @type, @url, @start, @end, @createDate, @createdBy, @lastUpdate, @lastUpdateBy)"; - - using MySqlCommand command = new MySqlCommand(query, connection); - - command.Parameters.AddWithValue("@appointmentId", appointment.AppointmentId); - command.Parameters.AddWithValue("@customerId", appointment.CustomerId); - command.Parameters.AddWithValue("@userId", appointment.UserId); - command.Parameters.AddWithValue("@title", appointment.Title); - command.Parameters.AddWithValue("@description", appointment.Description); - command.Parameters.AddWithValue("@location", appointment.Location); - command.Parameters.AddWithValue("@contact", appointment.Contact); - command.Parameters.AddWithValue("@type", appointment.AppointmentType); - command.Parameters.AddWithValue("@url", appointment.Url); - command.Parameters.AddWithValue("@start", appointment.Start); - command.Parameters.AddWithValue("@end", appointment.End); - command.Parameters.AddWithValue("@createDate", DateTime.UtcNow); - command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); - command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); - - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - } - - public static void UpdateAppointment(Appointment appointment) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - - string query = - "UPDATE appointment SET customerId = @customerId, userId = @userId, title = @title, description = @description, " + - "location = @location, contact = @contact, type = @type, url = @url, start = @start, end = @end, " + - "lastUpdate = @lastUpdate, lastUpdateBy = @lastUpdateBy WHERE appointmentId = @appointmentId"; - - using MySqlCommand command = new MySqlCommand(query, connection); - - command.Parameters.AddWithValue("@customerId", appointment.CustomerId); - command.Parameters.AddWithValue("@userId", appointment.UserId); - command.Parameters.AddWithValue("@title", appointment.Title); - command.Parameters.AddWithValue("@description", appointment.Description); - command.Parameters.AddWithValue("@location", appointment.Location); - command.Parameters.AddWithValue("@contact", appointment.Contact); - command.Parameters.AddWithValue("@type", appointment.AppointmentType); - command.Parameters.AddWithValue("@url", appointment.Url); - command.Parameters.AddWithValue("@start", appointment.Start); - command.Parameters.AddWithValue("@end", appointment.End); - command.Parameters.AddWithValue("@lastUpdate", DateTime.UtcNow); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); - command.Parameters.AddWithValue("@appointmentId", appointment.AppointmentId); - - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - } - - public static void DeleteAppointment(int appointmentId) - { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - string query = "DELETE FROM appointment WHERE appointmentId = @appointmentId"; - using MySqlCommand command = new MySqlCommand(query, connection); - command.Parameters.AddWithValue("@appointmentId", appointmentId); - command.ExecuteNonQuery(); - } - catch (MySqlException e) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - } - - public static void DeleteAppointment(Appointment appointment) - { - DeleteAppointment(appointment.AppointmentId); - } - - #endregion - #region Cities - public static City? RetrieveCity(int cityId) + string query = "SELECT * FROM client_schedule.city WHERE cityId = @cityId"; + using MySqlCommand command = new MySqlCommand(query, connection); + command.Parameters.AddWithValue("@cityId", cityId); + + using MySqlDataReader reader = command.ExecuteReader(); + + while (reader.Read()) { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try + 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"), + }; + + return city; + } + + 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) + { + int cityId = 0; + try + { + using (MySqlConnection connection = new MySqlConnection(ConnectionString)) { connection.Open(); + string query = "SELECT cityId FROM city WHERE city = @cityName AND countryId = @countryId"; + using (MySqlCommand cmd = new MySqlCommand(query, connection)) + { + cmd.Parameters.AddWithValue("@cityName", cityName); + cmd.Parameters.AddWithValue("@countryId", countryId); + object result = cmd.ExecuteScalar(); + if (result != null && result != DBNull.Value) + { + cityId = Convert.ToInt32(result); + } + } } - catch (MySqlException e) - { - Console.WriteLine(e); - throw; - } + } + catch (Exception ex) + { + MessageBox.Show($"Error getting city ID: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + return cityId; + } + + public static int AddCity(City city) + { + int cityId = 0; + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + + string query = + "INSERT INTO city (city, countryId, createDate, createdBy, lastUpdate, lastUpdateBy) " + + "VALUES (@cityName, @countryId, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + + "SELECT LAST_INSERT_ID();"; - string query = "SELECT * FROM client_schedule.city 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("@createDate", DateTime.UtcNow); + command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); + command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); + + object result = command.ExecuteScalar(); + if (result != null && result != DBNull.Value) + { + cityId = Convert.ToInt32(result); + } + } + 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); + } + 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); + } + } - 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"), - }; - - return city; - } - + 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); - public static List RetrieveAllCities() + using MySqlDataReader reader = command.ExecuteReader(); + + while (reader.Read()) { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try + var country = new Country() { - connection.Open(); - } - catch (MySqlException e) - { - Console.WriteLine(e); - throw; - } + CountryID = reader.GetInt32("countryId"), + CountryName = reader.GetString("country"), + CreateDate = reader.GetDateTime("createDate"), + CreatedBy = reader.GetString("createdBy"), + LastUpdate = reader.GetDateTime("lastUpdate"), + LastUpdateBy = reader.GetString("lastUpdateBy"), + }; - 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; + return country; } - public static int GetCityId(string cityName, int countryId) + return null; + } + + public static List RetrieveAllCountries() + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try { - int cityId = 0; - 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() { - using (MySqlConnection connection = new MySqlConnection(ConnectionString)) + 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) + { + int countryId = 0; + try + { + using (MySqlConnection connection = new MySqlConnection(ConnectionString)) + { + connection.Open(); + string query = "SELECT countryId FROM country WHERE country = @countryName"; + using (MySqlCommand cmd = new MySqlCommand(query, connection)) { - connection.Open(); - string query = "SELECT cityId FROM city WHERE city = @cityName AND countryId = @countryId"; - using (MySqlCommand cmd = new MySqlCommand(query, connection)) + cmd.Parameters.AddWithValue("@countryName", countryName); + object result = cmd.ExecuteScalar(); + if (result != null && result != DBNull.Value) { - cmd.Parameters.AddWithValue("@cityName", cityName); - cmd.Parameters.AddWithValue("@countryId", countryId); - object result = cmd.ExecuteScalar(); - if (result != null && result != DBNull.Value) - { - cityId = Convert.ToInt32(result); - } + countryId = Convert.ToInt32(result); } } } - catch (Exception ex) - { - Console.WriteLine($"Error getting city ID: {ex.Message}"); - } - return cityId; } - - public static int AddCity(City city) + catch (Exception ex) { - int cityId = 0; - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - - string query = - "INSERT INTO city (city, countryId, createDate, createdBy, lastUpdate, lastUpdateBy) " + - "VALUES (@cityName, @countryId, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + - "SELECT LAST_INSERT_ID();"; - - using MySqlCommand command = new MySqlCommand(query, connection); - - command.Parameters.AddWithValue("@cityName", city.CityName); - command.Parameters.AddWithValue("@countryId", city.CountryID); - command.Parameters.AddWithValue("@createDate", DateTime.UtcNow); - command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); - - object result = command.ExecuteScalar(); - if (result != null && result != DBNull.Value) - { - cityId = Convert.ToInt32(result); - } - } - catch (MySqlException e) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - return cityId; + MessageBox.Show($"Error getting country ID: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } - - public static void UpdateCity(City city) + return countryId; + } + + public static int AddCountry(Country country) + { + int countryId = 0; + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); + 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) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - } - - 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) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - } - - 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) - { - Console.WriteLine(e); - throw; - } + string query = + "INSERT INTO country (country, createDate, createdBy, lastUpdate, lastUpdateBy) " + + "VALUES (@countryName, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + + "SELECT LAST_INSERT_ID();"; - string query = "SELECT * FROM client_schedule.country WHERE countryId = @countryId"; + using MySqlCommand command = new MySqlCommand(query, connection); + + command.Parameters.AddWithValue("@countryName", country.CountryName); + command.Parameters.AddWithValue("@createDate", DateTime.UtcNow); + command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); + command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); + + object result = command.ExecuteScalar(); + if (result != null && result != DBNull.Value) + { + countryId = Convert.ToInt32(result); + } + } + 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); + } + 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); - - 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; + command.ExecuteNonQuery(); } - - public static List RetrieveAllCountries() + catch (MySqlException e) { - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - } - catch (MySqlException e) - { - Console.WriteLine(e); - throw; - } - - 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; + MessageBox.Show($"MySQL Error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } - - public static int GetCountryId(string countryName) + catch (Exception ex) { - int countryId = 0; - try - { - using (MySqlConnection connection = new MySqlConnection(ConnectionString)) - { - connection.Open(); - string query = "SELECT countryId FROM country WHERE country = @countryName"; - using (MySqlCommand cmd = new MySqlCommand(query, connection)) - { - cmd.Parameters.AddWithValue("@countryName", countryName); - object result = cmd.ExecuteScalar(); - if (result != null && result != DBNull.Value) - { - countryId = Convert.ToInt32(result); - } - } - } - } - catch (Exception ex) - { - Console.WriteLine($"Error getting country ID: {ex.Message}"); - } - return countryId; + MessageBox.Show($"General Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } - - public static int AddCountry(Country country) - { - int countryId = 0; - using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); - try - { - connection.Open(); - - string query = - "INSERT INTO country (country, createDate, createdBy, lastUpdate, lastUpdateBy) " + - "VALUES (@countryName, @createDate, @createdBy, CURRENT_TIMESTAMP, @lastUpdateBy); " + - "SELECT LAST_INSERT_ID();"; - - using MySqlCommand command = new MySqlCommand(query, connection); - - command.Parameters.AddWithValue("@countryName", country.CountryName); - command.Parameters.AddWithValue("@createDate", DateTime.UtcNow); - command.Parameters.AddWithValue("@createdBy", AppState.CurrentUser.Username); - command.Parameters.AddWithValue("@lastUpdateBy", AppState.CurrentUser.Username); - - object result = command.ExecuteScalar(); - if (result != null && result != DBNull.Value) - { - countryId = Convert.ToInt32(result); - } - } - catch (MySqlException e) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - 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) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - } - - 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) - { - Console.WriteLine($"MySQL Error: {e.Message}"); - throw; - } - catch (Exception ex) - { - Console.WriteLine($"General Error: {ex.Message}"); - throw; - } - } - - public static void DeleteCountry(Country country) - { - DeleteCountry(country.CountryID); - } - #endregion } -} \ No newline at end of file + + public static void DeleteCountry(Country country) + { + DeleteCountry(country.CountryID); + } + #endregion +} + \ No newline at end of file diff --git a/C969Project/LoginForm.cs b/C969Project/LoginForm.cs index 348c39c..fc3830c 100644 --- a/C969Project/LoginForm.cs +++ b/C969Project/LoginForm.cs @@ -34,12 +34,44 @@ namespace C969Project Console.WriteLine($"Login successful, {usr.Username}"); AppState.CurrentUser = usr; + CheckForAppointments(); + var dash = new DashboardForm(); dash.FormClosing += (o, args) => { Close(); }; dash.Show(); Hide(); } + private void CheckForAppointments() + { + if (AppState.CurrentUser is null) + { + MessageBox.Show("No user logged in, cannot check for appointments.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + var appointments = DatabaseHelper.RetrieveAppointments(AppState.CurrentUser.UserId); + + DateTime nowUtc = DateTime.UtcNow; + DateTime fifteenMinutesFromNowUtc = nowUtc.AddMinutes(15); + + var upcoming = appointments + .Where(a => a.Start >= nowUtc && a.Start <= fifteenMinutesFromNowUtc) + .OrderBy(a => a.Start) + .ToList(); + + if (upcoming.Any()) + { + var appt = upcoming.First(); + DateTime localStart = appt.Start.ToLocalTime(); + MessageBox.Show( + $"You have an upcoming {appt.AppointmentType} appointment '{appt.Title}' at {localStart}.", + "Upcoming Appointment", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + } + } + private void UpdateLoginButtonState() { if (string.IsNullOrEmpty(usernameTextBox.Text) || string.IsNullOrEmpty(passwordTextBox.Text)) diff --git a/C969Project/ReportsForm.Designer.cs b/C969Project/ReportsForm.Designer.cs new file mode 100644 index 0000000..f69a728 --- /dev/null +++ b/C969Project/ReportsForm.Designer.cs @@ -0,0 +1,41 @@ +using System.ComponentModel; + +namespace C969Project; + +partial class ReportsForm +{ + /// + /// Required designer variable. + /// + private IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "ReportsForm"; + } + + #endregion +} \ No newline at end of file diff --git a/C969Project/ReportsForm.cs b/C969Project/ReportsForm.cs new file mode 100644 index 0000000..a8ccbe9 --- /dev/null +++ b/C969Project/ReportsForm.cs @@ -0,0 +1,9 @@ +namespace C969Project; + +public partial class ReportsForm : Form +{ + public ReportsForm() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/C969Project/todo.md b/C969Project/todo.md new file mode 100644 index 0000000..1d22ba2 --- /dev/null +++ b/C969Project/todo.md @@ -0,0 +1,75 @@ +# ✅ C# Application Development To-Do List + +## 1. Login Form + +- [x] Create a login form with the following functionality: + - [x] Determine a user’s location. + - [x] Translate login and error control messages into: + - [x] English + - [x] One additional language + - [x] Verify correct username and password. + +## 2. Customer Record Management + +- [x] Add functionality to: + - [x] Add customer records + - [x] Update customer records + - [x] Delete customer records + +- [x] Validate customer record requirements: + - [x] Include name, address, and phone number fields. + - [x] Ensure fields are trimmed and non-empty. + - [x] Restrict phone number field to digits and dashes only. + +- [x] Add exception handling for: + - [x] Add operations + - [x] Update operations + - [x] Delete operations + +## 3. Appointment Management + +- [x] Add functionality to: + - [x] Add appointments + - [x] Update appointments + - [x] Delete appointments + - [x] Capture appointment type + - [x] Link each appointment to a specific customer + +- [x] Validate appointment requirements: + - [x] Appointments must be during business hours (9:00 a.m. – 5:00 p.m., Monday–Friday, EST). + - [x] Prevent scheduling of overlapping appointments. + +- [x] Add exception handling for: + - [x] Add operations + - [x] Update operations + - [x] Delete operations + +## 4. Calendar View + +- [x] Implement a calendar view: + - [x] Allow selection of a day of the month + - [x] Display appointments for that specific day + +## 5. Time Zone Adjustment + +- [x] Automatically adjust appointment times based on: + - [x] User time zones + - [x] Daylight saving time + +## 6. Appointment Alerts + +- [x] Create a function to: + - [x] Generate an alert when a user with an appointment within 15 minutes logs in + +## 7. Reports + +- [ ] Create a report generator using collection classes and lambda expressions: + - [ ] Report: Number of appointment types by month + - [ ] Report: Schedule for each user + - [ ] Report: One additional report of your choice + +## 8. Login History + +- [x] Record each login attempt: + - [x] Include timestamp and username + - [x] Append to “Login_History.txt” file