Ejemplo n.º 1
0
        internal void OnActionExecuting(
            ActionExecutingContext filterContext, 
            ApplicationUserManager userManager,
            ClientIdCalculator clientIdCalculator)
        {
            if (filterContext.ActionParameters.ContainsKey(USER_CONTEXT_KEY))
            {
                ApplicationUser applicationUser;

                if (filterContext.HttpContext.User.Identity.IsAuthenticated)
                {
                    string userId = filterContext.HttpContext.User.Identity.GetUserId();
                    applicationUser = userManager.FindByIdAsync(userId).Result;
                }else
                {
                    applicationUser = new AnonymousApplicationUser();
                    if (this.RequiresGamingGroup)
                    {
                        filterContext.Result = new RedirectToRouteResult(MVC.Account.Login().GetRouteValueDictionary());
                    }
                }

                applicationUser.AnonymousClientId = clientIdCalculator.GetClientId(filterContext.HttpContext.Request, applicationUser);

                filterContext.ActionParameters[USER_CONTEXT_KEY] = applicationUser;
            }

            base.OnActionExecuting(filterContext);
        }
        private int UpdateBoardGameGeekDefinitions(List<BoardGameGeekGameDefinition> boardGameGeekGameDefinitions)
        {
            var anonymousUser = new AnonymousApplicationUser();
            int totalGamesUpdated = 0;
            foreach (var existingBoardGameGeekGameDefinition in boardGameGeekGameDefinitions)
            {
                //delay between BGG calls to decrease likelyhood of getting blocked by BGG
                Thread.Sleep(400);
                var gameDetails = _boardGameGeekApiClient.GetGameDetails(existingBoardGameGeekGameDefinition.Id);

                if (gameDetails != null)
                {
                    existingBoardGameGeekGameDefinition.DateUpdated = DateTime.UtcNow;

                    existingBoardGameGeekGameDefinition.AverageWeight = gameDetails.AverageWeight;
                    existingBoardGameGeekGameDefinition.Description = gameDetails.Description;
                    existingBoardGameGeekGameDefinition.MaxPlayTime = gameDetails.MaxPlayTime;
                    existingBoardGameGeekGameDefinition.MinPlayTime = gameDetails.MinPlayTime;
                    existingBoardGameGeekGameDefinition.MaxPlayers = gameDetails.MaxPlayers;
                    existingBoardGameGeekGameDefinition.MinPlayers = gameDetails.MinPlayers;
                    existingBoardGameGeekGameDefinition.Name = gameDetails.Name;
                    existingBoardGameGeekGameDefinition.Thumbnail = gameDetails.Thumbnail;
                    existingBoardGameGeekGameDefinition.Image = gameDetails.Image;
                    existingBoardGameGeekGameDefinition.YearPublished = gameDetails.YearPublished;

                    foreach (var gameCategory in gameDetails.Categories)
                    {
                        if (
                            existingBoardGameGeekGameDefinition.Categories.All(
                                c => !c.CategoryName.Equals(gameCategory.Category, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            var existentCategory =
                                _dataContext.GetQueryable<BoardGameGeekGameCategory>()
                                    .FirstOrDefault(
                                        c =>
                                            c.CategoryName.Equals(gameCategory.Category,
                                                StringComparison.InvariantCultureIgnoreCase));
                            if (existentCategory == null)
                            {
                                existentCategory = new BoardGameGeekGameCategory()
                                {
                                    BoardGameGeekGameCategoryId = gameCategory.Id,
                                    CategoryName = gameCategory.Category
                                };
                            }

                            existingBoardGameGeekGameDefinition.Categories.Add(existentCategory);
                        }
                    }

                    foreach (var gameMechanic in gameDetails.Mechanics)
                    {
                        if (
                            existingBoardGameGeekGameDefinition.Mechanics.All(
                                c => !c.MechanicName.Equals(gameMechanic.Mechanic, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            var existentMechanic =
                                _dataContext.GetQueryable<BoardGameGeekGameMechanic>()
                                    .FirstOrDefault(
                                        c =>
                                            c.MechanicName.Equals(gameMechanic.Mechanic,
                                                StringComparison.InvariantCultureIgnoreCase));
                            if (existentMechanic == null)
                            {
                                existentMechanic = new BoardGameGeekGameMechanic()
                                {
                                    BoardGameGeekGameMechanicId = gameMechanic.Id,
                                    MechanicName = gameMechanic.Mechanic
                                };
                            }

                            existingBoardGameGeekGameDefinition.Mechanics.Add(existentMechanic);
                        }
                    }

                    _dataContext.Save(existingBoardGameGeekGameDefinition, anonymousUser);

                    if (totalGamesUpdated++ % 10 == 0)
                    {
                        _dataContext.CommitAllChanges();
                        Console.WriteLine("{0} BoardGameGeekGameDefinitions updated so far...", totalGamesUpdated);
                    }
                }
            }
            _dataContext.CommitAllChanges();

            return totalGamesUpdated;
        }