public static Task WithVendorsAsync(this IWorkContextBuilder builder, Store store, Language language) { var serviceProvider = builder.HttpContext.RequestServices; var customerService = serviceProvider.GetRequiredService <IMemberService>(); var catalogService = serviceProvider.GetRequiredService <ICatalogService>(); Func <int, int, IEnumerable <SortInfo>, IPagedList <Vendor> > factory = (pageNumber, pageSize, sortInfos) => { var vendors = customerService.SearchVendors(store, language, null, pageNumber, pageSize, sortInfos); foreach (var vendor in vendors) { vendor.Products = new MutablePagedList <Product>((pageNumber2, pageSize2, sortInfos2) => { var vendorProductsSearchCriteria = new ProductSearchCriteria { VendorId = vendor.Id, PageNumber = pageNumber2, PageSize = pageSize2, ResponseGroup = builder.WorkContext.CurrentProductSearchCriteria.ResponseGroup & ~ItemResponseGroup.ItemWithVendor, SortBy = SortInfo.ToString(sortInfos2), }; var searchResult = catalogService.SearchProducts(vendorProductsSearchCriteria); return(searchResult.Products); }, 1, ProductSearchCriteria.DefaultPageSize); } return(vendors); }; return(builder.WithVendorsAsync(() => new MutablePagedList <Vendor>(factory, 1, VendorSearchCriteria.DefaultPageSize))); }
protected virtual void SetChildCategoriesLazyLoading(Category[] categories) { foreach (var category in categories) { //Lazy loading for parents categories category.Parents = new MutablePagedList <Category>((pageNumber, pageSize, sortInfos) => { var catIds = category.Outline.Split('/'); return(new StaticPagedList <Category>(GetCategories(catIds, CategoryResponseGroup.Small), pageNumber, pageSize, catIds.Length)); }, 1, CategorySearchCriteria.DefaultPageSize); //Lazy loading for child categories category.Categories = new MutablePagedList <Category>((pageNumber, pageSize, sortInfos) => { var categorySearchCriteria = new CategorySearchCriteria { PageNumber = pageNumber, PageSize = pageSize, Outline = "/" + category.Outline }; if (!sortInfos.IsNullOrEmpty()) { categorySearchCriteria.SortBy = SortInfo.ToString(sortInfos); } var searchResult = SearchCategories(categorySearchCriteria); return(searchResult); }, 1, CategorySearchCriteria.DefaultPageSize); } }
protected virtual async Task LoadProductVendorsAsync(List <Product> products, WorkContext workContext) { var vendorIds = products.Where(p => !string.IsNullOrEmpty(p.VendorId)).Select(p => p.VendorId).Distinct().ToArray(); if (!vendorIds.IsNullOrEmpty()) { var vendors = await _customerService.GetVendorsByIdsAsync(workContext.CurrentStore, workContext.CurrentLanguage, vendorIds); foreach (var product in products) { product.Vendor = vendors.FirstOrDefault(v => v != null && v.Id == product.VendorId); if (product.Vendor != null) { product.Vendor.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos) => { var criteria = new ProductSearchCriteria { VendorId = product.VendorId, PageNumber = pageNumber, PageSize = pageSize, ResponseGroup = workContext.CurrentProductSearchCriteria.ResponseGroup & ~ItemResponseGroup.ItemWithVendor, SortBy = SortInfo.ToString(sortInfos), }; var searchResult = SearchProducts(criteria); return(searchResult.Products); }, 1, ProductSearchCriteria.DefaultPageSize); } } } }
/// <summary> /// Search in multiple data sources. /// </summary> /// <param name="criteria"></param> /// <returns></returns> public MembersSearchResult SearchMembers(MembersSearchCriteria criteria) { var retVal = new MembersSearchResult(); var skip = criteria.Skip; var take = criteria.Take; var memberTypes = criteria.MemberTypes; /// !!!Ahtung!!!: Because members can be searched in multiple data sources we have to always use sorting by memberType field (asc or desc) /// instead pagination will not works properly var sortByMemberType = criteria.SortInfos.FirstOrDefault(x => string.Equals(x.SortColumn, "memberType", StringComparison.OrdinalIgnoreCase)) ?? new SortInfo { SortColumn = "memberType" }; var sortInfos = criteria.SortInfos.Where(x => x != sortByMemberType); criteria.Sort = SortInfo.ToString(new[] { sortByMemberType }.Concat(sortInfos)); foreach (var memberMapping in _memberMappings) { criteria.MemberTypes = memberTypes.IsNullOrEmpty() ? memberMapping.AllSupportedMemberTypeNames.ToArray() : memberMapping.AllSupportedMemberTypeNames.Intersect(memberTypes, StringComparer.OrdinalIgnoreCase).ToArray(); if (!criteria.MemberTypes.IsNullOrEmpty() && criteria.Take >= 0) { var result = memberMapping.MemberSearchService.SearchMembers(criteria); retVal.Members.AddRange(result.Members); retVal.TotalCount += result.TotalCount; criteria.Skip = Math.Max(0, skip - retVal.TotalCount); criteria.Take = Math.Max(0, take - result.Members.Count()); } } //restore back criteria property values criteria.Skip = skip; criteria.Take = take; criteria.MemberTypes = memberTypes; return(retVal); }
private void LoadProductVendors(List <Product> products) { var vendorIds = products.Where(p => !string.IsNullOrEmpty(p.VendorId)).Select(p => p.VendorId).Distinct().ToList(); var vendors = vendorIds.Select(id => _customerService.GetVendorById(id)).ToList(); foreach (var product in products) { product.Vendor = vendors.FirstOrDefault(v => v.Id == product.VendorId); if (product.Vendor != null) { product.Vendor.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos) => { var workContext = _workContextFactory(); var criteria = new CatalogSearchCriteria { CatalogId = workContext.CurrentStore.Catalog, SearchInChildren = true, PageNumber = pageNumber, PageSize = pageSize, SortBy = SortInfo.ToString(sortInfos), ResponseGroup = CatalogSearchResponseGroup.WithProducts }; var searchResult = SearchProducts(criteria); return(searchResult.Products); }); } } }
public async Task <ActionResult> VendorDetails(string vendorId) { var vendor = (await _customerService.GetVendorsByIdsAsync(vendorId)).FirstOrDefault(); if (vendor != null) { vendor.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos) => { var criteria = new ProductSearchCriteria { VendorId = vendorId, PageNumber = pageNumber, PageSize = pageSize, SortBy = SortInfo.ToString(sortInfos), ResponseGroup = ItemResponseGroup.ItemSmall }; var searchResult = _catalogSearchService.SearchProducts(criteria); return(searchResult.Products); }); WorkContext.CurrentPageSeo = vendor.SeoInfo; WorkContext.CurrentVendor = vendor; return(View("vendor", WorkContext)); } throw new HttpException(404, "Vendor not found. Vendor ID: '" + vendorId + "'"); }
public async Task <ActionResult> ProductDetails(string productId) { var product = (await _catalogSearchService.GetProductsAsync(new[] { productId }, WorkContext.CurrentProductResponseGroup)).FirstOrDefault(); WorkContext.CurrentProduct = product; if (product != null) { WorkContext.CurrentPageSeo = product.SeoInfo; if (product.CategoryId != null) { var category = (await _catalogSearchService.GetCategoriesAsync(new[] { product.CategoryId }, CategoryResponseGroup.Full)).FirstOrDefault(); WorkContext.CurrentCategory = category; if (category != null) { category.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos) => { var criteria = WorkContext.CurrentCatalogSearchCriteria.Clone(); criteria.CategoryId = product.CategoryId; criteria.PageNumber = pageNumber; criteria.PageSize = pageSize; if (string.IsNullOrEmpty(criteria.SortBy) && !sortInfos.IsNullOrEmpty()) { criteria.SortBy = SortInfo.ToString(sortInfos); } return(_catalogSearchService.SearchProducts(criteria).Products); }); } } } return(View("product", WorkContext)); }
protected virtual Task LoadProductCustomerReviewsAsync(List <Product> products, WorkContext workContext) { if (products == null) { throw new ArgumentNullException(nameof(products)); } foreach (var product in products) { //Lazy loading for customer reviews product.CustomerReviews = new MutablePagedList <Model.CustomerReviews.CustomerReview>((pageNumber, pageSize, sortInfos) => { var criteria = new CustomerReviewSearchCriteria { ProductIds = new[] { product.Id }, PageNumber = pageNumber, PageSize = pageSize, Sort = SortInfo.ToString(sortInfos), }; var reviews = _customerReviewService.SearchReviews(criteria); foreach (var review in reviews) { review.ReviewRatings = new MutablePagedList <Model.ReviewRatings.ReviewRating>((pn, ps, si) => { return(_customerReviewService.GetReviewRatings(review.Id)); }, 1, CustomerReviewSearchCriteria.DefaultPageSize); } return(reviews); }, 1, CustomerReviewSearchCriteria.DefaultPageSize); } return(Task.CompletedTask); }
public async Task <ActionResult> VendorDetails(string vendorId) { var vendor = await _customerService.GetVendorByIdAsync(vendorId); if (vendor != null) { vendor.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos) => { var criteria = new CatalogSearchCriteria { CatalogId = base.WorkContext.CurrentStore.Catalog, VendorId = vendorId, SearchInChildren = true, PageNumber = pageNumber, PageSize = pageSize, SortBy = SortInfo.ToString(sortInfos), ResponseGroup = CatalogSearchResponseGroup.WithProducts }; var searchResult = _catalogSearchService.SearchProducts(criteria); return(searchResult.Products); }); WorkContext.CurrentPageSeo = vendor.SeoInfo; WorkContext.CurrentVendor = vendor; return(View("vendor", WorkContext)); } throw new HttpException(404, "Vendor not found. Vendor ID: '" + vendorId + "'"); }
protected virtual void LoadProductVendors(List <Product> products) { var vendorIds = products.Where(p => !string.IsNullOrEmpty(p.VendorId)).Select(p => p.VendorId).Distinct().ToArray(); if (!vendorIds.IsNullOrEmpty()) { var vendors = _customerService.GetVendorsByIds(vendorIds); foreach (var product in products) { product.Vendor = vendors.FirstOrDefault(v => v != null && v.Id == product.VendorId); if (product.Vendor != null) { product.Vendor.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos) => { var criteria = new ProductSearchCriteria { VendorId = product.VendorId, PageNumber = pageNumber, PageSize = pageSize, ResponseGroup = ItemResponseGroup.ItemSmall, SortBy = SortInfo.ToString(sortInfos), }; var searchResult = SearchProducts(criteria); return(searchResult.Products); }); } } } }
public async Task <ActionResult> VendorDetails(string vendorId) { var vendor = (await _customerService.GetVendorsByIdsAsync(base.WorkContext.CurrentStore, base.WorkContext.CurrentLanguage, vendorId)).FirstOrDefault(); if (vendor != null) { vendor.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos) => { var criteria = new ProductSearchCriteria { VendorId = vendorId, PageNumber = pageNumber, PageSize = pageSize, SortBy = SortInfo.ToString(sortInfos), ResponseGroup = base.WorkContext.CurrentProductSearchCriteria.ResponseGroup }; var searchResult = _catalogService.SearchProducts(criteria); return(searchResult.Products); }, 1, ProductSearchCriteria.DefaultPageSize); WorkContext.CurrentPageSeo = vendor.SeoInfo; WorkContext.CurrentVendor = vendor; return(View("vendor", WorkContext)); } return(NotFound()); }
protected virtual Task LoadProductCustomerReviewsAsync(Product[] products) { if (products == null) { throw new ArgumentNullException(nameof(products)); } foreach (var product in products) { // Lazy loading for customer reviews product.CustomerReviews = new MutablePagedList <Model.CustomerReviews.CustomerReview>( (pageNumber, pageSize, sortInfos) => { var criteria = new CustomerReviewSearchCriteria { ProductIds = new[] { product.Id }, PageNumber = pageNumber, PageSize = pageSize, Sort = SortInfo.ToString(sortInfos) }; return(_customerReviewService.SearchReviews(criteria)); }, 1, CustomerReviewSearchCriteria.DefaultPageSize); } return(Task.CompletedTask); }
public async Task <ActionResult> CategoryBrowsing(string categoryId, string view) { var category = (await _searchService.GetCategoriesAsync(new[] { categoryId }, CategoryResponseGroup.Full)).FirstOrDefault(); if (category == null) { return(NotFound($"Category {categoryId} not found.")); } WorkContext.CurrentCategory = category; WorkContext.CurrentPageSeo = category.SeoInfo.JsonClone(); WorkContext.CurrentPageSeo.Slug = category.Url; var criteria = WorkContext.CurrentProductSearchCriteria.Clone() as ProductSearchCriteria; criteria.Outline = category.Outline; // should we simply take it from current category? category.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos, @params) => { criteria.PageNumber = pageNumber; criteria.PageSize = pageSize; if (string.IsNullOrEmpty(criteria.SortBy) && !sortInfos.IsNullOrEmpty()) { criteria.SortBy = SortInfo.ToString(sortInfos); } if (@params != null) { criteria.CopyFrom(@params); } var result = _searchService.SearchProducts(criteria); //Need change ProductSearchResult with preserve reference because Scriban engine keeps this reference and use new operator will create the new //object that doesn't tracked by Scriban WorkContext.ProductSearchResult.Aggregations = result.Aggregations; WorkContext.ProductSearchResult.Products = result.Products; return(result.Products); }, 1, ProductSearchCriteria.DefaultPageSize); WorkContext.ProductSearchResult.Products = category.Products; // make sure title is set if (string.IsNullOrEmpty(WorkContext.CurrentPageSeo.Title)) { WorkContext.CurrentPageSeo.Title = category.Name; } if (string.IsNullOrEmpty(view)) { view = "grid"; } if (view.Equals("list", StringComparison.OrdinalIgnoreCase)) { return(View("collection.list", WorkContext)); } return(View("collection", WorkContext)); }
public async Task <ActionResult> CategoryBrowsing(string categoryId, string view) { var category = (await _searchService.GetCategoriesAsync(new[] { categoryId }, CategoryResponseGroup.Full)).FirstOrDefault(); if (category == null) { return(NotFound($"Category {categoryId} not found.")); } WorkContext.CurrentCategory = category; WorkContext.CurrentPageSeo = category.SeoInfo.JsonClone(); WorkContext.CurrentPageSeo.Slug = category.Url; var criteria = WorkContext.CurrentProductSearchCriteria.Clone() as ProductSearchCriteria; criteria.Outline = category.Outline; // should we simply take it from current category? category.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos, @params) => { criteria.PageNumber = pageNumber; criteria.PageSize = pageSize; if (string.IsNullOrEmpty(criteria.SortBy) && !sortInfos.IsNullOrEmpty()) { criteria.SortBy = SortInfo.ToString(sortInfos); } if (@params != null) { criteria.CopyFrom(@params); } var result = _searchService.SearchProducts(criteria); //Prevent double api request for get aggregations //Because catalog search products returns also aggregations we can use it to populate workContext using C# closure //now workContext.Aggregation will be contains preloaded aggregations for current search criteria WorkContext.Aggregations = new MutablePagedList <Aggregation>(result.Aggregations); return(result.Products); }, 1, ProductSearchCriteria.DefaultPageSize); // make sure title is set if (string.IsNullOrEmpty(WorkContext.CurrentPageSeo.Title)) { WorkContext.CurrentPageSeo.Title = category.Name; } if (string.IsNullOrEmpty(view)) { view = "grid"; } if (view.Equals("list", StringComparison.OrdinalIgnoreCase)) { return(View("collection.list", WorkContext)); } return(View("collection", WorkContext)); }
public async Task <ActionResult> ProductDetails(string productId) { var product = (await _catalogSearchService.GetProductsAsync(new[] { productId }, WorkContext.CurrentProductResponseGroup)).FirstOrDefault(); WorkContext.CurrentProduct = product; if (product != null) { WorkContext.CurrentPageSeo = product.SeoInfo.JsonClone(); WorkContext.CurrentPageSeo.Slug = product.Url; // make sure title is set if (string.IsNullOrEmpty(WorkContext.CurrentPageSeo.Title)) { WorkContext.CurrentPageSeo.Title = product.Name; } //Lazy initialize product breadcrumbs WorkContext.Breadcrumbs = new MutablePagedList <Breadcrumb>((pageNumber, pageSize, sortInfos, @params) => { var breadcrumbs = product.GetBreadcrumbs().ToList(); return(new StaticPagedList <Breadcrumb>(breadcrumbs, pageNumber, pageSize, breadcrumbs.Count)); }, 1, int.MaxValue); if (product.CategoryId != null) { var category = (await _catalogSearchService.GetCategoriesAsync(new[] { product.CategoryId }, CategoryResponseGroup.Full)).FirstOrDefault(); WorkContext.CurrentCategory = category; if (category != null) { category.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos, @params) => { var criteria = WorkContext.CurrentProductSearchCriteria.Clone() as ProductSearchCriteria; criteria.Outline = product.GetCategoryOutline(); criteria.PageNumber = pageNumber; criteria.PageSize = pageSize; if (string.IsNullOrEmpty(criteria.SortBy) && !sortInfos.IsNullOrEmpty()) { criteria.SortBy = SortInfo.ToString(sortInfos); } if (@params != null) { criteria.CopyFrom(@params); } return(_catalogSearchService.SearchProducts(criteria).Products); }, 1, ProductSearchCriteria.DefaultPageSize); } } } return(View("product", WorkContext)); }
protected virtual async Task LoadProductAssociationsAsync(IEnumerable <Product> products) { var allAssociations = products.SelectMany(x => x.Associations).ToList(); var allProductAssociations = allAssociations.OfType <ProductAssociation>().ToList(); var allCategoriesAssociations = allAssociations.OfType <CategoryAssociation>().ToList(); if (allProductAssociations.Any()) { var productsIds = allProductAssociations.Select(x => x.ProductId).ToArray(); var allAssociatedProducts = await GetProductsAsync(allProductAssociations.Select(x => x.ProductId).ToArray(), ItemResponseGroup.ItemInfo | ItemResponseGroup.ItemWithPrices | ItemResponseGroup.Seo | ItemResponseGroup.Outlines); foreach (var productAssociation in allProductAssociations) { productAssociation.Product = allAssociatedProducts.FirstOrDefault(x => x.Id == productAssociation.ProductId); } } if (allCategoriesAssociations.Any()) { var categoriesIds = allCategoriesAssociations.Select(x => x.CategoryId).ToArray(); var allAssociatedCategories = await GetCategoriesAsync(categoriesIds, CategoryResponseGroup.Info | CategoryResponseGroup.WithSeo | CategoryResponseGroup.WithOutlines | CategoryResponseGroup.WithImages); foreach (var categoryAssociation in allCategoriesAssociations) { categoryAssociation.Category = allAssociatedCategories.FirstOrDefault(x => x.Id == categoryAssociation.CategoryId); if (categoryAssociation.Category != null && categoryAssociation.Category.Products == null) { categoryAssociation.Category.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos) => { var criteria = new ProductSearchCriteria { PageNumber = pageNumber, PageSize = pageSize, Outline = categoryAssociation.Category.Outline, ResponseGroup = ItemResponseGroup.ItemInfo | ItemResponseGroup.ItemWithPrices | ItemResponseGroup.Inventory | ItemResponseGroup.ItemWithVendor }; if (!sortInfos.IsNullOrEmpty()) { criteria.SortBy = SortInfo.ToString(sortInfos); } var searchResult = SearchProducts(criteria); return(searchResult.Products); }, 1, ProductSearchCriteria.DefaultPageSize); } } } }
private async Task LoadProductAssociationsAsync(IEnumerable <Product> products) { var workContext = _workContextFactory(); var allAssociations = products.SelectMany(x => x.Associations).ToList(); var allProductAssociations = allAssociations.OfType <ProductAssociation>().ToList(); var allCategoriesAssociations = allAssociations.OfType <CategoryAssociation>().ToList(); if (allProductAssociations.Any()) { var allAssociatedProducts = await GetProductsAsync(allProductAssociations.Select(x => x.ProductId).ToArray(), ItemResponseGroup.ItemInfo | ItemResponseGroup.ItemWithPrices | ItemResponseGroup.Seo); foreach (var productAssociation in allProductAssociations) { productAssociation.Product = allAssociatedProducts.FirstOrDefault(x => x.Id == productAssociation.ProductId); } } if (allCategoriesAssociations.Any()) { var allAssociatedCategories = await GetCategoriesAsync(allCategoriesAssociations.Select(x => x.CategoryId).ToArray(), CategoryResponseGroup.Info | CategoryResponseGroup.WithSeo | CategoryResponseGroup.WithOutlines | CategoryResponseGroup.WithImages); foreach (var categoryAssociation in allCategoriesAssociations) { categoryAssociation.Category = allAssociatedCategories.FirstOrDefault(x => x.Id == categoryAssociation.CategoryId); if (categoryAssociation.Category != null && categoryAssociation.Category.Products == null) { categoryAssociation.Category.Products = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos) => { var criteria = new CatalogSearchCriteria { PageNumber = pageNumber, PageSize = pageSize, CatalogId = workContext.CurrentStore.Catalog, CategoryId = categoryAssociation.CategoryId, SearchInChildren = true, ResponseGroup = CatalogSearchResponseGroup.WithProducts, }; if (!sortInfos.IsNullOrEmpty()) { criteria.SortBy = SortInfo.ToString(sortInfos); } var searchResult = SearchProducts(criteria); return(searchResult.Products); }); } } } }
public static Task WithFulfillmentCentersAsync(this IWorkContextBuilder builder) { var serviceProvider = builder.HttpContext.RequestServices; var inventoryService = serviceProvider.GetRequiredService <IInventoryService>(); Func <int, int, IEnumerable <SortInfo>, IPagedList <FulfillmentCenter> > factory = (pageNumber, pageSize, sortInfos) => { return(inventoryService.SearchFulfillmentCenters(new FulfillmentCenterSearchCriteria { PageNumber = pageNumber, PageSize = pageNumber, Sort = SortInfo.ToString(sortInfos) })); }; return(builder.WithFulfillmentCentersAsync(() => new MutablePagedList <FulfillmentCenter>(factory, 1, FulfillmentCenterSearchCriteria.DefaultPageSize))); }
protected virtual Task LoadProductsAssociationsAsync(IEnumerable <Product> products, WorkContext workContext) { if (products == null) { throw new ArgumentNullException(nameof(products)); } foreach (var product in products) { //Associations product.Associations = new MutablePagedList <ProductAssociation>((pageNumber, pageSize, sortInfos, @params) => { var criteria = new ProductAssociationSearchCriteria { PageNumber = pageNumber, PageSize = pageSize, ProductId = product.Id, ResponseGroup = ItemResponseGroup.ItemInfo | ItemResponseGroup.ItemWithPrices | ItemResponseGroup.Inventory | ItemResponseGroup.ItemWithVendor }; if (!sortInfos.IsNullOrEmpty()) { criteria.Sort = SortInfo.ToString(sortInfos); } if (@params != null) { criteria.CopyFrom(@params); } var cacheKey = CacheKey.With(GetType(), "SearchProductAssociations", criteria.GetCacheKey()); var searchResult = _memoryCache.GetOrCreateExclusive(cacheKey, cacheEntry => { cacheEntry.AddExpirationToken(CatalogCacheRegion.CreateChangeToken()); cacheEntry.AddExpirationToken(_apiChangesWatcher.CreateChangeToken()); return(_productsApi.SearchProductAssociations(criteria.ToProductAssociationSearchCriteriaDto())); }); //Load products for resulting associations var associatedProducts = GetProductsAsync(searchResult.Results.Select(x => x.AssociatedObjectId).ToArray(), criteria.ResponseGroup).GetAwaiter().GetResult(); var result = new List <ProductAssociation>(); foreach (var associationDto in searchResult.Results) { var productAssociation = associationDto.ToProductAssociation(); productAssociation.Product = associatedProducts.FirstOrDefault(x => x.Id.EqualsInvariant(productAssociation.ProductId)); result.Add(productAssociation); } return(new StaticPagedList <ProductAssociation>(result, pageNumber, pageSize, searchResult.TotalCount ?? 0)); }, 1, ProductSearchCriteria.DefaultPageSize); } return(Task.CompletedTask); }
public IPagedList <Vendor> SearchVendors(string keyword, int pageNumber, int pageSize, IEnumerable <SortInfo> sortInfos) { var workContext = _workContextFactory(); var criteria = new CustomerModule.Client.Model.MembersSearchCriteria { Keyword = keyword, DeepSearch = true, Skip = (pageNumber - 1) * pageSize, Take = pageSize }; if (!sortInfos.IsNullOrEmpty()) { criteria.Sort = SortInfo.ToString(sortInfos); } var result = _customerApi.CustomerModuleSearchVendors(criteria); return(new StaticPagedList <Vendor>(result.Vendors.Select(x => x.ToWebModel(workContext.CurrentLanguage, workContext.CurrentStore)), pageNumber, pageSize, result.TotalCount.Value)); }
public virtual IPagedList <Vendor> SearchVendors(string keyword, int pageNumber, int pageSize, IEnumerable <SortInfo> sortInfos) { // TODO: implement indexed search for vendors var workContext = _workContextFactory(); var criteria = new customerDto.MembersSearchCriteria { SearchPhrase = keyword, DeepSearch = true, Skip = (pageNumber - 1) * pageSize, Take = pageSize }; if (!sortInfos.IsNullOrEmpty()) { criteria.Sort = SortInfo.ToString(sortInfos); } var result = _customerApi.CustomerModule.SearchVendors(criteria); return(new StaticPagedList <Vendor>(result.Vendors.Select(x => x.ToVendor(workContext.CurrentLanguage, workContext.CurrentStore)), pageNumber, pageSize, result.TotalCount.Value)); }
public async Task <Organization> GetOrganizationByIdAsync(string organizationId) { Organization result = null; var cacheKey = CacheKey.With(GetType(), "GetOrganizationByIdAsync", organizationId); var dto = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) => { var organizationDto = await _customerApi.GetOrganizationByIdAsync(organizationId); if (organizationDto != null) { cacheEntry.AddExpirationToken(CustomerCacheRegion.CreateChangeToken(organizationDto.Id)); cacheEntry.AddExpirationToken(_apiChangesWatcher.CreateChangeToken()); } return(organizationDto); }); if (dto != null) { result = dto.ToOrganization(); //Lazy load organization contacts result.Contacts = new MutablePagedList <Contact>((pageNumber, pageSize, sortInfos, @params) => { var criteria = new OrganizationContactsSearchCriteria { OrganizationId = result.Id, PageNumber = pageNumber, PageSize = pageSize }; if (!sortInfos.IsNullOrEmpty()) { criteria.Sort = SortInfo.ToString(sortInfos); } if (@params != null) { criteria.CopyFrom(@params); } return(SearchOrganizationContacts(criteria)); }, 1, 20); } return(result); }
public virtual IPagedList <Vendor> SearchVendors(Store store, Language language, string keyword, int pageNumber, int pageSize, IEnumerable <SortInfo> sortInfos) { // TODO: implement indexed search for vendors //TODO: Add caching for vendors var criteria = new customerDto.MembersSearchCriteria { SearchPhrase = keyword, DeepSearch = true, Skip = (pageNumber - 1) * pageSize, Take = pageSize }; if (!sortInfos.IsNullOrEmpty()) { criteria.Sort = SortInfo.ToString(sortInfos); } var vendorSearchResult = _customerApi.SearchVendors(criteria); var vendors = vendorSearchResult.Vendors.Select(x => x.ToVendor(language, store)); return(new StaticPagedList <Vendor>(vendors, pageNumber, pageSize, vendorSearchResult.TotalCount.Value)); }
protected virtual void SetChildCategoriesLazyLoading(Category[] categories) { foreach (var category in categories) { //Lazy loading for child categories category.Categories = new MutablePagedList <Category>((pageNumber, pageSize, sortInfos2) => { var categorySearchCriteria = new CategorySearchCriteria { PageNumber = pageNumber, PageSize = pageSize, Outline = "/" + category.Outline }; if (!sortInfos2.IsNullOrEmpty()) { categorySearchCriteria.SortBy = SortInfo.ToString(sortInfos2); } var searchResult = SearchCategories(categorySearchCriteria); return(searchResult); }, 1, CategorySearchCriteria.DefaultPageSize); } }
protected virtual void EstablishLazyDependenciesForCategories(IEnumerable <Category> categories) { if (categories == null) { throw new ArgumentNullException(nameof(categories)); } foreach (var category in categories) { //Lazy loading for parents categories category.Parents = new MutablePagedList <Category>((pageNumber, pageSize, sortInfos) => { var catIds = category.Outline.Split('/'); return(new StaticPagedList <Category>(GetCategories(catIds, CategoryResponseGroup.Small), pageNumber, pageSize, catIds.Length)); }, 1, CategorySearchCriteria.DefaultPageSize); //Lazy loading for child categories category.Categories = new MutablePagedList <Category>((pageNumber, pageSize, sortInfos, @params) => { var categorySearchCriteria = new CategorySearchCriteria { PageNumber = pageNumber, PageSize = pageSize, Outline = "/" + category.Outline }; if (!sortInfos.IsNullOrEmpty()) { categorySearchCriteria.SortBy = SortInfo.ToString(sortInfos); } if (@params != null) { categorySearchCriteria.CopyFrom(@params); } var searchResult = SearchCategories(categorySearchCriteria); return(searchResult); }, 1, CategorySearchCriteria.DefaultPageSize); } }
protected virtual Task LoadProductCustomerReviewsAsync(List <Product> products, WorkContext workContext) { if (products == null) { throw new ArgumentNullException(nameof(products)); } foreach (var product in products) { product.CustomerReviews = new MutablePagedList <Model.CustomerReviews.CustomerReview>((pageNumber, pageSize, sortInfos, @params) => { var criteria = new CustomerReviewSearchCriteria() { PageNumber = pageNumber, PageSize = pageSize, ProductIds = new[] { product.Id }, Sort = SortInfo.ToString(sortInfos) }; return(_customerReviewService.GetCustomerReviews(criteria)); }, 1, CustomerReviewSearchCriteria.DefaultPageSize); } return(Task.CompletedTask); }
public virtual IPagedList <Vendor> SearchVendors(Store store, Language language, string keyword, int pageNumber, int pageSize, IEnumerable <SortInfo> sortInfos) { // TODO: implement indexed search for vendors var criteria = new customerDto.MembersSearchCriteria { SearchPhrase = keyword, DeepSearch = true, Skip = (pageNumber - 1) * pageSize, Take = pageSize }; if (!sortInfos.IsNullOrEmpty()) { criteria.Sort = SortInfo.ToString(sortInfos); } var cacheKey = CacheKey.With(GetType(), "SearchVendors", keyword, pageNumber.ToString(), pageSize.ToString(), criteria.Sort); var result = _memoryCache.GetOrCreateExclusive(cacheKey, cacheEntry => { return(_customerApi.SearchVendors(criteria)); }); var vendors = result.Vendors.Select(x => x.ToVendor(language, store)); return(new StaticPagedList <Vendor>(vendors, pageNumber, pageSize, result.TotalCount.Value)); }
/// <summary> /// Search in multiple data sources. /// </summary> /// <param name="criteria"></param> /// <returns></returns> public GenericSearchResult <Member> SearchMembers(MembersSearchCriteria criteria) { var retVal = new GenericSearchResult <Member>(); var skip = criteria.Skip; var take = criteria.Take; var memberTypes = criteria.MemberTypes; /// !!!Ahtung!!!: Because operations can be searched in multiple data sources we have to always use sorting by operationType field (asc or desc) /// instead pagination will not works properly var sortByOperationType = criteria.SortInfos.FirstOrDefault(x => string.Equals(x.SortColumn, "memberType", StringComparison.OrdinalIgnoreCase)) ?? new SortInfo { SortColumn = "memberType" }; var sortInfos = criteria.SortInfos.Where(x => x != sortByOperationType); criteria.Sort = SortInfo.ToString(new[] { sortByOperationType }.Concat(sortInfos)); var operationSearchServiceGroups = GetServiceTypeInfoGroups <IMemberSearchService>(); foreach (var serviceGroup in operationSearchServiceGroups) { var allInheritedTypeNames = serviceGroup.SelectMany(m => m.AllSubclasses.Select(x => x.Name)).Distinct().ToArray(); criteria.MemberTypes = memberTypes.IsNullOrEmpty() ? allInheritedTypeNames : allInheritedTypeNames.Intersect(memberTypes, StringComparer.OrdinalIgnoreCase).ToArray(); if (!criteria.MemberTypes.IsNullOrEmpty() && criteria.Take >= 0) { var result = serviceGroup.Key.SearchMembers(criteria); retVal.Results.AddRange(result.Results); retVal.TotalCount += result.TotalCount; criteria.Skip = Math.Max(0, skip - retVal.TotalCount); criteria.Take = Math.Max(0, take - result.Results.Count()); } } //restore back criteria property values criteria.Skip = skip; criteria.Take = take; criteria.MemberTypes = memberTypes; return(retVal); }
private async Task LoadProductCustomerReviewsAsync(List <Product> products) { if (products == null) { throw new ArgumentNullException(nameof(products)); } foreach (var product in products) { //Lazy loading for customer reviews product.CustomerReviews = new MutablePagedList <Model.CustomerReviews.CustomerReview>((pageNumber, pageSize, sortInfos) => { var criteria = new CustomerReviewSearchCriteria { ProductIds = new[] { product.Id }, PageNumber = pageNumber, PageSize = pageSize, Sort = SortInfo.ToString(sortInfos), }; return(_customerReviewService.SearchReviews(criteria)); }, 1, CustomerReviewSearchCriteria.DefaultPageSize); product.AverageRating = await _customerReviewService.GetAverageRatingAsync(product.Id); } }
protected virtual async Task HandleNonAssetRequest(IOwinContext context, WorkContext workContext) { await InitializeShoppingCart(context, workContext); if (workContext.CurrentStore.QuotesEnabled) { var quoteRequestBuilder = Container.Resolve <IQuoteRequestBuilder>(); await quoteRequestBuilder.GetOrCreateNewTransientQuoteRequestAsync(workContext.CurrentStore, workContext.CurrentCustomer, workContext.CurrentLanguage, workContext.CurrentCurrency); workContext.CurrentQuoteRequest = quoteRequestBuilder.QuoteRequest; } var linkListService = Container.Resolve <IMenuLinkListService>(); var linkLists = await CacheManager.GetAsync("GetAllStoreLinkLists-" + workContext.CurrentStore.Id, "ApiRegion", async() => await linkListService.LoadAllStoreLinkListsAsync(workContext.CurrentStore.Id)); workContext.CurrentLinkLists = linkLists.GroupBy(x => x.Name).Select(x => x.FindWithLanguage(workContext.CurrentLanguage)).Where(x => x != null).ToList(); // load all static content var staticContents = CacheManager.Get(string.Join(":", "AllStoreStaticContent", workContext.CurrentStore.Id), "ContentRegion", () => { var staticContentService = Container.Resolve <IStaticContentService>(); var allContentItems = staticContentService.LoadStoreStaticContent(workContext.CurrentStore).ToList(); var blogs = allContentItems.OfType <Blog>().ToArray(); var blogArticlesGroup = allContentItems.OfType <BlogArticle>().GroupBy(x => x.BlogName, x => x).ToList(); foreach (var blog in blogs) { var blogArticles = blogArticlesGroup.FirstOrDefault(x => string.Equals(x.Key, blog.Name, StringComparison.OrdinalIgnoreCase)); if (blogArticles != null) { blog.Articles = new MutablePagedList <BlogArticle>(blogArticles); } } return(new { Pages = allContentItems, Blogs = blogs }); }); workContext.Pages = new MutablePagedList <ContentItem>(staticContents.Pages.Where(x => x.Language.IsInvariant || x.Language == workContext.CurrentLanguage)); workContext.Blogs = new MutablePagedList <Blog>(staticContents.Blogs.Where(x => x.Language.IsInvariant || x.Language == workContext.CurrentLanguage)); // Initialize blogs search criteria workContext.CurrentBlogSearchCritera = new BlogSearchCriteria(workContext.QueryString); //Pricelists var pricelistCacheKey = string.Join("-", "EvaluatePriceLists", workContext.CurrentStore.Id, workContext.CurrentCustomer.Id); workContext.CurrentPricelists = await CacheManager.GetAsync(pricelistCacheKey, "ApiRegion", async() => { var evalContext = workContext.ToPriceEvaluationContextDto(); var pricingModuleApi = Container.Resolve <IPricingModuleApiClient>(); var pricingResult = await pricingModuleApi.PricingModule.EvaluatePriceListsAsync(evalContext); return(pricingResult.Select(p => p.ToPricelist(workContext.AllCurrencies, workContext.CurrentLanguage)).ToList()); }); // Vendors with their products workContext.Vendors = new MutablePagedList <Vendor>((pageNumber, pageSize, sortInfos) => { var catalogSearchService = Container.Resolve <ICatalogSearchService>(); var customerService = Container.Resolve <ICustomerService>(); var vendors = customerService.SearchVendors(null, pageNumber, pageSize, sortInfos); foreach (var vendor in vendors) { vendor.Products = new MutablePagedList <Product>((pageNumber2, pageSize2, sortInfos2) => { var criteria = new ProductSearchCriteria { VendorId = vendor.Id, PageNumber = pageNumber2, PageSize = pageSize2, ResponseGroup = workContext.CurrentProductSearchCriteria.ResponseGroup & ~ItemResponseGroup.ItemWithVendor, SortBy = SortInfo.ToString(sortInfos2), }; var searchResult = catalogSearchService.SearchProducts(criteria); return(searchResult.Products); }, 1, ProductSearchCriteria.DefaultPageSize); } return(vendors); }, 1, VendorSearchCriteria.DefaultPageSize); }