View and delete appointments

This commit is contained in:
Spudnut2000 2025-06-19 22:27:18 -05:00
parent cef0cb9451
commit aad159f026
5 changed files with 138 additions and 55 deletions

View File

@ -31,74 +31,92 @@ partial class AppointmentsForm
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
dataGridView1 = new System.Windows.Forms.DataGridView(); appointmentDataView = new System.Windows.Forms.DataGridView();
monthCalendar1 = new System.Windows.Forms.MonthCalendar(); calendar = new System.Windows.Forms.MonthCalendar();
button1 = new System.Windows.Forms.Button(); addButton = new System.Windows.Forms.Button();
button2 = new System.Windows.Forms.Button(); modifyButton = new System.Windows.Forms.Button();
button3 = new System.Windows.Forms.Button(); deleteButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit(); showAllButton = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)appointmentDataView).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// dataGridView1 // appointmentDataView
// //
dataGridView1.Location = new System.Drawing.Point(12, 11); appointmentDataView.AllowUserToAddRows = false;
dataGridView1.Name = "dataGridView1"; appointmentDataView.AllowUserToDeleteRows = false;
dataGridView1.Size = new System.Drawing.Size(739, 375); appointmentDataView.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right));
dataGridView1.TabIndex = 0; appointmentDataView.Location = new System.Drawing.Point(12, 11);
appointmentDataView.Name = "appointmentDataView";
appointmentDataView.ReadOnly = true;
appointmentDataView.Size = new System.Drawing.Size(739, 523);
appointmentDataView.TabIndex = 0;
// //
// monthCalendar1 // calendar
// //
monthCalendar1.Location = new System.Drawing.Point(763, 11); calendar.Anchor = ((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right));
monthCalendar1.Name = "monthCalendar1"; calendar.Location = new System.Drawing.Point(773, 11);
monthCalendar1.TabIndex = 1; calendar.Name = "calendar";
calendar.TabIndex = 1;
// //
// button1 // addButton
// //
button1.Location = new System.Drawing.Point(12, 392); addButton.Location = new System.Drawing.Point(12, 540);
button1.Name = "button1"; addButton.Name = "addButton";
button1.Size = new System.Drawing.Size(75, 23); addButton.Size = new System.Drawing.Size(75, 23);
button1.TabIndex = 2; addButton.TabIndex = 2;
button1.Text = "Add"; addButton.Text = "Add";
button1.UseVisualStyleBackColor = true; addButton.UseVisualStyleBackColor = true;
// //
// button2 // modifyButton
// //
button2.Location = new System.Drawing.Point(93, 392); modifyButton.Location = new System.Drawing.Point(93, 540);
button2.Name = "button2"; modifyButton.Name = "modifyButton";
button2.Size = new System.Drawing.Size(75, 23); modifyButton.Size = new System.Drawing.Size(75, 23);
button2.TabIndex = 3; modifyButton.TabIndex = 3;
button2.Text = "Modify"; modifyButton.Text = "Modify";
button2.UseVisualStyleBackColor = true; modifyButton.UseVisualStyleBackColor = true;
// //
// button3 // deleteButton
// //
button3.Location = new System.Drawing.Point(676, 392); deleteButton.Location = new System.Drawing.Point(676, 540);
button3.Name = "button3"; deleteButton.Name = "deleteButton";
button3.Size = new System.Drawing.Size(75, 23); deleteButton.Size = new System.Drawing.Size(75, 23);
button3.TabIndex = 4; deleteButton.TabIndex = 4;
button3.Text = "Delete"; deleteButton.Text = "Delete";
button3.UseVisualStyleBackColor = true; deleteButton.UseVisualStyleBackColor = true;
//
// showAllButton
//
showAllButton.Location = new System.Drawing.Point(773, 185);
showAllButton.Name = "showAllButton";
showAllButton.Size = new System.Drawing.Size(227, 23);
showAllButton.TabIndex = 5;
showAllButton.Text = "Show All Appointments";
showAllButton.UseVisualStyleBackColor = true;
// //
// AppointmentsForm // AppointmentsForm
// //
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(1008, 625); ClientSize = new System.Drawing.Size(1008, 575);
Controls.Add(button3); Controls.Add(showAllButton);
Controls.Add(button2); Controls.Add(deleteButton);
Controls.Add(button1); Controls.Add(modifyButton);
Controls.Add(monthCalendar1); Controls.Add(addButton);
Controls.Add(dataGridView1); Controls.Add(calendar);
Controls.Add(appointmentDataView);
Text = "AppointmentsForm"; Text = "AppointmentsForm";
((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit(); ((System.ComponentModel.ISupportInitialize)appointmentDataView).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.Button showAllButton;
private System.Windows.Forms.MonthCalendar monthCalendar1;
private System.Windows.Forms.Button button1; private System.Windows.Forms.DataGridView appointmentDataView;
private System.Windows.Forms.Button button2; private System.Windows.Forms.MonthCalendar calendar;
private System.Windows.Forms.Button button3; private System.Windows.Forms.Button addButton;
private System.Windows.Forms.Button modifyButton;
private System.Windows.Forms.Button deleteButton;
#endregion #endregion
} }

