private void ValidateAppt(DataLayer.appointment appt)
 {
     using (var ctx = new DataLayer.ScheduleEntities())
     {
         string CurrentUserName = dbcontext.users.Find(mainForm.CurrentUser).userName;
         // Lambda with LINQ query for concise and efficient queries
         bool overlapping = ctx.appointments
                            .Where(a =>
                                   a.createdBy == CurrentUserName && (a.start > appt.start && a.start < appt.end) ||
                                   (a.end <appt.end && a.start> appt.start)
                                   )
                            .Any();
         if (overlapping)
         {
             throw new WorkingTooHardException("Appointment overlaps with existing appointment");
         }
         if (
             appt.start.ToLocalTime().Hour < 9 ||
             appt.start.ToLocalTime().Hour > 17 ||
             appt.start.ToLocalTime().Day != appt.end.ToLocalTime().Day
             )
         {
             throw new WorkingTooHardException("Appointments must be within business hours");
         }
         if (appt.start.ToLocalTime() > appt.end.ToLocalTime())
         {
             throw new TimeTravelImpossibleException("Appointment end cannot be before appointment start");
         }
     }
 }
        public MakeAppointment(MainScreen mainForm, int apptId)
            : this(mainForm)
        {
            try
            {
                // Lambda with LINQ query for concise and efficient queries
                appt = dbcontext.appointments
                       .Where(a => a.appointmentId == apptId)
                       .FirstOrDefault();
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("An appointment to modify could not be loaded");
                this.Hide();
            }

            // fill form fields
            textBoxTitle.Text = appt.title;
            textBoxDesc.Text  = appt.description;
            comboBoxCustomer.SelectedValue = appt.customerId;
            textBoxContact.Text            = appt.contact;
            comboBoxType.SelectedItem      = appt.type;
            dateTimePickerStart.Value      = appt.start.ToLocalTime();
            dateTimePickerEnd.Value        = appt.end.ToLocalTime();
            textBoxLocation.Text           = appt.location;
            textBoxURL.Text = appt.url;
            this.apptId     = appt.appointmentId;
        }