public async Task <IActionResult> AddToCart(AddToCartInputModel inputModel)
        {
            var currentUserId = this.userService.GetUserId(this.User.Identity.Name);
            var productId     = inputModel.ProductId;
            var quantity      = inputModel.Quantity;
            var product       = this.productService.GetProductById(productId, false);

            if (product.QuantityInStock < quantity)
            {
                this.ModelState.AddModelError("QuantityInStock", "Cannot buy more of this product than there is in stock");
            }

            if (this.ModelState.IsValid == false)
            {
                //Store needed info for get request in TempData only if the model state is invalid after doing the complex checks
                TempData[GlobalConstants.ErrorsFromPOSTRequest] = ModelStateHelper.SerialiseModelState(this.ModelState);

                //Store needed info for get request in TempData
                return(this.RedirectToAction("ProductPage", "Products", new { productId = productId }, "Reviews"));
            }

            await this.cartService.AddToCartAsync(productId, currentUserId, quantity);

            return(this.RedirectToAction(nameof(this.All)));
        }
示例#2
0
        public async Task <IActionResult> AddToCart(AddToCartInputModel input)
        {
            var user = await this.userManager.GetUserAsync(this.User);

            var productId = input.ProductId;
            var quantity  = input.Quantity;

            await this.cartsService.AddToCartAsync(productId, user.Id, quantity);

            return(this.RedirectToAction(nameof(this.All)));
        }