private static SqlAdunisAppointment ConvertDivAppointment(SqlTimeperiod period, WhAdunisCourse course, AdunisType type)
        {
            // special handling of modules without appointments
            var start = period.Begin.AddDays(-7).SyncWeekday(DayOfWeek.Sunday);

            var appointment = new SqlAdunisAppointment
            {
                Type       = type,
                CourseName = course.Name,
                StartTime  = start.Date,
                EndTime    = period.End,
                TermID     = period.TermID
            };

            appointment.SetId();

            return(appointment);
        }
 public void Init(int termId)
 {
     this.period = this.adunisRepository.RetrieveTimeperiodById(termId);
     this.InitWeeks();
 }
        public async Task <ResultState> UpdateAppointmentsAsync(bool force, SqlTimeperiod period, CancellationToken cancellationToken)
        {
            if (!force && period.LastAppointmentUpdate > DateTime.UtcNow.Date)
            {
                return(ResultState.NotModified);
            }

            if (!this.account.HasAccount)
            {
                return(ResultState.OAuthExpired);
            }

            if (!this.device.HasNetworkConnectivity)
            {
                return(ResultState.ErrorNetwork);
            }

            if (period.Type != AdunisType.Timetable && period.Type != AdunisType.Exam)
            {
                return(ResultState.NoData);
            }

            try
            {
                var courseData = await this.handler.GetCourseDataAsync(period.Identity, period.ParentTermID, period.Type, cancellationToken);

                if (cancellationToken.IsCancellationRequested)
                {
                    return(ResultState.Canceled);
                }

                var appointments = new List <SqlAdunisAppointment>();
                var uniqueDays   = new HashSet <DayOfWeek>();

                if (courseData == null || !courseData.Any())
                {
                    this.repo.DeleteAppointments(period);

                    period.SetUniqueDays(uniqueDays);
                    period.HasAppointments       = false;
                    period.LastAppointmentUpdate = DateTime.UtcNow;

                    this.repo.UpdateTimeperiod(period);
                    return(ResultState.NoData);
                }

                foreach (var course in courseData)
                {
                    foreach (var allocation in course.CourseAllocations)
                    {
                        if (allocation.TimeslotId == SpecialTimeSlotId)
                        {
                            // courses without appointments (SA, BA, Challenge-Projekt)
                            appointments.Add(ConvertDivAppointment(period, course, period.Type));
                        }
                        else
                        {
                            appointments.AddRange(
                                allocation.Appointments.Select(
                                    appointment => ConvertAppointment(period, course, appointment, period.Type)));

                            if (allocation.DayOfWeek.HasValue)
                            {
                                uniqueDays.Add(allocation.DayOfWeek.Value);
                            }
                        }
                    }
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    return(ResultState.Canceled);
                }

                this.repo.PersistAppointments(appointments);

                period.SetUniqueDays(uniqueDays.OrderBy(week => ((int)week + 6) % 7));
                period.HasAppointments       = appointments.Count > 0;
                period.LastAppointmentUpdate = DateTime.UtcNow;

                this.repo.UpdateTimeperiod(period);

                return(ResultState.Success);
            }
            catch (OAuthExpiredException)
            {
                return(ResultState.OAuthExpired);
            }
            catch (Exception ex)
            {
                // most likely network connectivity problems
                this.logging.Exception(this, ex);
                return(ResultState.Error);
            }
        }
 public IEnumerable <SqlAdunisAppointment> RetrieveAppointmentsForDay(SqlTimeperiod period, DateTime date)
 => this.RetrieveAppointmentsForRange(period, date, date.AddDays(1));