Browse Source

Simplify methods of `IFlashSaleInventoryManager`

pull/210/head
gdlcf88 4 years ago
parent
commit
490c38dc5f
  1. 6
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs
  2. 13
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanAppService.cs
  3. 8
      plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/FlashSaleInventoryManager.cs
  4. 4
      plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Abstractions/EasyAbp/Eshop/Products/Products/IFlashSaleInventoryManager.cs
  5. 8
      plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/FlashSaleInventoryAppService.cs
  6. 8
      plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/LocalFlashSaleInventoryManager.cs
  7. 16
      plugins/FlashSales/test/EasyAbp.EShop.Plugins.FlashSales.Application.Tests/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandlerTests.cs
  8. 4
      plugins/FlashSales/test/EasyAbp.EShop.Plugins.FlashSales.Application.Tests/EasyAbp/EShop/Products/Products/FakeFlashSaleInventoryManager.cs

6
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs

@ -63,10 +63,8 @@ public class FlashSaleOrderCreationResultEventHandler : IDistributedEventHandler
await FlashSaleResultRepository.UpdateAsync(flashSaleResult, autoSave: true);
if (!await FlashSaleInventoryManager.TryRollBackInventoryAsync(
plan.TenantId, product.InventoryProviderName,
plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true
))
if (!await FlashSaleInventoryManager.TryRollBackInventoryAsync(plan.TenantId, product.InventoryProviderName,
plan.StoreId, plan.ProductId, plan.ProductSkuId))
{
Logger.LogWarning("Try roll back inventory failed.");
return; // Avoid to remove cache on the UOW completed.

13
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanAppService.cs

@ -262,9 +262,8 @@ public class FlashSalePlanAppService :
return CreateFailureResultDto(FlashSalesErrorCodes.DuplicateFlashSalesOrder);
}
if (!await FlashSaleInventoryManager.TryReduceInventoryAsync(
plan.TenantId, preOrderCache.InventoryProviderName,
plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true))
if (!await FlashSaleInventoryManager.TryReduceInventoryAsync(plan.TenantId, preOrderCache.InventoryProviderName,
plan.StoreId, plan.ProductId, plan.ProductSkuId))
{
await FlashSaleCurrentResultCache.RemoveAsync(plan.Id, CurrentUser.GetId());
return CreateFailureResultDto(FlashSalesErrorCodes.ProductSkuInventoryExceeded);
@ -298,11 +297,9 @@ public class FlashSalePlanAppService :
{
Logger.LogWarning("Failed to publish the CreateFlashSaleOrderEto event!");
Logger.LogException(e, LogLevel.Warning);
await FlashSaleInventoryManager.TryRollBackInventoryAsync(
plan.TenantId, preOrderCache.InventoryProviderName,
plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true
);
await FlashSaleInventoryManager.TryRollBackInventoryAsync(plan.TenantId,
preOrderCache.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId);
await FlashSaleCurrentResultCache.RemoveAsync(plan.Id, CurrentUser.GetId());
return CreateFailureResultDto(FlashSalesErrorCodes.DistributedEventBusUnavailable);

8
plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/FlashSaleInventoryManager.cs

@ -18,16 +18,16 @@ public class FlashSaleInventoryManager : IFlashSaleInventoryManager, ITransientD
}
public virtual async Task<bool> TryReduceInventoryAsync(Guid? tenantId, string providerName, Guid storeId,
Guid productId, Guid productSkuId, int quantity, bool increaseSold)
Guid productId, Guid productSkuId)
{
return await FlashSaleInventoryReducerAppService.TryReduceAsync(new ReduceInventoryInput(
tenantId, providerName, storeId, productId, productSkuId, quantity, increaseSold));
tenantId, providerName, storeId, productId, productSkuId, 1, true));
}
public virtual async Task<bool> TryRollBackInventoryAsync(Guid? tenantId, string providerName, Guid storeId,
Guid productId, Guid productSkuId, int quantity, bool decreaseSold)
Guid productId, Guid productSkuId)
{
return await FlashSaleInventoryReducerAppService.TryIncreaseAsync(new IncreaseInventoryInput(
tenantId, providerName, storeId, productId, productSkuId, quantity, decreaseSold));
tenantId, providerName, storeId, productId, productSkuId, 1, true));
}
}

4
plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Abstractions/EasyAbp/Eshop/Products/Products/IFlashSaleInventoryManager.cs

@ -6,8 +6,8 @@ namespace EasyAbp.Eshop.Products.Products;
public interface IFlashSaleInventoryManager
{
Task<bool> TryReduceInventoryAsync(Guid? tenantId, string providerName, Guid storeId, Guid productId,
Guid productSkuId, int quantity, bool increaseSold);
Guid productSkuId);
Task<bool> TryRollBackInventoryAsync(Guid? tenantId, string providerName, Guid storeId, Guid productId,
Guid productSkuId, int quantity, bool decreaseSold);
Guid productSkuId);
}

8
plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/FlashSaleInventoryAppService.cs

@ -22,9 +22,7 @@ public class FlashSaleInventoryAppService : ProductsAppService, IFlashSaleInvent
input.ProviderName,
input.StoreId,
input.ProductId,
input.ProductSkuId,
input.Quantity,
input.IncreaseSold
input.ProductSkuId
);
}
@ -37,9 +35,7 @@ public class FlashSaleInventoryAppService : ProductsAppService, IFlashSaleInvent
input.ProviderName,
input.StoreId,
input.ProductId,
input.ProductSkuId,
input.Quantity,
input.ReduceSold
input.ProductSkuId
);
}
}

