コード例 #1
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());
            }
        }