Пример #1
0
        public async Task <Result> UpdateProductAsync(
            ShoppingCartRequestModel model, string userId)
        {
            var productId       = model.ProductId;
            var requestQuantity = model.Quantity;

            var productQuantity = await this.GetProductQuantityById(productId);

            if (productQuantity < requestQuantity)
            {
                return(NotEnoughProductsMessage);
            }

            var shoppingCartProduct = await this.FindByProductAndUserAsync(
                productId,
                userId);

            if (shoppingCartProduct == null)
            {
                return(InvalidErrorMessage);
            }

            shoppingCartProduct.Quantity = requestQuantity;

            await this.Data.SaveChangesAsync();

            return(Result.Success);
        }
Пример #2
0
        public async Task <Result> AddProductAsync(
            ShoppingCartRequestModel model, string userId)
        {
            var productId       = model.ProductId;
            var requestQuantity = model.Quantity;

            var productQuantity = await this.GetProductQuantityById(productId);

            if (productQuantity < requestQuantity)
            {
                return(NotEnoughProductsMessage);
            }

            var shoppingCart = await this
                               .All()
                               .FirstOrDefaultAsync(c => c.UserId == userId);

            shoppingCart ??= new ShoppingCart
            {
                UserId = userId
            };

            var shoppingCartProduct = new ShoppingCartProduct
            {
                ShoppingCart = shoppingCart,
                ProductId    = productId,
                Quantity     = requestQuantity
            };

            await this.Data.AddAsync(shoppingCartProduct);

            await this.Data.SaveChangesAsync();

            return(Result.Success);
        }
Пример #3
0
        private async Task OnSubmitAsync(int id)
        {
            var cartRequest = new ShoppingCartRequestModel
            {
                ProductId = id,
                Quantity  = 1
            };

            await this.ShoppingCartsService.AddProduct(cartRequest);

            this.NavigationManager.NavigateTo("/cart", forceLoad: true);
        }
Пример #4
0
        private async Task AddToCart(int id)
        {
            var cartRequest = new ShoppingCartRequestModel
            {
                ProductId = id,
                Quantity  = 1
            };

            var result = await this.ShoppingCartsService.AddProduct(cartRequest);

            if (!result.Succeeded)
            {
                this.ToastService.ShowError(result.Errors.First());
            }
            else
            {
                this.NavigationManager.NavigateTo("/cart", forceLoad: true);
            }
        }
 public async Task <ActionResult> UpdateProduct(
     ShoppingCartRequestModel model)
 => await this.shoppingCarts
 .UpdateProductAsync(model, this.currentUser.UserId)
 .ToActionResult();