Exemple #1
0
        public IActionResult Details(int id)
        {
            var product   = _repository.GetProductById(id);
            var size      = _repository.GetSizesById(id);
            var viewModel = new ProductDetailsViewModel
            {
                Product = product,
                Sizes   = size
            };

            return(View(viewModel));
        }
        public IActionResult AddToCart(ProductDetailsViewModel viewModel)
        {
            var    item   = _repository.GetProductById(viewModel.ProductId);
            var    user   = _userManager.GetUserAsync(User).Result;
            string userId = user.Id;

            var cart = _repository.GetCartByUserId(userId);

            if (ModelState.IsValid)
            {
                if (cart == null)
                {
                    var cartModel = new Cart();
                    cartModel.User     = user;
                    cartModel.CartItem = new List <CartItem>()
                    {
                        new CartItem()
                        {
                            Product  = item,
                            Quantity = viewModel.Quantity,
                            Size     = viewModel.Size,
                            CartId   = cartModel.Id
                        }
                    };
                    cartModel.TotalPrice += item.Price * viewModel.Quantity;


                    _repository.AddEntity(cartModel);


                    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 korpu. ");
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        throw new Exception("Nije uspelo");
                    }
                }
                else
                {
                    item = _repository.GetProductById(viewModel.ProductId);
                    viewModel.Product = item;

                    if (_repository.IsThereSameProductInTheCart(cart.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 korpu. ");
                        return(View("../Home/Details", viewModel));
                    }
                    else
                    {
                        cart.CartItem.Append(
                            new CartItem()
                        {
                            Product  = item,
                            Quantity = viewModel.Quantity,
                            Size     = viewModel.Size,
                            CartId   = cart.Id
                        }
                            );



                        cart.TotalPrice += item.Price * viewModel.Quantity;



                        _repository.AddEntity(new CartItem()
                        {
                            Product  = item,
                            Quantity = viewModel.Quantity,
                            Size     = viewModel.Size,
                            CartId   = cart.Id
                        });

                        _repository.UpdateCartWithNewCartItem(cart);

                        if (_repository.SaveAll())
                        {
                            //return Json(new { Success = true });
                            FlashMessage.Info("Uspešno ste dodali artikal u korpu. ");
                            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.Sizes   = _repository.GetSizesById(item.Id);
                viewModel.Product = item;
                return(View("../Home/Details", viewModel));
            }
        }