public async Task <IEnumerable <ScheduleDiff> > ProcessAsync(CancellationToken ct = default)
        {
            var indexPage = await _flexKidsClient.GetAvailableSchedulesPage(ct);

            IndexContent indexContent          = _parser.GetIndexContent(indexPage);
            var          weekSchedulesToImport = new List <WeekAndImportedSchedules>(indexContent.Weeks.Count);

            foreach (KeyValuePair <int, WeekItem> item in indexContent.Weeks)
            {
                var htmlComboboxIndex = item.Key;
                var year       = item.Value.Year;
                var weekNumber = item.Value.WeekNr;

                var htmlSchedule = await _flexKidsClient.GetSchedulePage(htmlComboboxIndex, ct);

                var parsedSchedules = _parser.GetScheduleFromContent(htmlSchedule, year).ToList();

                WeekSchedule weekSchedule = await _repo.Get(year, weekNumber);

                weekSchedulesToImport.Add(new WeekAndImportedSchedules
                {
                    WeekSchedule  = weekSchedule,
                    ScheduleItems = parsedSchedules,
                });
            }

            return(await ProcessRawData(weekSchedulesToImport));
        }
Esempio n. 2
0
        public IEnumerable <ScheduleDiff> GetChanges()
        {
            var rooterFirstPage  = flexKidsConnection.GetAvailableSchedulesPage();
            var indexContent     = parser.GetIndexContent(rooterFirstPage);
            var somethingChanged = false;
            var weekAndHtml      = new Dictionary <int, WeekAndHtml>(indexContent.Weeks.Count);

            foreach (var i in indexContent.Weeks)
            {
                var htmlSchedule = flexKidsConnection.GetSchedulePage(i.Key);
                var htmlHash     = hash.Hash(htmlSchedule);
                var week         = repo.GetWeek(i.Value.Year, i.Value.WeekNr);

                if (week == null || htmlHash != week.Hash)
                {
                    somethingChanged = true;
                }

                weekAndHtml.Add(i.Key, new WeekAndHtml
                {
                    Week            = GetCreateOrUpdateWeek(week, i.Value.Year, i.Value.WeekNr, htmlHash),
                    Hash            = htmlHash,
                    Html            = htmlSchedule,
                    ScheduleChanged = (week == null || htmlHash != week.Hash)
                });
            }

            if (somethingChanged == false)
            {
                return(Enumerable.Empty <ScheduleDiff>());
            }

            var diffsResult = new List <ScheduleDiff>();

            foreach (var item in weekAndHtml.Select(a => a.Value))
            {
                var dbSchedules = repo.GetSchedules(item.Week.Year, item.Week.WeekNr);
                IList <ScheduleDiff> diffResult;
                if (item.ScheduleChanged)
                {
                    var parsedSchedules = parser.GetScheduleFromContent(item.Html, item.Week.Year);
                    diffResult = GetDiffs(dbSchedules, parsedSchedules, item.Week);

                    var schedulesToDelete = diffResult
                                            .Where(x => x.Status == ScheduleStatus.Removed)
                                            .Select(x => x.Schedule);
                    repo.Delete(schedulesToDelete);

                    var schedulesToInsert = diffResult
                                            .Where(x => x.Status == ScheduleStatus.Added)
                                            .Select(x => x.Schedule);
                    foreach (var schedule in schedulesToInsert)
                    {
                        repo.Insert(schedule);
                    }

                    OnScheduleChanged(diffResult.OrderBy(x => x.Start).ThenBy(x => x.Status));
                }
                else
                {
                    diffResult = new List <ScheduleDiff>(dbSchedules.Count);
                    foreach (var dbSchedule in dbSchedules)
                    {
                        diffResult.Add(new ScheduleDiff
                        {
                            Schedule = dbSchedule,
                            Status   = ScheduleStatus.Unchanged
                        });
                    }
                }
                diffsResult.AddRange(diffResult);
            }

            return(diffsResult);
        }