From 7ed72561bdefe2a2ca8fc991295e784f494dc9cf Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Tue, 16 Jun 2020 15:08:09 +0800 Subject: [PATCH] Add IProductRepository.WithDetails(...) methods --- .../Products/Products/ProductAppService.cs | 7 ++-- .../Products/Products/IProductRepository.cs | 4 ++ .../Products/Products/ProductRepository.cs | 41 +++++++++++++++---- 3 files changed, 39 insertions(+), 13 deletions(-) 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 10b1125f..47c77678 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 @@ -53,8 +53,8 @@ namespace EasyAbp.EShop.Products.Products protected override IQueryable CreateFilteredQuery(GetProductListDto input) { var query = input.CategoryId.HasValue - ? _repository.GetQueryable(input.StoreId, input.CategoryId.Value) - : _repository.GetQueryable(input.StoreId); + ? _repository.WithDetails(input.StoreId, input.CategoryId.Value) + : _repository.WithDetails(input.StoreId); return input.ShowHidden ? query : query.Where(x => !x.IsHidden); } @@ -261,8 +261,7 @@ namespace EasyAbp.EShop.Products.Products foreach (var product in products) { - // Todo: How to get list with details if the queryable is customized? - var productDto = MapToGetListOutputDto(await _repository.GetAsync(product.Id)); + var productDto = MapToGetListOutputDto(product); await LoadRealInventoriesAsync(product, productDto, input.StoreId); await LoadPricesAsync(product, productDto, input.StoreId); diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/IProductRepository.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/IProductRepository.cs index 3115a4b7..1e1ab030 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/IProductRepository.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain/EasyAbp/EShop/Products/Products/IProductRepository.cs @@ -10,5 +10,9 @@ namespace EasyAbp.EShop.Products.Products IQueryable GetQueryable(Guid storeId); + IQueryable WithDetails(Guid storeId, Guid categoryId); + + IQueryable WithDetails(Guid storeId); + } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.EntityFrameworkCore/EasyAbp/EShop/Products/Products/ProductRepository.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.EntityFrameworkCore/EasyAbp/EShop/Products/Products/ProductRepository.cs index 0ffad2ac..470df422 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.EntityFrameworkCore/EasyAbp/EShop/Products/Products/ProductRepository.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.EntityFrameworkCore/EasyAbp/EShop/Products/Products/ProductRepository.cs @@ -22,19 +22,42 @@ namespace EasyAbp.EShop.Products.Products public IQueryable GetQueryable(Guid storeId, Guid categoryId) { - return from product in DbContext.Products - join productStore in DbContext.ProductStores on product.Id equals productStore.ProductId - join productCategory in DbContext.ProductCategories on product.Id equals productCategory.ProductId - where productStore.StoreId == storeId && productCategory.CategoryId == categoryId - select product; + return JoinProductCategories(GetQueryable(storeId), categoryId); } public IQueryable GetQueryable(Guid storeId) { - return from product in DbContext.Products - join productStore in DbContext.ProductStores on product.Id equals productStore.ProductId - where productStore.StoreId == storeId - select product; + return JoinProductStores(GetQueryable(), storeId); + } + + public IQueryable WithDetails(Guid storeId, Guid categoryId) + { + return JoinProductCategories(WithDetails(storeId), categoryId); + } + + public IQueryable WithDetails(Guid storeId) + { + return JoinProductStores(WithDetails(), storeId); + } + + protected virtual IQueryable JoinProductStores(IQueryable queryable, Guid storeId) + { + return queryable.Join( + DbContext.ProductStores.Where(productStore => productStore.StoreId == storeId), + product => product.Id, + productStore => productStore.ProductId, + (product, productStore) => product + ); + } + + protected virtual IQueryable JoinProductCategories(IQueryable queryable, Guid categoryId) + { + return queryable.Join( + DbContext.ProductCategories.Where(productCategory => productCategory.CategoryId == categoryId), + product => product.Id, + productCategory => productCategory.ProductId, + (product, productCategory) => product + ); } } } \ No newline at end of file