Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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();