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

View File

@ -1,9 +1,56 @@
using C969Project.Data;
using C969Project.Data.Models;
namespace C969Project;
public partial class AppointmentsForm : Form
{
private List<Appointment> _appointments = new();
public AppointmentsForm()
{
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
{
private RecordsForm _recordsForm;
private AppointmentsForm _appointmentsForm;
public DashboardForm()
{
InitializeComponent();
_recordsForm = new RecordsForm();
_appointmentsForm = new AppointmentsForm();
CustomersButton.Click += (sender, args) =>
{
_recordsForm.ShowDialog();
_recordsForm.UpdateCustomersList(null, EventArgs.Empty);
};
AppointmentsButton.Click += (sender, args) =>
{
_appointmentsForm.ShowDialog();
};
}
}

View File

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

View File

@ -13,6 +13,8 @@ public partial class RecordsForm : Form
InitializeComponent();
UpdateCustomersList(null, EventArgs.Empty);
Shown += UpdateCustomersList;
AddButton.Click += (sender, args) =>
{
_form.InitAdd();
@ -29,7 +31,14 @@ public partial class RecordsForm : Form
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);
if (result == DialogResult.OK)
{
@ -39,8 +48,6 @@ public partial class RecordsForm : Form
}
};
}
public void UpdateCustomersList(object? sender, EventArgs args)
{