示例#1
0
        public async Task <IActionResult> OnGetAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(RedirectToPage("/Account/Login"));
            }

            if (!await _context.HasCartAsync(user))
            {
                IsCartEmpty = true;
                return(Page());
            }

            var cart = await _context.GetCartAsync(user);

            if (await _context.GetCartItemCount(cart) == 0)
            {
                IsCartEmpty = true;
                return(Page());
            }

            var cartItems = _context.CartGames
                            .Include(cg => cg.Game)
                            .ThenInclude(g => g.Category)
                            .Where(cg => cg.CartId == cart.Id);

            Games = new List <Game>();
            foreach (var cartItem in cartItems)
            {
                var game = cartItem.Game;
                if (Games.Contains(game))
                {
                    continue;
                }

                Total += game.Price;
                Games.Add(game);
            }

            return(Page());
        }
示例#2
0
        public async Task <IActionResult> OnPostCartAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(RedirectToPage("/Account/Login"));
            }

            Game = await _context.GetGameAsync(Id);

            if (Game == null)
            {
                return(NotFound());
            }

            if (await _context.DoesUserOwnGameAsync(user, Game))
            {
                StatusMessage = $"Error: You already own this game.";
                return(RedirectToPage());
            }

            var cart = await _context.GetCartAsync(user);

            if (cart == null)
            {
                StatusMessage = $"Error: An error occurred while getting your cart.";
                return(RedirectToPage());
            }

            if (await _context.DoesGameExistInCartAsync(cart, Game))
            {
                StatusMessage = $"Error: This game is already in your cart.";
                return(RedirectToPage());
            }

            if (await _context.AddToCartAsync(cart, Game))
            {
                StatusMessage = $"'{Game.Title}' has been added to your cart.";
                return(RedirectToPage());
            }
            else
            {
                StatusMessage = $"Error: We were unable to add that game to your cart.";
                return(RedirectToPage());
            }
        }
        public async Task <IActionResult> OnGetAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(RedirectToPage("/Account/Login"));
            }

            if (!await _context.HasCartAsync(user))
            {
                return(RedirectToPage("./Index"));
            }

            var cart = await _context.GetCartAsync(user);

            CartCount = await _context.GetCartItemCount(cart);

            if (CartCount == 0)
            {
                return(RedirectToPage("./Index"));
            }

            var cartItems = _context.CartGames
                            .Include(cg => cg.Game)
                            .ThenInclude(g => g.Category)
                            .Where(cg => cg.CartId == cart.Id);

            Games = new List <Game>();
            foreach (var cartItem in cartItems)
            {
                var game = cartItem.Game;
                if (Games.Contains(game))
                {
                    continue;
                }

                Total += game.Price;
                Games.Add(game);
            }

            if (Total == 0)
            {
                return(await OnPostFreeAsync());
            }

            Countries = new List <SelectListItem>();
            Countries.AddRange(AddressHelper.GetCountries().Select(keyValue => new SelectListItem()
            {
                Value = keyValue.Key,
                Text  = keyValue.Value
            }).OrderBy(x => x.Text));

            Provinces = new List <SelectListItem>();
            Provinces.AddRange(AddressHelper.GetProvinces().Select(keyValue => new SelectListItem()
            {
                Value = keyValue.Key,
                Text  = keyValue.Value
            }).OrderBy(x => x.Text));

            Addresses = await _context.UserAddresses
                        .Include(x => x.User)
                        .Include(x => x.Address)
                        .Where(x => x.UserId == user.Id).ToListAsync();

            Payments = await _context.Payments
                       .Where(x => x.Id == user.PaymentId)
                       .ToListAsync();

            return(Page());
        }