Example #1
0
        public virtual async Task <bool> TryReduceInventoryAsync(ProductInventory productInventory, int quantity, bool increaseSold)
        {
            if (quantity < 0)
            {
                return(false);
            }

            var originalInventory = productInventory.Inventory;

            if (!productInventory.TryReduceInventory(quantity, increaseSold))
            {
                return(false);
            }

            using var uow = _unitOfWorkManager.Begin(isTransactional: true);

            await _productInventoryRepository.UpdateAsync(productInventory, true);

            PublishInventoryChangedEventOnUowCompleted(uow, productInventory.ProductId, productInventory.ProductSkuId,
                                                       originalInventory, productInventory.Inventory, productInventory.Sold);

            await uow.CompleteAsync();

            return(true);
        }
        public virtual async Task <bool> TryReduceInventoryAsync(ProductInventory productInventory, int quantity, bool increaseSold)
        {
            if (quantity < 0)
            {
                return(false);
            }

            var originalInventory = productInventory.Inventory;

            if (!productInventory.TryReduceInventory(quantity, increaseSold))
            {
                return(false);
            }

            await _productInventoryRepository.UpdateAsync(productInventory, true);

            PublishInventoryChangedEventOnUowCompleted(productInventory.ProductId, productInventory.ProductSkuId,
                                                       originalInventory, productInventory.Inventory, productInventory.Sold);

            return(true);
        }
Example #3
0
        protected virtual async Task <bool> TryReduceInventoryAsync(InventoryQueryModel model,
                                                                    ProductInventory productInventory, int quantity, bool increaseSold)
        {
            if (quantity < 0)
            {
                return(false);
            }

            var originalInventory = productInventory.Inventory;

            if (!productInventory.TryReduceInventory(quantity, increaseSold))
            {
                return(false);
            }

            await _productInventoryRepository.UpdateAsync(productInventory, true);

            await PublishInventoryChangedEventAsync(model.TenantId, model.StoreId,
                                                    productInventory.ProductId, productInventory.ProductSkuId, originalInventory,
                                                    productInventory.Inventory, productInventory.Sold);

            return(true);
        }