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)); }
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)); }
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 }); }
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); }
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)))); }
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)); }
public virtual async Task <InventoryDataModel> GetInventoryDataAsync(InventoryQueryModel model) { return(await _productInventoryRepository.GetInventoryDataAsync(model.ProductSkuId)); }
protected virtual Task <string> GetLockKeyAsync(InventoryQueryModel model) { return(Task.FromResult(string.Format(DefaultProductInventoryLockKeyFormat, model.TenantId, model.ProductId, model.ProductSkuId))); }