コード例 #1
0
        public static async Task UpdateDatabaseFromWebAsync(ComeAndTicketContext db)
        {
            await db.Markets
            .Include(m => m.Theaters)
            .ThenInclude(t => t.ShowTimes)
            .ThenInclude(s => s.UsersUpdated)
            .LoadAsync();

            await db.Movies
            .LoadAsync();

            IEnumerable <Market> marketsFromWeb = await ReadMarketsFromWebAsync();

            await UpdateMarketsAsync(db, marketsFromWeb);

            var theatersByMarket = await Task.WhenAll(
                from market in db.Markets
                select ReadTheatersFromWebAsync(market));

            var theaters = theatersByMarket.SelectMany(t => t);

            await UpdateTheatersAsync(db, theaters);

            var showTimesByTheater = await Task.WhenAll(
                from theater in db.Theaters
                select ReadShowTimesFromWebAsync(theater));

            var showTimes = showTimesByTheater.SelectMany(s => s).ToArray();

            await UpdateShowTimesAsync(db, showTimes);
        }
コード例 #2
0
        private static async Task UpdateShowTimesAsync(ComeAndTicketContext db, IEnumerable <IGrouping <string, ShowTime> > showTimesByMovie)
        {
            await db.Movies.LoadAsync();

            var moviesByTitle = await db.Movies.ToDictionaryAsync(
                m => m.Title,
                m => m,
                StringComparer.CurrentCultureIgnoreCase);

            foreach (var showTimesFromWeb in showTimesByMovie)
            {
                string movieTitle = showTimesFromWeb.Key;
                if (!moviesByTitle.ContainsKey(movieTitle))
                {
                    _logger.Info("Adding movie: {Movie}", movieTitle);
                    var movie = new Movie(movieTitle);
                    db.Movies.Add(movie);
                    moviesByTitle.Add(movieTitle, movie);
                }
            }

            await db.ShowTimes.LoadAsync();

            var showTimesByTicketsUrl = await db.ShowTimes.ToDictionaryAsync(
                s => s.TicketsUrl,
                s => s,
                StringComparer.OrdinalIgnoreCase);

            foreach (IGrouping <string, ShowTime> showTimesFromWeb in showTimesByMovie)
            {
                string movieTitle = showTimesFromWeb.Key;
                IEnumerable <ShowTime> deDupedShowTimesFromWeb = showTimesFromWeb.GroupBy(s => s.TicketsUrl).Select(g => g.First());

                var movieFromDb = moviesByTitle[movieTitle];
                foreach (ShowTime showTimeFromWeb in deDupedShowTimesFromWeb)
                {
                    showTimeFromWeb.Movie = movieFromDb;

                    if (showTimesByTicketsUrl.TryGetValue(showTimeFromWeb.TicketsUrl, out ShowTime showTimeInDb))
                    {
                        if (showTimeInDb.SeatsLeft != showTimeFromWeb.SeatsLeft ||
                            showTimeInDb.Date != showTimeFromWeb.Date ||
                            showTimeInDb.TicketsStatus != showTimeFromWeb.TicketsStatus)
                        {
                            showTimeInDb.LastUpdated = DateTime.UtcNow;
                        }
                        showTimeInDb.SeatsLeft     = showTimeFromWeb.SeatsLeft;
                        showTimeInDb.Date          = showTimeFromWeb.Date;
                        showTimeInDb.TicketsStatus = showTimeFromWeb.TicketsStatus;
                    }
                    else
                    {
                        showTimeFromWeb.Created     = DateTime.UtcNow;
                        showTimeFromWeb.LastUpdated = DateTime.UtcNow;
                        db.ShowTimes.Add(showTimeFromWeb);
                    }
                }
            }
        }
コード例 #3
0
        private static async Task UpdateTheatersAsync(ComeAndTicketContext db, IEnumerable <Theater> theatersFromWeb)
        {
            await db.Theaters.LoadAsync();

            var theatersByUrl = await db.Theaters.ToDictionaryAsync(
                t => t.Url,
                t => t,
                StringComparer.OrdinalIgnoreCase);

            foreach (var theaterFromWeb in theatersFromWeb)
            {
                if (theatersByUrl.TryGetValue(theaterFromWeb.Url, out Theater theaterFromDb))
                {
                    theaterFromDb.Name = theaterFromWeb.Name;
                }
                else
                {
                    db.Theaters.Add(theaterFromWeb);
                }
            }
            await db.SaveChangesAsync();
        }
コード例 #4
0
        private static async ValueTask UpdateMarketsAsync(ComeAndTicketContext db, IEnumerable <Market> marketsFromWeb)
        {
            await db.Markets.LoadAsync();

            var marketsByName = await db.Markets.ToDictionaryAsync(
                m => m.Name,
                m => m,
                StringComparer.CurrentCultureIgnoreCase);

            foreach (var marketFromWeb in marketsFromWeb)
            {
                if (marketsByName.TryGetValue(marketFromWeb.Name, out Market marketFromDb))
                {
                    marketFromDb.Name = marketFromWeb.Name;
                }
                else
                {
                    db.Markets.Add(marketFromWeb);
                }
            }
            await db.SaveChangesAsync();
        }
コード例 #5
0
 public DeleteModel(MaguSoft.ComeAndTicket.Core.Model.ComeAndTicketContext context)
 {
     _context = context;
 }
コード例 #6
0
 public IndexModel(MaguSoft.ComeAndTicket.Core.Model.ComeAndTicketContext context)
 {
     _context = context;
 }