From 02f0bd341c98d08dc32cde15d2ef7d032efd22a2 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Mon, 25 May 2020 19:56:39 +0800 Subject: [PATCH] Move create, update and delete method to ProductManager --- modules/EasyAbp.EShop.Baskets/common.props | 2 +- modules/EasyAbp.EShop.Orders/common.props | 2 +- modules/EasyAbp.EShop.Payments/common.props | 2 +- modules/EasyAbp.EShop.Products/common.props | 2 +- .../Products/Products/ProductAppService.cs | 68 +---------- .../Products/Products/IProductManager.cs | 9 ++ .../ProductCodeDuplicatedException.cs | 0 .../EShop/Products/Products/ProductManager.cs | 108 +++++++++++++++++- modules/EasyAbp.EShop.Stores/common.props | 2 +- 9 files changed, 127 insertions(+), 68 deletions(-) rename modules/EasyAbp.EShop.Products/src/{EasyAbp.EShop.Products.Application => EasyAbp.EShop.Products.Domain}/EasyAbp/EShop/Products/Products/ProductCodeDuplicatedException.cs (100%) diff --git a/modules/EasyAbp.EShop.Baskets/common.props b/modules/EasyAbp.EShop.Baskets/common.props index bf9c7312..a1e442aa 100644 --- a/modules/EasyAbp.EShop.Baskets/common.props +++ b/modules/EasyAbp.EShop.Baskets/common.props @@ -1,7 +1,7 @@ latest - 0.1.16 + 0.1.17 $(NoWarn);CS1591 true EasyAbp Team diff --git a/modules/EasyAbp.EShop.Orders/common.props b/modules/EasyAbp.EShop.Orders/common.props index bf9c7312..a1e442aa 100644 --- a/modules/EasyAbp.EShop.Orders/common.props +++ b/modules/EasyAbp.EShop.Orders/common.props @@ -1,7 +1,7 @@ latest - 0.1.16 + 0.1.17 $(NoWarn);CS1591 true EasyAbp Team diff --git a/modules/EasyAbp.EShop.Payments/common.props b/modules/EasyAbp.EShop.Payments/common.props index bf9c7312..a1e442aa 100644 --- a/modules/EasyAbp.EShop.Payments/common.props +++ b/modules/EasyAbp.EShop.Payments/common.props @@ -1,7 +1,7 @@ latest - 0.1.16 + 0.1.17 $(NoWarn);CS1591 true EasyAbp Team diff --git a/modules/EasyAbp.EShop.Products/common.props b/modules/EasyAbp.EShop.Products/common.props index bf9c7312..a1e442aa 100644 --- a/modules/EasyAbp.EShop.Products/common.props +++ b/modules/EasyAbp.EShop.Products/common.props @@ -1,7 +1,7 @@ latest - 0.1.16 + 0.1.17 $(NoWarn);CS1591 true EasyAbp Team diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs index 112b1089..d19c2074 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductAppService.cs @@ -64,52 +64,13 @@ namespace EasyAbp.EShop.Products.Products TryToSetTenantId(product); - await CheckProductCodeUniqueAsync(product); - await UpdateProductAttributesAsync(product, input); - - await Repository.InsertAsync(product, autoSave: true); - - await CheckProductDetailAvailableAsync(product.Id, input.ProductDetailId); - - await AddProductToStoreAsync(product.Id, input.StoreId); - await UpdateProductCategoriesAsync(product.Id, input.CategoryIds); + await _productManager.CreateAsync(product, input.StoreId, input.CategoryIds); return MapToGetOutputDto(product); } - - private async Task CheckProductCodeUniqueAsync(Product product) - { - if (product.Code.IsNullOrEmpty()) - { - return; - } - - if (await _repository.FindAsync(x => x.Code == product.Code && x.Id != product.Id) != null) - { - throw new ProductCodeDuplicatedException(product.Code); - } - } - - protected virtual async Task CheckProductDetailAvailableAsync(Guid currentProductId, Guid desiredProductDetailId) - { - var otherOwner = await _repository.FindAsync(x => - x.ProductDetailId == desiredProductDetailId && x.Id != currentProductId); - - // Todo: should also check ProductSku owner - - if (otherOwner != null) - { - throw new EntityNotFoundException(typeof(ProductDetail), desiredProductDetailId); - } - } - - protected virtual async Task AddProductToStoreAsync(Guid productId, Guid storeId) - { - await _productStoreRepository.InsertAsync(new ProductStore(GuidGenerator.Create(), CurrentTenant.Id, - storeId, productId, true), true); - } + public override async Task UpdateAsync(Guid id, CreateUpdateProductDto input) { @@ -122,16 +83,10 @@ namespace EasyAbp.EShop.Products.Products CheckProductIsNotStatic(product); MapToEntity(input, product); - - await CheckProductCodeUniqueAsync(product); - + await UpdateProductAttributesAsync(product, input); - await Repository.UpdateAsync(product, autoSave: true); - - await CheckProductDetailAvailableAsync(product.Id, input.ProductDetailId); - - await UpdateProductCategoriesAsync(product.Id, input.CategoryIds); + await _productManager.UpdateAsync(product, input.CategoryIds); return MapToGetOutputDto(product); } @@ -326,12 +281,10 @@ namespace EasyAbp.EShop.Products.Products var product = await GetEntityByIdAsync(id); CheckProductIsNotStatic(product); - - await _productCategoryRepository.DeleteAsync(x => x.ProductId.Equals(id)); - + await CheckStoreIsProductOwnerAsync(id, storeId); - await _repository.DeleteAsync(product); + await _productManager.DeleteAsync(product); } private static void CheckProductIsNotStatic(Product product) @@ -417,15 +370,6 @@ namespace EasyAbp.EShop.Products.Products return ObjectMapper.Map(product); } - protected virtual async Task UpdateProductCategoriesAsync(Guid productId, IEnumerable categoryIds) - { - await _productCategoryRepository.DeleteAsync(x => x.ProductId.Equals(productId)); - foreach (var categoryId in categoryIds) - { - await _productCategoryRepository.InsertAsync( - new ProductCategory(GuidGenerator.Create(), CurrentTenant.Id, categoryId, productId), true); - } - } } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/IProductManager.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/IProductManager.cs index 2277c166..487c13ac 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/IProductManager.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/IProductManager.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Domain.Services; @@ -6,6 +7,14 @@ namespace EasyAbp.EShop.Products.Products { public interface IProductManager : IDomainService { + Task CreateAsync(Product product, Guid? storeId = null, IEnumerable categoryIds = null); + + Task UpdateAsync(Product product, IEnumerable categoryIds = null); + + Task DeleteAsync(Product product); + + Task DeleteAsync(Guid id); + Task IsInventorySufficientAsync(Product product, ProductSku productSku, Guid storeId, int quantity); Task GetInventoryAsync(Product product, ProductSku productSku, Guid storeId); diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductCodeDuplicatedException.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/ProductCodeDuplicatedException.cs similarity index 100% rename from modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Application/EasyAbp/EShop/Products/Products/ProductCodeDuplicatedException.cs rename to modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/ProductCodeDuplicatedException.cs diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/ProductManager.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/ProductManager.cs index 854f68c7..1332a87d 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/ProductManager.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/ProductManager.cs @@ -1,21 +1,127 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; +using EasyAbp.EShop.Products.ProductCategories; +using EasyAbp.EShop.Products.ProductDetails; +using EasyAbp.EShop.Products.ProductStores; +using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Services; namespace EasyAbp.EShop.Products.Products { public class ProductManager : DomainService, IProductManager { + private readonly IProductRepository _productRepository; + private readonly IProductStoreRepository _productStoreRepository; + private readonly IProductCategoryRepository _productCategoryRepository; private readonly IProductInventoryProvider _productInventoryProvider; public ProductManager( + IProductRepository productRepository, + IProductStoreRepository productStoreRepository, + IProductCategoryRepository productCategoryRepository, IProductInventoryProvider productInventoryProvider) { + _productRepository = productRepository; + _productStoreRepository = productStoreRepository; + _productCategoryRepository = productCategoryRepository; _productInventoryProvider = productInventoryProvider; } + + public virtual async Task CreateAsync(Product product, Guid? storeId = null, + IEnumerable categoryIds = null) + { + await CheckProductCodeUniqueAsync(product); + + await _productRepository.InsertAsync(product, autoSave: true); + + await CheckProductDetailAvailableAsync(product.Id, product.ProductDetailId); + + await UpdateProductCategoriesAsync(product.Id, categoryIds); + + if (storeId.HasValue) + { + await AddProductToStoreAsync(product.Id, storeId.Value); + } + + return product; + } + + public virtual async Task UpdateAsync(Product product, IEnumerable categoryIds = null) + { + await CheckProductCodeUniqueAsync(product); + + await _productRepository.UpdateAsync(product, autoSave: true); + + await CheckProductDetailAvailableAsync(product.Id, product.ProductDetailId); + + await UpdateProductCategoriesAsync(product.Id, categoryIds); + + return product; + } + + public virtual async Task DeleteAsync(Product product) + { + await _productCategoryRepository.DeleteAsync(x => x.ProductId.Equals(product.Id)); + + await _productRepository.DeleteAsync(product); + } + + public virtual async Task DeleteAsync(Guid id) + { + await _productCategoryRepository.DeleteAsync(x => x.ProductId.Equals(id)); + + await _productRepository.DeleteAsync(id); + } + + protected virtual async Task AddProductToStoreAsync(Guid productId, Guid storeId) + { + await _productStoreRepository.InsertAsync(new ProductStore(GuidGenerator.Create(), CurrentTenant.Id, + storeId, productId, true), true); + } + + protected virtual async Task CheckProductCodeUniqueAsync(Product product) + { + if (product.Code.IsNullOrEmpty()) + { + return; + } + + if (await _productRepository.FindAsync(x => x.Code == product.Code && x.Id != product.Id) != null) + { + throw new ProductCodeDuplicatedException(product.Code); + } + } + protected virtual async Task CheckProductDetailAvailableAsync(Guid currentProductId, Guid desiredProductDetailId) + { + var otherOwner = await _productRepository.FindAsync(x => + x.ProductDetailId == desiredProductDetailId && x.Id != currentProductId); + + // Todo: should also check ProductSku owner + + if (otherOwner != null) + { + throw new EntityNotFoundException(typeof(ProductDetail), desiredProductDetailId); + } + } + + protected virtual async Task UpdateProductCategoriesAsync(Guid productId, IEnumerable categoryIds) + { + await _productCategoryRepository.DeleteAsync(x => x.ProductId.Equals(productId)); + + if (categoryIds == null) + { + return; + } + + foreach (var categoryId in categoryIds) + { + await _productCategoryRepository.InsertAsync( + new ProductCategory(GuidGenerator.Create(), CurrentTenant.Id, categoryId, productId), true); + } + } + public virtual async Task IsInventorySufficientAsync(Product product, ProductSku productSku, Guid storeId, int quantity) { var inventory = await _productInventoryProvider.GetInventoryAsync(product, productSku, storeId); diff --git a/modules/EasyAbp.EShop.Stores/common.props b/modules/EasyAbp.EShop.Stores/common.props index bf9c7312..a1e442aa 100644 --- a/modules/EasyAbp.EShop.Stores/common.props +++ b/modules/EasyAbp.EShop.Stores/common.props @@ -1,7 +1,7 @@ latest - 0.1.16 + 0.1.17 $(NoWarn);CS1591 true EasyAbp Team