public async Task OnScheduleStart(
            [TimerTrigger("0 0 * * *", RunOnStartup = true)] TimerInfo timerInfo,
            [DurableClient] IDurableOrchestrationClient starter,
            ILogger log)
        {
            var regions = new[] { "en-US" /*, "pt-BR"*/ };

            try
            {
                foreach (var region in regions)
                {
                    _playstationWishlistContext.PlaystationGames.RemoveRange(_playstationWishlistContext.PlaystationGames.Where(g => g.Region == region));

                    foreach (var game in await PlaystationStoreScrapper.GetAllGamesByRegion(region))
                    {
                        await _playstationWishlistContext.PlaystationGames.AddAsync(_mapper.Map <PlaystationWishlist.DataAccess.Models.PlaystationGame>(game));
                    }

                    Console.WriteLine("Saving changes to database.");
                    await _playstationWishlistContext.SaveChangesAsync();

                    Console.WriteLine("Preparing to send e-mail.");
                    var discountedGames = _playstationWishlistContext.PlaystationGames.Where(g => g.OriginalPrice != null);

                    foreach (var discountedGame in discountedGames.ToList())
                    {
                        discountedGame.DiscountPercentage = CalculateDiscountPercentage(discountedGame);

                        var wishlistItemsForGame = _playstationWishlistContext.WishlistItems.Where(item => item.GameUrl == discountedGame.Url);

                        foreach (var wishlistItem in wishlistItemsForGame)
                        {
                            var user = _identityAppContext.Users.FirstOrDefault(u => u.Id == wishlistItem.UserId);
                            if (user != null)
                            {
                                Console.WriteLine($"Sending e-mail to {user.Email} about {discountedGame.Name}");
                                Sender.Send(discountedGame, user);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex, "ProcessTimerAction error.");
                throw;
            }

            Task.WaitAll(_tasks.ToArray());
        }
예제 #2
0
        // ReSharper disable once UnusedMember.Global
        public async Task ProcessTimerAction([TimerTrigger("0 0 * * *", RunOnStartup = true)] TimerInfo timerInfo, ILogger logger)
        {
            try
            {
                var playstationWishlistContext = (PlaystationWishlistContext)ServiceLocator.Instance.GetService(typeof(PlaystationWishlistContext));
                var identityContext            = (IdentityAppContext)ServiceLocator.Instance.GetService(typeof(IdentityAppContext));

                var regions = new[] { "en-US" /*, "pt-BR"*/ };

                playstationWishlistContext.PlaystationGames.RemoveRange(playstationWishlistContext.PlaystationGames);

                foreach (var region in regions)
                {
                    foreach (var game in await PlaystationStoreScrapper.GetAllGamesByRegion(region))
                    {
                        await playstationWishlistContext.PlaystationGames.AddAsync(_mapper.Map <PlaystationWishlist.DataAccess.Models.PlaystationGame>(game));
                    }
                }

                var discountedGames = playstationWishlistContext.PlaystationGames.Where(g => g.OriginalPrice != null);

                foreach (var discountedGame in discountedGames.ToList())
                {
                    discountedGame.DiscountPercentage = CalculateDiscountPercentage(discountedGame);

                    var wishlistItemsForGame = playstationWishlistContext.WishlistItems.Where(item => item.GameUrl == discountedGame.Url);

                    foreach (var wishlistItem in wishlistItemsForGame)
                    {
                        var user = identityContext.Users.FirstOrDefault(u => u.Id == wishlistItem.UserId);
                        if (user != null)
                        {
                            Console.WriteLine($"Sending e-mail to {user.Email} about {discountedGame.Name}");
                            Sender.Send(discountedGame, user);
                        }
                    }
                }

                Console.WriteLine("Saving changes to database.");
                await playstationWishlistContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "ProcessTimerAction error.");
                throw;
            }
        }