View File

@ -1,9 +1,56 @@
using C969Project.Data;
using C969Project.Data.Models;
namespace C969Project; namespace C969Project;
public partial class AppointmentsForm : Form public partial class AppointmentsForm : Form
{ {
private List<Appointment> _appointments = new();
public AppointmentsForm() public AppointmentsForm()
{ {
InitializeComponent(); InitializeComponent();
Shown += UpdateAppointmentsList;
calendar.DateSelected += ShowAppointmentsFromSelectedDay;
showAllButton.Click += UpdateAppointmentsList;
deleteButton.Click += DeleteButtonOnClick;
}
private void DeleteButtonOnClick(object? sender, EventArgs e)
{
Appointment? selectedAppointment;
if (appointmentDataView.CurrentRow?.DataBoundItem is Appointment appointment)
{
selectedAppointment = appointment;
}
else
{
MessageBox.Show("Error while trying to delete appointment");
return;
}
var result = MessageBox.Show("Are you sure you want to delete this appointment?", "Delete Appointment?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (result == DialogResult.OK)
{
DatabaseHelper.DeleteAppointment(selectedAppointment);
UpdateAppointmentsList(null, EventArgs.Empty);
}
}
private void UpdateAppointmentsList(object? sender, EventArgs e)
{
appointmentDataView.DataSource = null;
_appointments.Clear();
_appointments.AddRange(DatabaseHelper.RetrieveAllAppointments(AppState.CurrentUser.UserId));
appointmentDataView.DataSource = _appointments;
}
private void ShowAppointmentsFromSelectedDay(object? sender, EventArgs e)
{
var date = calendar.SelectionStart;
List<Appointment>? selectedAppointments = _appointments.Where(a => a.Start == date).ToList();
appointmentDataView.DataSource = selectedAppointments?.Count > 0 ? selectedAppointments : null;
} }
} }

View File

@ -3,19 +3,26 @@ namespace C969Project;
public partial class DashboardForm : Form public partial class DashboardForm : Form
{ {
private RecordsForm _recordsForm; private RecordsForm _recordsForm;
private AppointmentsForm _appointmentsForm;
public DashboardForm() public DashboardForm()
{ {
InitializeComponent(); InitializeComponent();
_recordsForm = new RecordsForm(); _recordsForm = new RecordsForm();
_appointmentsForm = new AppointmentsForm();
CustomersButton.Click += (sender, args) => CustomersButton.Click += (sender, args) =>
{ {
_recordsForm.ShowDialog(); _recordsForm.ShowDialog();
_recordsForm.UpdateCustomersList(null, EventArgs.Empty); _recordsForm.UpdateCustomersList(null, EventArgs.Empty);
}; };
AppointmentsButton.Click += (sender, args) =>
{
_appointmentsForm.ShowDialog();
};
} }
} }

View File

@ -421,7 +421,7 @@ namespace C969Project.Data
return null; return null;
} }
public static List<Appointment> RetrieveAllAppointments() public static List<Appointment> RetrieveAllAppointments(int? userId = null)
{ {
using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection")); using MySqlConnection connection = new MySqlConnection(AppSettings.GetSetting("ConnectionStrings", "DefaultConnection"));
try try
@ -436,7 +436,11 @@ namespace C969Project.Data
List<Appointment> appointments = new List<Appointment>(); List<Appointment> appointments = new List<Appointment>();
using MySqlCommand command = new MySqlCommand("SELECT * FROM client_schedule.appointment", connection); 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(); using MySqlDataReader reader = command.ExecuteReader();
while (reader.Read()) while (reader.Read())
{ {

View File

@ -13,6 +13,8 @@ public partial class RecordsForm : Form
InitializeComponent(); InitializeComponent();
UpdateCustomersList(null, EventArgs.Empty); UpdateCustomersList(null, EventArgs.Empty);
Shown += UpdateCustomersList;
AddButton.Click += (sender, args) => AddButton.Click += (sender, args) =>
{ {
_form.InitAdd(); _form.InitAdd();
@ -29,7 +31,14 @@ public partial class RecordsForm : Form
DeleteButton.Click += (sender, args) => DeleteButton.Click += (sender, args) =>
{ {
var selectedCustomer = _customers[CustomersDataGrid.CurrentCell.RowIndex]; if (CustomersDataGrid.CurrentCell is null || CustomersDataGrid.CurrentCell.RowIndex > _customers?.Count)
{
MessageBox.Show("Please select a customer to delete");
return;
}
var selectedCustomer = _customers?[CustomersDataGrid.CurrentCell.RowIndex];
var result = MessageBox.Show("Are you sure you want to delete this record?", "Delete Record", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); var result = MessageBox.Show("Are you sure you want to delete this record?", "Delete Record", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (result == DialogResult.OK) if (result == DialogResult.OK)
{ {
@ -39,8 +48,6 @@ public partial class RecordsForm : Form
} }
}; };
} }
public void UpdateCustomersList(object? sender, EventArgs args) public void UpdateCustomersList(object? sender, EventArgs args)
{ {