Пример #1
0
        public async Task Add_ReturnsRedirectResult_WhenSuccessAndUrlNotNullOrEmpty(bool?isGift)
        {
            // Arrange
            User recipientUser = user3;

            GetUserAsyncReturns = identityUser;

            int expectedRecipientUserId = isGift.HasValue && isGift.Value ? recipientUser.Id : identityUser.Id;
            int?recipientUserId         = isGift.HasValue ? expectedRecipientUserId : default(int?);

            // Act
            var result = await ControllerSUT.Add(game1.GameId, recipientUserId, "url");

            // Assert
            var redirectResult = Assert.IsAssignableFrom <RedirectResult>(result);

            Assert.Equal("url", redirectResult.Url);
            Assert.IsAssignableFrom <string>(ControllerSUT.TempData["CartAdded"]);

            Assert.Single(await _context.CartGames.ToListAsync());

            CartGame cartItem = await _context.CartGames.FirstOrDefaultAsync();

            Assert.Equal(game1.GameId, cartItem.GameId);
            Assert.Equal(identityUser.Id, cartItem.CartUserId);
            Assert.Equal(expectedRecipientUserId, cartItem.ReceivingUserId);
        }
Пример #2
0
        public async Task <IActionResult> Gift(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            CartGame cartItem = await _context.CartGames
                                .Include(c => c.Game)
                                .FirstOrDefaultAsync(c => c.CartGameId == id);

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

            User user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(Problem());
            }

            var relationships = await _context.UserRelationships
                                .Include(r => r.RelatedUser)
                                .Where(r => r.RelatingUserId == user.Id && r.Type == Relationship.Friend &&
                                       !r.RelatedUser.ReceivingCartItems.Any(c => c.GameId == cartItem.GameId && c.CartUserId == r.RelatingUserId))
                                .Select(r => new
            {
                r.RelatedUserId,
                r.RelatedUser.UserName
            })
                                .ToListAsync();

            // user has no friends - redirect to Friends list with message popup
            if (!relationships.Any())
            {
                TempData["FriendsMessage"] = ADD_FRIENDS;
                return(RedirectToAction(nameof(FriendsController.Index), "Friends"));
            }

            ViewData["RelatedUserId"] = new SelectList(relationships, "RelatedUserId", "UserName");

            return(View(cartItem));
        }
Пример #3
0
        public async Task Add_ReturnsRedirectToActionResult_WhenSuccessAndUrlNullOrEmpty()
        {
            // Arrange
            GetUserAsyncReturns = identityUser;

            // Act
            var result = await ControllerSUT.Add(game1.GameId, identityUser.Id, null);

            // Assert
            var redirectResult = Assert.IsAssignableFrom <RedirectToActionResult>(result);

            Assert.Equal(nameof(CartController.Index), redirectResult.ActionName);
            Assert.IsAssignableFrom <string>(ControllerSUT.TempData["CartAdded"]);

            Assert.Single(await _context.CartGames.ToListAsync());

            CartGame cartItem = await _context.CartGames.FirstOrDefaultAsync();

            Assert.Equal(game1.GameId, cartItem.GameId);
            Assert.Equal(identityUser.Id, cartItem.CartUserId);
            Assert.Equal(identityUser.Id, cartItem.ReceivingUserId);
        }
        // POST: Cart/AddToCart/5
        public async Task <IActionResult> AddToCart(int gameId)
        {
            // Get the current user, checking that they are logged in
            var curUser = _context.Account.FirstOrDefault(a => a.AspuserId == User.Identity.Name);

            if (curUser == null)
            {
                // Display NotLoggedIn page
                return(View("NotLoggedIn"));
            }

            // Get the gameStoreContext
            var gameStoreContext = _context.Cart.Include(c => c.CartGame);
            // Get any carts matching the current user's accountId
            var userCart = gameStoreContext.Where(a => a.AccountId == curUser.AccountId);
            // Create a new Cart object
            Cart newCart = new Cart();

            // If there is no cart...
            if (userCart.Count() == 0)
            {
                // Create a new cart with current user's accountId
                newCart.AccountId = curUser.AccountId;
                _context.Cart.Add(newCart);
            }
            // If there is more than 1 cart for an account...
            else if (userCart.Count() > 1)
            {
                // Delete each cart
                foreach (var cart in userCart)
                {
                    await Delete(cart.CartId);
                }
                // Create a new cart with current user's accountId
                newCart.AccountId = curUser.AccountId;
                _context.Cart.Add(newCart);
            }
            else
            {
                // Create a new cart object using the found cart
                // (This is used for consistency with the other If options)
                newCart = userCart.FirstOrDefault();
            }

            await _context.SaveChangesAsync();

            // Create a CartGame object with the gameId and the newCart cartId
            CartGame newCartGame = new CartGame();

            newCartGame.CartId = newCart.CartId;
            newCartGame.GameId = gameId;

            // Add the cartGame object to the context
            _context.CartGame.Add(newCartGame);

            // Save changes and go to the view
            await _context.SaveChangesAsync();

            //return View(await gameStoreContext.ToListAsync());
            return(RedirectToAction("Index"));
        }
Пример #5
0
        public async Task <IActionResult> Gift(int id, [Bind("CartGameId,CartUserId,ReceivingUserId,GameId,AddedOn")] CartGame cartItem)
        {
            if (id != cartItem.CartGameId)
            {
                return(NotFound());
            }

            User user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(Problem());
            }

            if (cartItem.CartUserId != user.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    User recipientUser = await _context.Users.FirstOrDefaultAsync(u => u.Id == cartItem.ReceivingUserId);

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

                    bool duplicateIndexExists = await _context.CartGames
                                                .AnyAsync(c => c.CartGameId != cartItem.CartGameId && c.GameId == cartItem.GameId && c.CartUserId == user.Id && c.ReceivingUserId == recipientUser.Id);

                    if (duplicateIndexExists)
                    {
                        TempData["CartError"] = $"Gift for friend already exists in your cart.";
                        return(RedirectToAction(nameof(Index)));
                    }

                    _context.CartGames.Update(cartItem);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!await _context.CartGames.AnyAsync(c => c.CartGameId == cartItem.CartGameId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            var relationships = await _context.UserRelationships
                                .Include(r => r.RelatedUser)
                                .Where(r => r.RelatingUserId == user.Id && r.Type == Relationship.Friend &&
                                       !r.RelatedUser.ReceivingCartItems.Any(c => c.GameId == cartItem.GameId && c.CartUserId == r.RelatingUserId))
                                .Select(r => new
            {
                r.RelatedUserId,
                r.RelatedUser.UserName
            })
                                .ToListAsync();

            // user has no friends - redirect to Friends list with message popup
            if (!relationships.Any())
            {
                TempData["FriendsMessage"] = ADD_FRIENDS;
                return(RedirectToAction(nameof(FriendsController.Index), "Friends"));
            }

            ViewData["RelatedUserId"] = new SelectList(relationships, "RelatedUserId", "UserName");

            return(View(cartItem));
        }