Example #1
0
        public void Add_AddOne_UpdateIfExists()
        {
            // Arrange
            Product product1 = new Product
            {
                Id          = 1,
                ProductName = "Racing Car",
                CategoryId  = 1,
                Description = "Nice Racing Car",
                ImagePath   = "abc.png",
                UnitPrice   = 23.1
            };
            var      cartId   = Guid.NewGuid().ToString();
            CartItem cartItem = new CartItem
            {
                Id        = Guid.NewGuid().ToString(),
                CartId    = cartId,
                ProductId = product1.Id,
                Product   = product1,
                Created   = DateTime.Now,
                Quantity  = 1
            };

            var context = new WingtipContext(new DbContextOptionsBuilder <WingtipContext>().UseInMemoryDatabase("WingtipToysDB").Options);

            context.Products.Add(product1);
            context.CartItems.Add(cartItem);
            context.SaveChanges();

            ICartService service = new CartService(context, _mapper);

            CartItemDto cartDto = new CartItemDto
            {
                CartId    = cartId,
                ProductId = product1.Id,
                Quantity  = 3
            };

            // Act
            service.Add(cartDto);

            // Assert
            var tempList = context.CartItems.ToList();

            Assert.AreEqual(1, tempList.Count);
            Assert.AreEqual(cartItem.Id, tempList[0].Id);
            Assert.AreEqual(cartItem.CartId, tempList[0].CartId);
            Assert.AreEqual(cartItem.ProductId, tempList[0].ProductId);
            Assert.AreEqual(3, tempList[0].Quantity);

            // Clean data
            context.Products.RemoveRange(context.Products);
            context.CartItems.RemoveRange(context.CartItems);
            context.SaveChanges();
            context.Dispose();
        }
        public void Removing_unknown_article_is_not_error_and_article_is_not_in_the_basket()
        {
            var svc = new CartService();

            var content1 = svc.Add(CartItems.Empty, Helpers.Build(("A1", 5)));
            var content2 = svc.Remove(content1, Helpers.Build(("A2", 1)));

            Assert.AreEqual(5, content2.Items["A1"]);
            Assert.AreEqual(1, content2.Items.Count);
        }
Example #3
0
        public IActionResult Post([FromQuery] int id, int price, string guid = null)
        {
            var cartGuid = Guid.TryParse(guid, out Guid guidresult) ? guidresult : Guid.NewGuid();

            var result = cartService.Add(id, cartGuid, price);

            if (!result)
            {
                return(BadRequest());
            }
            return(Ok(cartGuid));
        }
 public IActionResult Add([FromBody] CartItem cartItem)
 {
     if (cartItem.CartId < 1)
     {
         var cart = cartService.Create(cartItem);
         return(Ok(cart));
     }
     else
     {
         var cart = cartService.Add(cartItem);
         return(Ok(cart));
     }
 }
        public IActionResult AddToCart([FromQuery] int productId, [FromQuery] string cartId)
        {
            var data = cartService.Add(productId, cartId);

            if (data == false)
            {
                return(BadRequest());
            }
            else
            {
                return(Ok());
            }
        }
Example #6
0
        public HttpResponseMessage AddProduct(AddProduct addProduct)
        {
            var temporaryReservation = _productStockService.MakeATemporaryReservation(addProduct);

            if (temporaryReservation != null)
            {
                if (!_cartService.Add(addProduct, temporaryReservation))
                {
                    return(Error("Sorry, cannot add to cart"));
                }
            }
            else
            {
                return(Error("Sorry, not enough stock"));
            }
            return(new HttpResponseMessage(HttpStatusCode.Created));
        }
Example #7
0
        public ActionResult Add(int id)
        {
            var user = GetSession <UserSessionInfo>(USER_SESSION);

            if (user == null)
            {
                return(JavaScript("$('#login-modal').modal();"));
            }
            var cart = new CartAddViewModel()
            {
                AdvertisementId = id,
                BuyerId         = user.Id
            };

            _cartService.Add(cart);
            return(Json(true, JsonRequestBehavior.AllowGet));
        }
Example #8
0
 public JsonResult Add(string goodsId, int?number)
 {
     if (null == number)
     {
         return(Json("error", JsonRequestBehavior.AllowGet));
     }
     else
     {
         Cart cart = new Cart();
         cart.number  = number.Value;
         cart.goodsId = goodsId;
         cart.userId  = User.Identity.GetUserId();
         if (!cartService.Add(cart))
         {
             return(Json("error", JsonRequestBehavior.AllowGet));
         }
         return(Json("ok", JsonRequestBehavior.AllowGet));
     }
 }
Example #9
0
        public async Task <IActionResult> OnPostAddToCartAsync()
        {
            if (ModelState.IsValid)
            {
                var productVM = await _productService.GetById(CartItem.ProductId); // For the case where user tried

                if (!productVM.Product.IsAvailable)                                // to modify the HTML and add
                {
                    return(RedirectToPage());                                      // unavailable item to cart
                }
                var currentUserId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
                CartItem.UserId = currentUserId;

                CartItem cartItemFromDb = await _cartService.GetCartItemByUserIdAndProductId(CartItem);

                bool addToCartSuccess = false;

                if (cartItemFromDb == null)
                {
                    addToCartSuccess = await _cartService.Add(CartItem);
                }
                else
                {
                    cartItemFromDb.Quantity += CartItem.Quantity;
                    addToCartSuccess         = await _cartService.Update(cartItemFromDb);
                }

                if (addToCartSuccess)
                {
                    var cartItemsCount = await _cartService.GetItemsCount(currentUserId);

                    HttpContext.Session.SetInt32(SD.CART_SESSION_KEY, cartItemsCount);
                    SuccessMessage = "Item added to cart successfully!";
                }
                else
                {
                    ErrorMessage = "Add to cart failed.";
                }

                return(LocalRedirect("/"));
            }
            return(RedirectToPage());
        }
