Beispiel #1
0
        public static async Task InitializeAsync(AppDbContext context, ICovidStatsService covidStatsService)
        {
            context.Database.Migrate();

            if (!context.DailyCovidOverviews.Any())
            {
                DailyCovidOverview[] retrievedDailyCovidOverviews = await covidStatsService.GetInitalPocosAsync();

                for (int i = 0; i < retrievedDailyCovidOverviews.Length; i++)
                {
                    DailyCovidOverview recordToBeAdded;
                    if (i > 0)
                    {
                        recordToBeAdded = DailyCovidOverviewHelpers.PrepareNewRecord(retrievedDailyCovidOverviews[i], retrievedDailyCovidOverviews[i - 1]);
                    }
                    else
                    {
                        recordToBeAdded = DailyCovidOverviewHelpers.PrepareNewFirstRecord(retrievedDailyCovidOverviews[i]);
                    }

                    context.DailyCovidOverviews.Add(recordToBeAdded);
                }


                context.SaveChanges();
            }
        }
        // fetches data for dayD and dayBeforeD to get daily new cases as well.
        private async Task TryUpdatingDataForDateAsync(DateTime dateUtc)
        {
            DailyCovidOverview[] dailyDataExternal = await _covidStatsService.GetPocosByDateAsync(dateUtc);

            DailyCovidOverview externalDayD      = dailyDataExternal.Where(dailyData => dailyData.Date == dateUtc).FirstOrDefault();
            DailyCovidOverview externalDayBefore = dailyDataExternal.Where(dailyData => dailyData.Date == dateUtc.AddDays(-1)).FirstOrDefault();

            // if there is data available at the external server
            if (externalDayD != null)
            {
                // Scope has to be created to access any scoped service.
                using (IServiceScope scope = _services.CreateScope())
                {
                    var context = scope.ServiceProvider.GetRequiredService <AppDbContext>();
                    DailyCovidOverview internalDayD = context.DailyCovidOverviews.Where(dailyData => dailyData.Date == dateUtc).FirstOrDefault();
                    // if there is no data for this day in our db, add new database record
                    if (internalDayD == null)
                    {
                        DailyCovidOverview recordToBePersisted = DailyCovidOverviewHelpers.PrepareNewRecord(externalDayD, externalDayBefore);
                        context.DailyCovidOverviews.Add(recordToBePersisted);
                    }

                    context.SaveChanges();
                }
            }
        }