using C969Project.Data; using C969Project.Data.Models; namespace C969Project; public partial class AppointmentsForm : Form { private List _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? selectedAppointments = _appointments.Where(a => a.Start == date).ToList(); appointmentDataView.DataSource = selectedAppointments?.Count > 0 ? selectedAppointments : null; } }