From 1764749ecc7847060ab00297b3e214d595a2ebfb Mon Sep 17 00:00:00 2001 From: Spudnut2000 Date: Thu, 26 Jun 2025 20:20:36 -0500 Subject: [PATCH] User schedules report done --- C969Project/Data/DatabaseHelper.cs | 56 +++++++++++++++++++++ C969Project/Data/Models/User.cs | 5 ++ C969Project/ReportsForm.Designer.cs | 58 ++++++++++++--------- C969Project/ReportsForm.cs | 78 ++++++++++++++++++++++++++--- 4 files changed, 168 insertions(+), 29 deletions(-) diff --git a/C969Project/Data/DatabaseHelper.cs b/C969Project/Data/DatabaseHelper.cs index 53e1ba5..3445ad0 100644 --- a/C969Project/Data/DatabaseHelper.cs +++ b/C969Project/Data/DatabaseHelper.cs @@ -40,6 +40,62 @@ public static class DatabaseHelper return null; } + + public static List RetrieveUsers() + { + 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 users = new List(); + + using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.user", connection); + using MySqlDataReader reader = command.ExecuteReader(); + while (reader.Read()) + { + var user = new User(reader.GetInt32("userId"), reader.GetString("userName")); + + users.Add(user); + } + + return users; + } + + public static User? RetrieveUser(int id) + { + using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); + try + { + connection.Open(); + } + catch (MySqlException e) + { + MessageBox.Show($"Database connection error: {e.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return null; + } + + string query = "SELECT * FROM client_schedule.user WHERE userId = @userId"; + using MySqlCommand command = new MySqlCommand(query, connection); + command.Parameters.AddWithValue("@userId", id); + + using MySqlDataReader reader = command.ExecuteReader(); + + User? user = null; + + while (reader.Read()) + { + user = new User(reader.GetInt32("userId"), reader.GetString("userName")); + } + + return user ?? null; + } #region Customers public static List RetrieveCustomers() diff --git a/C969Project/Data/Models/User.cs b/C969Project/Data/Models/User.cs index 83c801a..0343ff1 100644 --- a/C969Project/Data/Models/User.cs +++ b/C969Project/Data/Models/User.cs @@ -10,4 +10,9 @@ public class User UserId = id; Username = username; } + + public override string ToString() + { + return Username; + } } \ No newline at end of file diff --git a/C969Project/ReportsForm.Designer.cs b/C969Project/ReportsForm.Designer.cs index 8f860f8..38972aa 100644 --- a/C969Project/ReportsForm.Designer.cs +++ b/C969Project/ReportsForm.Designer.cs @@ -36,14 +36,15 @@ partial class ReportsForm refreshButton1 = new System.Windows.Forms.Button(); apptByMonthDataGrid = new System.Windows.Forms.DataGridView(); userSchedulesTabPage = new System.Windows.Forms.TabPage(); - customersByCountryTabPage = new System.Windows.Forms.TabPage(); - scheduleDataGrid = new System.Windows.Forms.DataGridView(); + userScheduleDataGrid = new System.Windows.Forms.DataGridView(); + userComboBox = new System.Windows.Forms.ComboBox(); refreshButton2 = new System.Windows.Forms.Button(); + customersByCountryTabPage = new System.Windows.Forms.TabPage(); tabControl1.SuspendLayout(); apptByMothTabPage.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)apptByMonthDataGrid).BeginInit(); userSchedulesTabPage.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)scheduleDataGrid).BeginInit(); + ((System.ComponentModel.ISupportInitialize)userScheduleDataGrid).BeginInit(); SuspendLayout(); // // tabControl1 @@ -88,8 +89,9 @@ partial class ReportsForm // // userSchedulesTabPage // + userSchedulesTabPage.Controls.Add(userScheduleDataGrid); + userSchedulesTabPage.Controls.Add(userComboBox); userSchedulesTabPage.Controls.Add(refreshButton2); - userSchedulesTabPage.Controls.Add(scheduleDataGrid); userSchedulesTabPage.Location = new System.Drawing.Point(4, 24); userSchedulesTabPage.Name = "userSchedulesTabPage"; userSchedulesTabPage.Padding = new System.Windows.Forms.Padding(3); @@ -98,6 +100,30 @@ partial class ReportsForm userSchedulesTabPage.Text = "User Schedules"; userSchedulesTabPage.UseVisualStyleBackColor = true; // + // userScheduleDataGrid + // + userScheduleDataGrid.Location = new System.Drawing.Point(6, 35); + userScheduleDataGrid.Name = "userScheduleDataGrid"; + userScheduleDataGrid.Size = new System.Drawing.Size(756, 357); + userScheduleDataGrid.TabIndex = 5; + // + // userComboBox + // + userComboBox.FormattingEnabled = true; + userComboBox.Location = new System.Drawing.Point(6, 6); + userComboBox.Name = "userComboBox"; + userComboBox.Size = new System.Drawing.Size(660, 23); + userComboBox.TabIndex = 4; + // + // refreshButton2 + // + refreshButton2.Location = new System.Drawing.Point(672, 6); + refreshButton2.Name = "refreshButton2"; + refreshButton2.Size = new System.Drawing.Size(90, 23); + refreshButton2.TabIndex = 3; + refreshButton2.Text = "Refresh"; + refreshButton2.UseVisualStyleBackColor = true; + // // customersByCountryTabPage // customersByCountryTabPage.Location = new System.Drawing.Point(4, 24); @@ -107,23 +133,6 @@ partial class ReportsForm customersByCountryTabPage.Text = "Customers By Country"; customersByCountryTabPage.UseVisualStyleBackColor = true; // - // scheduleDataGrid - // - scheduleDataGrid.Location = new System.Drawing.Point(6, 6); - scheduleDataGrid.Name = "scheduleDataGrid"; - scheduleDataGrid.ReadOnly = true; - scheduleDataGrid.Size = new System.Drawing.Size(756, 357); - scheduleDataGrid.TabIndex = 2; - // - // refreshButton2 - // - refreshButton2.Location = new System.Drawing.Point(672, 369); - refreshButton2.Name = "refreshButton2"; - refreshButton2.Size = new System.Drawing.Size(90, 23); - refreshButton2.TabIndex = 3; - refreshButton2.Text = "Refresh"; - refreshButton2.UseVisualStyleBackColor = true; - // // ReportsForm // AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -135,11 +144,14 @@ partial class ReportsForm apptByMothTabPage.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)apptByMonthDataGrid).EndInit(); userSchedulesTabPage.ResumeLayout(false); - ((System.ComponentModel.ISupportInitialize)scheduleDataGrid).EndInit(); + ((System.ComponentModel.ISupportInitialize)userScheduleDataGrid).EndInit(); ResumeLayout(false); } - private System.Windows.Forms.DataGridView scheduleDataGrid; + private System.Windows.Forms.DataGridView userScheduleDataGrid; + + private System.Windows.Forms.ComboBox userComboBox; + private System.Windows.Forms.Button refreshButton2; private System.Windows.Forms.Button refreshButton1; diff --git a/C969Project/ReportsForm.cs b/C969Project/ReportsForm.cs index 3a5b8bb..81268b0 100644 --- a/C969Project/ReportsForm.cs +++ b/C969Project/ReportsForm.cs @@ -1,4 +1,5 @@ using C969Project.Data; +using C969Project.Data.Models; namespace C969Project; @@ -21,6 +22,15 @@ public partial class ReportsForm : Form ApptTypeByMonth_GenerateReport(); refreshButton1.Click += (sender, args) => { ApptTypeByMonth_GenerateReport(); }; + + UserSchedule_UpdateUsers(); + userComboBox.SelectedIndexChanged += (sender, args) => { UserSchedule_GenerateReport(); }; + userComboBox.SelectedIndex = -1; // Reset selection + refreshButton2.Click += (sender, args) => + { + UserSchedule_UpdateUsers(); + UserSchedule_GenerateReport(); + }; } #region Apptointments types by Month @@ -42,11 +52,8 @@ public partial class ReportsForm : Form apptByMonthDataGrid.DataSource = appointmentTypes; } - - #endregion - - class AppointmentTypeByMonth + private class AppointmentTypeByMonth { public string Month { get; set; } public string AppointmentType { get; set; } @@ -58,10 +65,69 @@ public partial class ReportsForm : Form AppointmentType = appointmentType; Count = count; } + } + + #endregion + + #region User Schedule + + private void UserSchedule_GenerateReport() + { + userScheduleDataGrid.DataSource = null; - public override string ToString() + User? selectedUser = (User?)userComboBox.SelectedItem; + + List userSchedules = new(); + var appointments = DatabaseHelper.RetrieveAppointments(); + + var usersAppointments = appointments + .FindAll(a => a.UserId == selectedUser.UserId) + .ToList(); + + if (usersAppointments.Count == 0) { - return $"{Month} - {AppointmentType}: {Count}"; + MessageBox.Show("No appointments found for the selected user.", "No Appointments", MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + foreach (var appointment in usersAppointments) + { + userSchedules.Add(new UserSchedule( + selectedUser.Username, + appointment.Title, + appointment.AppointmentType, + appointment.Start.ToLocalTime().ToString("MM/dd/yyyy hh:mm ") + TimeZoneInfo.Local.Id, + appointment.End.ToLocalTime().ToString("MM/dd/yyyy hh:mm ") + TimeZoneInfo.Local.Id + )); + } + + userScheduleDataGrid.DataSource = userSchedules; + } + + private void UserSchedule_UpdateUsers() + { + userComboBox.DataSource = null; + userComboBox.DataSource = DatabaseHelper.RetrieveUsers(); + userComboBox.SelectedIndex = -1; + } + + private class UserSchedule + { + public string Username { get; set; } + public string AppointmentName { get; set; } + public string AppointmentType { get; set; } + public string StartDate { get; set; } + public string EndDate { get; set; } + + public UserSchedule(string username, string appointmentName, string appointmentType, string startDate, string endDate) + { + Username = username; + AppointmentName = appointmentType; + AppointmentType = appointmentType; + StartDate = startDate; + EndDate = endDate; } } + + #endregion } \ No newline at end of file