Example #10
0
        // GET: Cart
        public async Task <ActionResult> ShowUserCart(int ID)
        {
            Customer user = await manager.FindByNameAsync(User.Identity.Name);

            AddToSession();

            if (ID != -1)
            {
                Cart cart = new Cart();


                cart.CustomerId = user.Id;
                cart.ProductId  = ID;
                cart.Quantity   = 1;

                cartService.Add(cart);
            }
            var carts = cartService.GetUserCart(user.Id);

            return(View(carts));
        }
        public ActionResult Add(CartAddView model)
        {
            if (Session["UserId"] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var cart     = Session["Cart"];
            var cartJson = "";

            if (cart != null)
            {
                cartJson = cart.ToString();
            }

            var cartStr = cartService.Add(model, cartJson);

            Session["Cart"] = cartStr;

            return(RedirectToAction("Index", "Home"));
        }
Example #12
0
        public IActionResult Add([FromBody] CartItem cartItem)
        {
            var result = this.cartService.Get(cartItem.CartId);

            if (result == null)
            {
                var cart = cartService.Create(cartItem);
                return(Ok(cart));
            }

            else
            {
                if (result.Products.Any(product => product.Id == cartItem.ProductId))
                {
                    var cart = cartService.Update(cartItem);
                    return(Ok(cart));
                }
                else
                {
                    var cart = cartService.Add(cartItem);
                    return(Ok(cart));
                }
            }
        }
        protected async Task AddToCart()
        {
            await CartService.Add(FoodOrderService.GetFoodOrder(Food, CustomIngredients));

            UriHelper.NavigateTo("/cart");
        }
Example #14
0
 /// <summary>
 /// 新增购物车
 /// </summary>
 /// <param name="cart"></param>
 /// <returns></returns>
 public int Add(Cart cart)
 {
     return(cs.Add(cart));
 }
Example #15
0
 public IActionResult AddItemToCart(int id)
 {
     cartService.Add(Request.Cookies["guid"], id);
     return(RedirectToAction("Index"));
 }
        public void AddItem_SingleSoap_ShouldReturnEmptyFalse()
        {
            // Arrage
            productRepository.Find(ProductCodes.DoveSoap).Returns(
                new ShoppingCart.Dtos.Product(ProductCodes.DoveSoap, 39.99m)
                );

            sut.Add(ProductCodes.DoveSoap);

            // Act
            var result = sut.IsEmpty();

            // Assert
            result.ShouldBeFalse();
        }
        public void CartServiceTest()
        {
            ICartService cartService = new CartService(_context, _mapper);
            string       cartId      = "f24c3e54-4d90-4e2e-ac08-f663c4ecc7a2";

            List <CartItemDto> cartList = cartService.Get(cartId);

            if (cartList.Count > 0)
            {
                foreach (CartItemDto dto in cartList)
                {
                    cartService.Delete(cartId, dto.Id);
                }
            }

            CartItemDto cartDto1 = new CartItemDto
            {
                CartId    = cartId,
                ProductId = 10,
                Quantity  = 3
            };

            cartService.Add(cartDto1);
            CartItemDto cartDto2 = new CartItemDto
            {
                CartId    = cartId,
                ProductId = 11,
                Quantity  = 6
            };

            cartService.Add(cartDto2);
            int chk = 0;

            cartList = cartService.Get(cartId);
            foreach (CartItemDto item in cartList)
            {
                Assert.IsNotNull(item.Id);
                if (item.ProductId == cartDto1.ProductId)
                {
                    cartDto1.Id = item.Id;
                    Assert.AreEqual(cartDto1.Quantity, item.Quantity);
                    chk++;
                }
                if (item.ProductId == cartDto2.ProductId)
                {
                    cartDto2.Id = item.Id;
                    Assert.AreEqual(cartDto2.Quantity, item.Quantity);
                    chk++;
                }
            }
            Assert.IsTrue(chk == 2);
            cartDto1.Quantity = 1;
            cartService.Patch(cartId, cartDto1.Id, cartDto1.Quantity);

            cartDto2.Quantity = 4;
            cartService.Patch(cartId, cartDto2.Id, cartDto2.Quantity);

            chk      = 0;
            cartList = cartService.Get(cartId);
            CollectionAssert.Contains(cartList, cartDto1);
            CollectionAssert.Contains(cartList, cartDto2);

            /*foreach (CartItemDto item in cartList)
             * {
             *  if (item.ProductId == cartDto1.ProductId)
             *  {
             *      Assert.AreEqual(cartDto1.Quantity, item.Quantity);
             *      chk++;
             *  }
             *  if (item.ProductId == cartDto2.ProductId)
             *  {
             *      Assert.AreEqual(cartDto2.Quantity, item.Quantity);
             *      chk++;
             *  }
             * }
             * Assert.IsTrue(chk == 2);*/

            cartService.Delete(cartId, cartDto1.Id);
            cartService.Delete(cartId, cartDto2.Id);

            cartList = cartService.Get(cartId);
            Assert.IsTrue(cartList.Count == 0);

            CartItemDto cartDto3 = new CartItemDto
            {
                ProductId = 10,
                Quantity  = 3
            };

            cartId = cartService.Add(cartDto3);
            Assert.IsNotNull(cartId);
            cartList = cartService.Get(cartId);
            Assert.IsTrue(cartList.Count == 1);
            cartService.Delete(cartId);
            cartList = cartService.Get(cartId);
            Assert.IsTrue(cartList.Count == 0);
        }