Ejemplo n.º 1
0
        private UserComputerGamesViewModel BuildViewModel(IQueryable <UserComputerGame> userComputerGames, string searchString, string gameGenre, int?selectedYear, decimal?fromPrice, decimal?toPrice)
        {
            List <string> distinctGenres = userComputerGames
                                           .OrderBy(game => game.Genre)
                                           .Select(game => game.Genre)
                                           .Distinct()
                                           .ToList();

            List <int> distinctYears = userComputerGames
                                       .OrderBy(game => game.YearPublished)
                                       .Select(game => game.YearPublished)
                                       .Distinct()
                                       .ToList();

            if (fromPrice > toPrice)
            {
                ModelState.AddModelError("", "From price cannot be greater than To price");
            }
            else if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(searchString))
                {
                    userComputerGames = userComputerGames.Where(game => game.Title.Contains(searchString));
                }

                if (!string.IsNullOrEmpty(gameGenre))
                {
                    userComputerGames = userComputerGames.Where(game => game.Genre == gameGenre);
                }

                if (selectedYear != null)
                {
                    userComputerGames = userComputerGames.Where(game => game.YearPublished == selectedYear);
                }

                if (fromPrice != null && toPrice != null)
                {
                    userComputerGames = userComputerGames.Where(game => game.Price >= fromPrice)
                                        .Where(game => game.Price <= toPrice);
                }
            }

            UserComputerGamesViewModel viewModel = new UserComputerGamesViewModel
            {
                Genres = new SelectList(distinctGenres),
                Years  = new SelectList(distinctYears),
                Games  = userComputerGames.ToList()
            };

            return(viewModel);
        }
Ejemplo n.º 2
0
        // GET: UserComputerGames
        public ActionResult Index(string searchString,
                                  string gameGenre,
                                  int?selectedYear,
                                  decimal?fromPrice,
                                  decimal?toPrice)
        {
            // Upon login set the userId for use throughout
            int currentUserId = (int)Session["CurrentUserId"];

            // Ensure we only get games for the logged in user
            var userComputerGames = db.UserComputerGames
                                    .Where(game => game.User.ID == currentUserId);

            UserComputerGamesViewModel viewModel = BuildViewModel(userComputerGames,
                                                                  searchString,
                                                                  gameGenre,
                                                                  selectedYear,
                                                                  fromPrice,
                                                                  toPrice);

            return(View("Index", viewModel));
        }