public async Task <JsonResult> UpdateCartItemAsync(string cartItemId, int quantity, string size, int quantityBeforeChange) { var user = await _userManager.GetUserAsync(User); var cart = _repository.GetCartByUserId(user.Id); var cartItem = _repository.GetCartItemByCartId(cart.Id); var cItemId = int.Parse(cartItemId); foreach (var item in cartItem) { if (item.Id == int.Parse(cartItemId)) { var cartItemQuantity = cartItem.SingleOrDefault(id => id.Id == cItemId).Quantity = quantity; cartItem.SingleOrDefault(id => id.Id == cItemId).Size = size; if (cartItemQuantity == quantityBeforeChange) { continue; } else { if (cartItemQuantity > quantityBeforeChange) { cart.TotalPrice += item.Product.Price; } else if (cartItemQuantity < quantityBeforeChange) { cart.TotalPrice -= item.Product.Price; } } } } cart.CartItem = cartItem; _repository.UpdateEntity(cart); if (_repository.SaveAll()) { var result = new { Success = "true", message = cart.Id }; return(Json(result)); } else { var result = new { Success = "true", message = cart.Id }; return(Json(result)); } }
public IActionResult AddToWishList(Product viewModel) { if (User.Identity.IsAuthenticated) { var item = _repository.GetProductById(viewModel.Id); var user = _userManager.GetUserAsync(User).Result; string userId = user.Id; var wishList = _repository.GetWishListByUserId(userId); if (ModelState.IsValid) { if (wishList == null) { var wishListModel = new WishList(); wishListModel.User = user; wishListModel.WishListItems = new List <WishListItem>() { new WishListItem() { Product = item, WishListId = wishListModel.Id } }; _repository.AddEntity(wishListModel); if (_repository.SaveAll()) { //var result = new { Success = "true", message = "Uspešno ste dodali artikal u korpu. " }; //return Json(result); FlashMessage.Info("Uspešno ste dodali artikal u listu želja. "); return(RedirectToAction("Index", "Home")); } else { throw new Exception("Nije uspelo"); } } else { item = _repository.GetProductById(viewModel.Id); viewModel = item; if (_repository.IsThereSameProductInTheWishList(wishList.Id, item.Id) != false) { //var result = new { Success = "true", message = "Već ste dodali ovaj artikal u korpu. " }; //return Json(result); ModelState.AddModelError("Product", "Već ste dodali ovaj artikal u listu želja. "); return(View("../Home/Details", viewModel)); } else { wishList.WishListItems.Append( new WishListItem() { Product = item, WishListId = wishList.Id }); _repository.AddEntity(new WishListItem() { Product = item, WishListId = wishList.Id }); _repository.UpdateEntity(wishList); if (_repository.SaveAll()) { //return Json(new { Success = true }); FlashMessage.Info("Uspešno ste dodali artikal u listu želja. "); return(RedirectToAction("Index", "Home")); } else { //var result = new { Success = "true", message = "Uspešno ste dodali artikal u korpu. " }; //return Json(result); return(RedirectToAction("Index", "Home")); } } } } else { viewModel = item; return(View("../Home/Details", viewModel)); } } else { return(RedirectToAction("Login", "Account")); } }