Beispiel #1
0
        public async Task <IActionResult> AddCartItem(int?quantity, string productName, string locationName)
        {
            // Get the user who's azdding the item to the cart
            var           userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            VendorAppUser user   = await userManager.GetUserAsync(User);

            LocationInventory targetLocationInventory = await cartRepo.GetLocationInventoryByProductAndLocationName(locationName, productName);

            // Redirect user back to product page if any of the paramerters aren't valid
            if (quantity == null || productName == null || locationName == null || targetLocationInventory == null)
            {
                TempData["ErrorMessage"] = "Please enter valid input";
                return(Redirect(Request.Headers["Referer"]));
            }

            if (quantity > targetLocationInventory.Quanitity || quantity <= 0)
            {
                TempData["ErrorMessage"] = "Please enter a valid quantity";
                return(Redirect(Request.Headers["Referer"]));
            }

            // Add item to cart
            await cartRepo.AddItemToCart(userId, productName, locationName, quantity ?? -1);

            // Document changes
            TempData["FlashMessage"] = "Added to cart";
            cartLogger.LogInformation($"{productName} was added to {user.UserName}'s cart");

            // Take client back to list of products

            return(RedirectToAction("Index", "Product"));
        }
        public async void ItShouldUpdateUsersNumCartItems()
        {
            //Given
            EFCoreCartRepository cartRepo = new EFCoreCartRepository(ctx);
            EFCoreUserRepository userRepo = new EFCoreUserRepository(ctx);
            VendorAppUser        newUser  = new VendorAppUser();

            //When
            await userRepo.Add(newUser);


            newUser = (await userRepo.GetAll()).ElementAt(0);



            await cartRepo.AddItemToCart(newUser.Id, "product", "location", 1);

            //Then
            VendorAppUser actualUser = (await userRepo.GetAll()).ElementAt(0);

            Assert.Equal(1, actualUser.NumCartItems);
        }
        public async void ItShouldAddCartItemsForUser()
        {
            // * Arrange
            EFCoreCartRepository cartRepo = new EFCoreCartRepository(ctx);

            // Make a mock user who will own the cart
            string        userId   = "user-id";
            VendorAppUser tempUser = new VendorAppUser
            {
                Id = userId
            };
            // Mock<EFCoreUserRepository> mockUser = new Mock<EFCoreUserRepository>();
            // mockUser.Setup(u => u.Get(userId)).ReturnsAsync(tempUser);
            // Make a cart
            Cart newCart = new Cart
            {
                User      = tempUser,
                CartItems = new List <CartItem>()
            };

            // * Act
            // Add cart to DbContext
            await cartRepo.Add(newCart);

            // Make some cart items

            CartItem c1 = new CartItem
            {
                Cart            = newCart,
                ProductName     = "Product1",
                AmountPurchased = 2
            };

            CartItem c2 = new CartItem
            {
                Cart            = newCart,
                ProductName     = "Product2",
                AmountPurchased = 2
            };
            CartItem c3 = new CartItem
            {
                Cart            = newCart,
                ProductName     = "Product3",
                AmountPurchased = 4
            };

            // Add items to cart
            await cartRepo.AddItemToCart(userId, "Product0", "Location0", 2);

            await cartRepo.AddItemToCart(userId, "Product1", "Location1", 10);

            await cartRepo.AddItemToCart(userId, "Product2", "Location2", 50);

            // * Assert
            // Retrive Cart back from context
            Cart actualCart = await cartRepo.FindCartByUserId(tempUser.Id);

            // Assert all values

            Assert.Equal(userId, actualCart.User.Id);
            // Check all cart items
            List <CartItem> actualCartItems = (List <CartItem>)actualCart.CartItems;

            Assert.Equal(3, actualCartItems.Count);

            Assert.Equal("Product0", actualCartItems[0].ProductName);
            Assert.Equal(2, actualCartItems[0].AmountPurchased);
            Assert.Equal("Product2", actualCartItems[2].ProductName);
            Assert.Equal(50, actualCartItems[2].AmountPurchased);
        }