示例#1
0
        public virtual async Task <bool> TryReduceInventoryAsync(Product product, ProductSku productSku, int quantity,
                                                                 bool increaseSold)
        {
            var model = new InventoryQueryModel(product.TenantId, product.StoreId, product.Id, productSku.Id);

            return(await(await _productInventoryProviderResolver.GetAsync(product))
                   .TryReduceInventoryAsync(model, quantity, increaseSold));
        }
示例#2
0
    public virtual async Task <bool> TryRollBackInventoryAsync(Guid?tenantId, string providerName, Guid storeId, Guid productId,
                                                               Guid productSkuId, int quantity, bool decreaseSold)
    {
        var model = new InventoryQueryModel(tenantId, storeId, productId, productSkuId);

        return(await(await ProductInventoryProviderResolver.GetAsync(providerName))
               .TryIncreaseInventoryAsync(model, quantity, decreaseSold));
    }
示例#3
0
        public virtual async Task <bool> IsInventorySufficientAsync(Product product, ProductSku productSku, int quantity)
        {
            var model = new InventoryQueryModel(product.TenantId, product.StoreId, product.Id, productSku.Id);

            var inventoryData =
                await(await _productInventoryProviderResolver.GetAsync(product)).GetInventoryDataAsync(model);

            return(product.InventoryStrategy == InventoryStrategy.NoNeed || inventoryData.Inventory - quantity >= 0);
        }
    public virtual async Task <InventoryDataModel> GetInventoryDataAsync(InventoryQueryModel model)
    {
        var actor = await GetActorAsync(model);

        var stateModel = await actor.GetInventoryStateAsync();

        return(new InventoryDataModel
        {
            Inventory = stateModel.Inventory,
            Sold = stateModel.Sold
        });
    }
示例#5
0
        public virtual async Task <bool> TryIncreaseInventoryAsync(InventoryQueryModel model, int quantity,
                                                                   bool decreaseSold)
        {
            await using var handle = await _distributedLock.TryAcquireAsync(await GetLockKeyAsync(model), TimeSpan.FromSeconds(30));

            if (handle == null)
            {
                _logger.LogWarning("TryIncreaseInventory failed to acquire lock for product inventory: {TenantId},{ProductId},{ProductSkuId}",
                                   model.TenantId, model.ProductId, model.ProductSkuId);
                return(false);
            }

            var productInventory = await GetOrCreateProductInventoryAsync(model.ProductId, model.ProductSkuId);

            return(await TryIncreaseInventoryAsync(model, productInventory, quantity, decreaseSold));
        }
    public virtual async Task <bool> TryIncreaseInventoryAsync(InventoryQueryModel model, int quantity,
                                                               bool decreaseSold)
    {
        var actor = await GetActorAsync(model);

        try
        {
            await actor.IncreaseInventoryAsync(quantity, decreaseSold);
        }
        catch (Exception e)
        {
            _logger.LogError("Actor threw: {Message}", e.Message);

            return(false);
        }

        return(true);
    }
示例#7
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);
        }
    public virtual async Task <bool> TryReduceInventoryAsync(InventoryQueryModel model, int quantity, bool increaseSold)
    {
        var actor = await GetActorAsync(model);

        var stateModel = await actor.GetInventoryStateAsync();

        if (stateModel.Inventory < quantity)
        {
            return(false);
        }

        try
        {
            await actor.ReduceInventoryAsync(quantity, increaseSold);
        }
        catch (Exception e)
        {
            _logger.LogError("Actor threw: {Message}", e.Message);

            return(false);
        }

        return(true);
    }
 protected virtual ActorId GetActorId(InventoryQueryModel model)
 {
     return(new ActorId(
                $"eshop_inventory_{(model.TenantId.HasValue ? model.TenantId.Value : "host")}_{model.ProductSkuId}"));
 }
 protected virtual Task <IInventoryActor> GetActorAsync(InventoryQueryModel model)
 {
     return(Task.FromResult(ActorProxyFactory.CreateActorProxy <IInventoryActor>(GetActorId(model), ActorType)));
 }
 protected virtual string GetGrainId(InventoryQueryModel model)
 {
     return($"eshop_inventory_{(model.TenantId.HasValue ? model.TenantId.Value : "host")}_{model.ProductSkuId}");
 }
 protected virtual Task <IInventoryGrain> GetGrainAsync(InventoryQueryModel model)
 {
     return(Task.FromResult(GrainFactory.GetGrain <IInventoryGrain>(GetGrainId(model))));
 }
示例#13
0
        public virtual async Task <InventoryDataModel> GetInventoryDataAsync(Product product, ProductSku productSku)
        {
            var model = new InventoryQueryModel(product.TenantId, product.StoreId, product.Id, productSku.Id);

            return(await(await _productInventoryProviderResolver.GetAsync(product)).GetInventoryDataAsync(model));
        }
示例#14
0
 public virtual async Task <InventoryDataModel> GetInventoryDataAsync(InventoryQueryModel model)
 {
     return(await _productInventoryRepository.GetInventoryDataAsync(model.ProductSkuId));
 }
示例#15
0
 protected virtual Task <string> GetLockKeyAsync(InventoryQueryModel model)
 {
     return(Task.FromResult(string.Format(DefaultProductInventoryLockKeyFormat, model.TenantId, model.ProductId, model.ProductSkuId)));
 }