public virtual ActionResult Add(int id, string returnUrl) { var product = _productRepository.GetById(id); if (product == null) { throw new HttpException((int)HttpStatusCode.NotFound, Resources.Error_NotFound_Product); } var cartItem = _cartItemRepository.GetById(id); if (cartItem != null) { cartItem.Count++; } else { cartItem = new CartItemModel { Id = product.Id, Title = product.Title, Count = 1, UnitPrice = product.UnitPrice }; } _cartItemRepository.AddOrUpdate(cartItem); _cartItemRepository.SaveChanges(); product.AddedToCartCount++; _productRepository.AddOrUpdate(product); _productRepository.SaveChanges(); return(RedirectedAsync(returnUrl, CartOperationModel.Succeeded(_cartItemRepository.GetItemsCount(), Resources.Success_AddedToCart))); }
public CartItem AddToCart(JsonElement request) { var productId = request.GetProperty("productId").GetString(); var categoryId = request.GetProperty("categoryId").GetString(); Product product; product = _giftRepository.GetGift(int.Parse(productId)); var user = GetCurrentUserAsync().Result; var cart = _cartRepository.GetAllCarts().LastOrDefault(c => c.CustomerId == user.Id && c.isOrdered == false) ?? new Cart(); cart.Customer = user; cart.CustomerId = user.Id; var cartItem = _cartItemRepository.GetAllCartItems() .FirstOrDefault(c => c.ProductId == product.Id && c.CartId == cart.Id) ?? new CartItem(); cartItem.Product = product; cartItem.Cart = cart; cartItem.Quantity++; cartItem.CartId = cart.Id; cartItem.ProductId = product.Id; _cartItemRepository.AddOrUpdate(cartItem); _cartRepository.AddOrUpdate(cart); return(cartItem); }
public ActionResult Edit([Bind(Include = "Id,Quantity,ProductId,CartId")] CartItem cartItem) { if (ModelState.IsValid) { repo.AddOrUpdate(cartItem); return(RedirectToAction("Index")); } ViewBag.ProductId = repo.GetSelectList(cartItem); return(View(cartItem)); }