private void CreateAppointments(ScheduledHour entity)
        {
            DateTime startTime         = DateTime.Now;
            DateTime currentDate       = entity.StartDate;
            DateTime endDate           = entity.EndDate;
            var      appointmentLength = new TimeSpan(0, 15, 0);

            endDate = endDate.AddDays(1);
            while (currentDate != endDate)
            {
                if (CheckDayOfWeek(entity, currentDate))
                {
                    var currentTime = entity.StartTime;
                    while (currentTime < entity.EndTime)
                    {
                        var appointment = new Appointment();
                        appointment.ProfessorID = entity.ProfessorID;
                        appointment.Status      = Appointment.StatusType.Open;
                        appointment.DateTime    = currentDate + currentTime;
                        appointment.ModifiedAt  = DateTime.Now;
                        appointment.CreatedAt   = DateTime.Now;
                        _context.Appointments.Add(appointment);
                        currentTime = currentTime.Add(appointmentLength);
                    }
                    _context.SaveChanges();
                }
                currentDate = currentDate.AddDays(1);
            }
        }
 private bool CheckDayOfWeek(ScheduledHour entity, DateTime currentDate)
 {
     return((entity.Monday && currentDate.DayOfWeek == DayOfWeek.Monday) ||
            (entity.Tuesday && currentDate.DayOfWeek == DayOfWeek.Tuesday) ||
            (entity.Wednesday && currentDate.DayOfWeek == DayOfWeek.Wednesday) ||
            (entity.Thursday && currentDate.DayOfWeek == DayOfWeek.Thursday) ||
            (entity.Friday && currentDate.DayOfWeek == DayOfWeek.Friday));
 }
 public Object CreateScheduledHour(ScheduledHour entity)
 {
     entity.CreatedAt  = DateTime.Now;
     entity.ModifiedAt = DateTime.Now;
     _context.ScheduledHours.Add(entity);
     _context.SaveChanges();
     if (entity.TypeID == ScheduledHour.ScheduledHourType.OfficeHour)
     {
         CreateAppointments(entity);
         return(new { success = true, message = "Office hours scheduled", ScheduledHour = entity });
     }
     else if (entity.TypeID == ScheduledHour.ScheduledHourType.Cancellation)
     {
         CancelAppointments(entity);
         return(new { success = true, message = "Office hours cancelled", ScheduledHour = entity });
     }
     return(new { success = false, message = "Office hour type invalid", ScheduledHour = entity });
 }
        private void Cancel(ScheduledHour entity, Appointment.StatusType status)
        {
            DateTime startTime         = DateTime.Now;
            DateTime currentDate       = entity.StartDate;
            DateTime endDate           = entity.EndDate;
            var      appointmentLength = new TimeSpan(0, 15, 0);

            endDate = endDate.AddDays(1);
            while (currentDate != endDate)
            {
                if (CheckDayOfWeek(entity, currentDate))
                {
                    var currentTime = entity.StartTime;
                    while (currentTime < entity.EndTime)
                    {
                        var currentDateTime = currentDate + currentTime;

                        var appointment = _context.Appointments.FirstOrDefault(a => a.DateTime == currentDateTime && a.ProfessorID == entity.ProfessorID);
                        if (appointment != null)
                        {
                            if (status == Appointment.StatusType.Cancelled)
                            {
                                if (appointment.Status != Appointment.StatusType.Open || appointment.Status != Appointment.StatusType.Cancelled)
                                {
                                    emailAppointmentCancelled(appointment);
                                }
                            }
                            appointment.ModifiedAt = DateTime.Now;

                            appointment.Status = status;
                            _context.Appointments.Update(appointment);
                        }


                        currentTime = currentTime.Add(appointmentLength);
                    }
                    _context.SaveChanges();
                }

                currentDate = currentDate.AddDays(1);
            }
        }
        private void DeleteAppointments(ScheduledHour entity)
        {
            DateTime startTime         = DateTime.Now;
            DateTime currentDate       = entity.StartDate;
            DateTime endDate           = entity.EndDate;
            var      appointmentLength = new TimeSpan(0, 15, 0);

            endDate = endDate.AddDays(1);
            while (currentDate != endDate)
            {
                if (CheckDayOfWeek(entity, currentDate))
                {
                    var currentTime = entity.StartTime;
                    while (currentTime < entity.EndTime)
                    {
                        var currentDateTime = currentDate + currentTime;
                        var appointment     = _context.Appointments.Where(a => a.DateTime == currentDateTime && a.ProfessorID == entity.ProfessorID).Include(a => a.Professor).FirstOrDefault();
                        if (appointment != null)
                        {
                            if (appointment.Status != Appointment.StatusType.Open || appointment.Status != Appointment.StatusType.Cancelled)
                            {
                                AppointmentCancellation appointmentCancellation = new AppointmentCancellation {
                                    FirstName = appointment.FirstName, LastName = appointment.LastName, DateTime = appointment.DateTime, Created = DateTime.Now, Status = appointment.Status, Reason = "Professor Cancelled Office Hour", Email = appointment.Email, ProfessorID = appointment.ProfessorID
                                };
                                emailAppointmentCancelled(appointment);
                            }

                            _context.Appointments.Remove(appointment);
                        }

                        currentTime = currentTime.Add(appointmentLength);
                    }
                    _context.SaveChanges();
                }
                currentDate = currentDate.AddDays(1);
            }
        }
 private void UncancelAppointments(ScheduledHour entity)
 {
     Cancel(entity, Appointment.StatusType.Open);
 }
 private void CancelAppointments(ScheduledHour entity)
 {
     Cancel(entity, Appointment.StatusType.Cancelled);
 }
 public IActionResult CreateScheduledHour([FromBody] ScheduledHour scheduledHour)
 {
     return(new ObjectResult(_respository.CreateScheduledHour(scheduledHour)));
 }