8
plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/LocalFlashSaleInventoryManager.cs

@ -16,18 +16,18 @@ public class LocalFlashSaleInventoryManager : ILocalFlashSaleInventoryManager, I
}
public virtual async Task<bool> TryReduceInventoryAsync(Guid? tenantId, string providerName, Guid storeId, Guid productId,
Guid productSkuId, int quantity, bool increaseSold)
Guid productSkuId)
{
var model = new InventoryQueryModel(tenantId, storeId, productId, productSkuId);
return await (await ProductInventoryProviderResolver.GetAsync(providerName))
.TryReduceInventoryAsync(model, quantity, increaseSold, true);
.TryReduceInventoryAsync(model, 1, true, true);
}
public virtual async Task<bool> TryRollBackInventoryAsync(Guid? tenantId, string providerName, Guid storeId, Guid productId,
Guid productSkuId, int quantity, bool decreaseSold)
Guid productSkuId)
{
var model = new InventoryQueryModel(tenantId, storeId, productId, productSkuId);
return await (await ProductInventoryProviderResolver.GetAsync(providerName))
.TryIncreaseInventoryAsync(model, quantity, decreaseSold, true);
.TryIncreaseInventoryAsync(model, 1, true, true);
}
}

16
plugins/FlashSales/test/EasyAbp.EShop.Plugins.FlashSales.Application.Tests/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandlerTests.cs

@ -97,7 +97,7 @@ public class FlashSaleOrderCreationResultEventHandlerTests : FlashSalesApplicati
};
FlashSaleInventoryManager
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true)
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId)
.Returns(Task.FromResult(true));
await FlashSaleOrderCreationResultEventHandler.HandleEventAsync(flashSaleOrderCreationResultEto);
@ -113,7 +113,7 @@ public class FlashSaleOrderCreationResultEventHandlerTests : FlashSalesApplicati
flashSaleCurrentResultCache.ResultDto.Status.ShouldBe(FlashSaleResultStatus.Successful);
await FlashSaleInventoryManager.DidNotReceive()
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true);
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId);
}
[Fact]
@ -137,7 +137,7 @@ public class FlashSaleOrderCreationResultEventHandlerTests : FlashSalesApplicati
};
FlashSaleInventoryManager
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true)
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId)
.Returns(Task.FromResult(true));
await FlashSaleOrderCreationResultEventHandler.HandleEventAsync(flashSaleOrderCreationResultEto);
@ -153,7 +153,7 @@ public class FlashSaleOrderCreationResultEventHandlerTests : FlashSalesApplicati
flashSaleCurrentResultCache.ResultDto.Status.ShouldBe(FlashSaleResultStatus.Failed);
await FlashSaleInventoryManager.Received()
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true);
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId);
}
[Fact]
@ -177,7 +177,7 @@ public class FlashSaleOrderCreationResultEventHandlerTests : FlashSalesApplicati
};
FlashSaleInventoryManager
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true)
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId)
.Returns(Task.FromResult(true));
await FlashSaleOrderCreationResultEventHandler.HandleEventAsync(flashSaleOrderCreationResultEto);
@ -191,7 +191,7 @@ public class FlashSaleOrderCreationResultEventHandlerTests : FlashSalesApplicati
flashSaleCurrentResultCache.ShouldBeNull();
await FlashSaleInventoryManager.Received()
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true);
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId);
}
[Fact]
@ -215,7 +215,7 @@ public class FlashSaleOrderCreationResultEventHandlerTests : FlashSalesApplicati
};
FlashSaleInventoryManager
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true)
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId)
.Returns(Task.FromResult(false));
await FlashSaleOrderCreationResultEventHandler.HandleEventAsync(flashSaleOrderCreationResultEto);
@ -224,6 +224,6 @@ public class FlashSaleOrderCreationResultEventHandlerTests : FlashSalesApplicati
flashSaleCurrentResultCache.ShouldNotBeNull();
await FlashSaleInventoryManager.Received()
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId, 1, true);
.TryRollBackInventoryAsync(plan.TenantId, Product1.InventoryProviderName, plan.StoreId, plan.ProductId, plan.ProductSkuId);
}
}

4
plugins/FlashSales/test/EasyAbp.EShop.Plugins.FlashSales.Application.Tests/EasyAbp/EShop/Products/Products/FakeFlashSaleInventoryManager.cs

@ -14,13 +14,13 @@ public class FakeFlashSaleInventoryManager : IFlashSaleInventoryManager
}
public Task<bool> TryReduceInventoryAsync(Guid? tenantId, string providerName, Guid storeId, Guid productId,
Guid productSkuId, int quantity, bool increaseSold)
Guid productSkuId)
{
return Task.FromResult(ShouldReduceSuccess);
}
public Task<bool> TryRollBackInventoryAsync(Guid? tenantId, string providerName, Guid storeId, Guid productId,
Guid productSkuId, int quantity, bool decreaseSold)
Guid productSkuId)
{
return Task.FromResult(true);
}

Loading…
Cancel
Save