From 490c38dc5f4aa1f495e76ede7ed8a95cb1d36568 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Tue, 16 Aug 2022 13:37:11 +0800 Subject: [PATCH] Simplify methods of `IFlashSaleInventoryManager` --- .../FlashSaleOrderCreationResultEventHandler.cs | 6 ++---- .../FlashSalePlans/FlashSalePlanAppService.cs | 13 +++++-------- .../Products/FlashSaleInventoryManager.cs | 8 ++++---- .../Products/IFlashSaleInventoryManager.cs | 4 ++-- .../Products/FlashSaleInventoryAppService.cs | 8 ++------ .../Products/LocalFlashSaleInventoryManager.cs | 8 ++++---- ...shSaleOrderCreationResultEventHandlerTests.cs | 16 ++++++++-------- .../Products/FakeFlashSaleInventoryManager.cs | 4 ++-- 8 files changed, 29 insertions(+), 38 deletions(-) diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs index c626ef58..fc067341 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandler.cs +++ b/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. diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanAppService.cs b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanAppService.cs index 7fd09c35..1c73d4a4 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSalePlanAppService.cs +++ b/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); diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/FlashSaleInventoryManager.cs b/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/FlashSaleInventoryManager.cs index aaeb31ce..41ad7ee8 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/FlashSaleInventoryManager.cs +++ b/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 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 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)); } } \ No newline at end of file diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Abstractions/EasyAbp/Eshop/Products/Products/IFlashSaleInventoryManager.cs b/plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Abstractions/EasyAbp/Eshop/Products/Products/IFlashSaleInventoryManager.cs index 93558adc..8041e743 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Abstractions/EasyAbp/Eshop/Products/Products/IFlashSaleInventoryManager.cs +++ b/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 TryReduceInventoryAsync(Guid? tenantId, string providerName, Guid storeId, Guid productId, - Guid productSkuId, int quantity, bool increaseSold); + Guid productSkuId); Task TryRollBackInventoryAsync(Guid? tenantId, string providerName, Guid storeId, Guid productId, - Guid productSkuId, int quantity, bool decreaseSold); + Guid productSkuId); } \ No newline at end of file diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/FlashSaleInventoryAppService.cs b/plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/FlashSaleInventoryAppService.cs index cbac9f41..3b03efb6 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/FlashSaleInventoryAppService.cs +++ b/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 ); } } diff --git a/plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/LocalFlashSaleInventoryManager.cs b/plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/LocalFlashSaleInventoryManager.cs index 6e38ba04..aae7dad0 100644 --- a/plugins/FlashSales/src/EasyAbp.EShop.Products.Plugins.FlashSales.Application/EasyAbp/EShop/Products/Products/LocalFlashSaleInventoryManager.cs +++ b/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 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 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); } } diff --git a/plugins/FlashSales/test/EasyAbp.EShop.Plugins.FlashSales.Application.Tests/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandlerTests.cs b/plugins/FlashSales/test/EasyAbp.EShop.Plugins.FlashSales.Application.Tests/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandlerTests.cs index b526b164..bf9e63eb 100644 --- a/plugins/FlashSales/test/EasyAbp.EShop.Plugins.FlashSales.Application.Tests/EasyAbp/EShop/Plugins/FlashSales/FlashSalePlans/FlashSaleOrderCreationResultEventHandlerTests.cs +++ b/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); } } diff --git a/plugins/FlashSales/test/EasyAbp.EShop.Plugins.FlashSales.Application.Tests/EasyAbp/EShop/Products/Products/FakeFlashSaleInventoryManager.cs b/plugins/FlashSales/test/EasyAbp.EShop.Plugins.FlashSales.Application.Tests/EasyAbp/EShop/Products/Products/FakeFlashSaleInventoryManager.cs index 866e3b8c..faf0f706 100644 --- a/plugins/FlashSales/test/EasyAbp.EShop.Plugins.FlashSales.Application.Tests/EasyAbp/EShop/Products/Products/FakeFlashSaleInventoryManager.cs +++ b/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 TryReduceInventoryAsync(Guid? tenantId, string providerName, Guid storeId, Guid productId, - Guid productSkuId, int quantity, bool increaseSold) + Guid productSkuId) { return Task.FromResult(ShouldReduceSuccess); } public Task TryRollBackInventoryAsync(Guid? tenantId, string providerName, Guid storeId, Guid productId, - Guid productSkuId, int quantity, bool decreaseSold) + Guid productSkuId) { return Task.FromResult(true); }