public JsonResult AddProduct(int postId, int quantity, decimal price, FormCollection form)
        {
            var post = _postService.GetById(postId);

            var ctx = new AddToCartContext
            {
                Post     = post,
                Quantity = quantity,
                Price    = price
            };

            //Create cart
            _shoppingCartItemService.AddToCart(ctx);

            var model = PrepareMiniShoppingCartModel();

            var jsonResult =
                Json(new { success = true, list = this.RenderRazorViewToString("_Order.TopCart", model) },
                     JsonRequestBehavior.AllowGet);

            return(jsonResult);
        }
示例#2
0
        //sepete ürün ekleme
        public IActionResult Post([FromBody] ShoppingCartItemModel model)
        {
            ServiceResponse <ShoppingCartItem> response = new ServiceResponse <ShoppingCartItem>();

            var selectedProduct = _productService.GetById(model.ProductId);

            if (selectedProduct == null)
            {
                response.HasError = true;
                response.Errors.Add("Selected Product Does Not Exist!");

                return(BadRequest(response));
            }
            else if (selectedProduct.Deleted || !selectedProduct.Published)
            {
                response.HasError = true;
                response.Errors.Add("Selected Product Does Not Publish!");

                return(BadRequest(response));
            }
            else if (selectedProduct.StockQuantity <= 0 || selectedProduct.StockQuantity <= selectedProduct.MinStockQuantity)
            {
                response.HasError = true;
                response.Errors.Add("Selected Product Has No Stock!");

                return(BadRequest(response));
            }
            else if (selectedProduct.StockQuantity - model.Quantity <= selectedProduct.MinStockQuantity)
            {
                response.HasError = true;
                response.Errors.Add("Stock Of Selected Product Is Not Enough!");

                return(BadRequest(response));
            }

            var shoppingCartItem = _shoppingCartItemService.GetEx(s => s.CustomerUserName == model.CustomerUsername && s.ProductId == model.ProductId).FirstOrDefault();

            if (shoppingCartItem == null)
            {
                ShoppingCartItem newShoppingCartItem = new ShoppingCartItem
                {
                    CustomerUserName = model.CustomerUsername,
                    ProductId        = model.ProductId,
                    Quantity         = model.Quantity
                };

                _shoppingCartItemService.AddToCart(model.CustomerUsername, newShoppingCartItem);

                response.Entity = _shoppingCartItemService.GetById(newShoppingCartItem.Id);
            }
            else
            {
                shoppingCartItem.Quantity += model.Quantity;
                _shoppingCartItemService.AddToCart(model.CustomerUsername, shoppingCartItem);

                response.Entity = _shoppingCartItemService.GetById(shoppingCartItem.Id);
            }

            response.IsSuccess = true;

            return(Ok(response));
        }