public async Task <BasketItemDto> UpdateItemQuantity(UpdateItemQuantityCommand command)
        {
            try
            {
                var basketItem = await _basketItemRepository.Table.FirstOrDefaultAsync(x => x.BasketId == command.BasketId && x.ItemId == command.ItemId);

                if (basketItem == null)
                {
                    throw new BasketException(ErrorCode.ItemNotFound, string.Format(ErrorMessageConstants.ItemNotFound, command.ItemId));
                }

                var product = await _productRepository.Table.FirstOrDefaultAsync(x => x.Id == command.ItemId);

                var updatedProductStock = UpdateProductStock(product, basketItem.Quantity, command.Quantity);

                if (updatedProductStock.Quantity <= 0)
                {
                    throw new BasketException(ErrorCode.OutOfStock, string.Format(ErrorMessageConstants.OutOfStock, updatedProductStock.Quantity));
                }

                basketItem.Handle(command);

                _basketItemRepository.Update(basketItem);
                _productRepository.Update(updatedProductStock);
                await _unitOfWork.CommitAsync();

                return(await GetBasketItems(new GetBasketItemQuery(command.BasketId)));
            }
            catch (Exception e)
            {
                throw new BasketException(ErrorCode.InvalidOperation, ErrorMessageConstants.CanNotUpdateItem, e);
            }
        }
        private void UpdateItemQuantityTo(int quantity)
        {
            UpdateItemQuantityCommand command = new UpdateItemQuantityCommand(_basketRepository);

            command.Do(_basketId,
                       _itemReference,
                       quantity);
        }
Exemple #3
0
 private async Task UpdateItemQuantity(Guid cartId, Guid productId, int quantity)
 {
     var command = new UpdateItemQuantityCommand
     {
         CartId    = cartId,
         ProductId = productId,
         Quantity  = quantity
     };
     await _mediator.Send(command);
 }
 public BasketController(CreateBasketCommand createBasketCommand,
                         BasketQuery basketQuery,
                         DeleteBasketCommand deleteBasketCommand,
                         AddItemToBasketCommand addItemToBasketCommand,
                         UpdateItemQuantityCommand updateItemQuantityCommand)
 {
     _createBasketCommand       = createBasketCommand;
     _basketQuery               = basketQuery;
     _deleteBasketCommand       = deleteBasketCommand;
     _addItemToBasketCommand    = addItemToBasketCommand;
     _updateItemQuantityCommand = updateItemQuantityCommand;
 }
 public void Handle(UpdateItemQuantityCommand command)
 {
     UpdateAtUtc = DateTime.UtcNow;
     Quantity    = command.Quantity;
 }
Exemple #6
0
        public async Task <IActionResult> UpdateBasketItem(UpdateItemQuantityCommand command)
        {
            var result = await _basketService.UpdateItemQuantity(command);

            return(Ok(result));
        }