76 lines
2.5 KiB
C#
76 lines
2.5 KiB
C#
using C969Project.Data;
|
|
using C969Project.Data.Models;
|
|
|
|
namespace C969Project;
|
|
|
|
public partial class AppointmentsForm : Form
|
|
{
|
|
private List<Appointment> _appointments = new();
|
|
private AddOrUpdateAppointmentForm _addOrUpdateAppointmentForm = new();
|
|
|
|
public AppointmentsForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
Shown += UpdateAppointmentsList;
|
|
|
|
calendar.DateSelected += ShowAppointmentsFromSelectedDay;
|
|
showAllButton.Click += UpdateAppointmentsList;
|
|
deleteButton.Click += DeleteButtonOnClick;
|
|
addButton.Click += AddButtonOnClick;
|
|
modifyButton.Click += ModifyButtonOnClick;
|
|
}
|
|
|
|
private void ModifyButtonOnClick(object? sender, EventArgs e)
|
|
{
|
|
if (appointmentDataView.CurrentRow?.DataBoundItem is Appointment selectedAppointment)
|
|
{
|
|
_addOrUpdateAppointmentForm.InitUpdate(selectedAppointment);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Error while trying to modify appointment");
|
|
}
|
|
}
|
|
|
|
private void AddButtonOnClick(object? sender, EventArgs e)
|
|
{
|
|
_addOrUpdateAppointmentForm.InitAdd();
|
|
}
|
|
|
|
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.RetrieveAppointments(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;
|
|
}
|
|
} |