public static module.CatalogProduct GetByIdsOptimized( this IItemService service, string[] itemIds, module.ItemResponseGroup respGroup) { return(null); }
public coreModel.CatalogProduct[] GetByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup, string catalogId = null) { coreModel.CatalogProduct[] result; using (var repository = _catalogRepositoryFactory()) { result = repository.GetItemByIds(itemIds, respGroup) .Select(x => x.ToCoreModel()) .ToArray(); } // Fill outlines for products if (respGroup.HasFlag(coreModel.ItemResponseGroup.Outlines)) { _outlineService.FillOutlinesForObjects(result, catalogId); } // Fill SEO info for products, variations and outline items if ((respGroup & coreModel.ItemResponseGroup.Seo) == coreModel.ItemResponseGroup.Seo) { var objectsWithSeo = new List <ISeoSupport>(result); var variations = result.Where(p => p.Variations != null) .SelectMany(p => p.Variations); objectsWithSeo.AddRange(variations); var outlineItems = result.Where(p => p.Outlines != null) .SelectMany(p => p.Outlines.SelectMany(o => o.Items)); objectsWithSeo.AddRange(outlineItems); _commerceService.LoadSeoForObjects(objectsWithSeo.ToArray()); } return(result); }
public dataModel.Item[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge) { if (itemIds == null) { throw new ArgumentNullException("categoriesIds"); } if (!itemIds.Any()) { return(new dataModel.Item[] { }); } //Used breaking query EF performance concept https://msdn.microsoft.com/en-us/data/hh949853.aspx#8 var retVal = Items.Include(x => x.Images).Where(x => itemIds.Contains(x.Id)).ToArray(); //Load product catalogs separately var catalogIds = retVal.Select(x => x.CatalogId).Distinct().ToArray(); var catalogs = Catalogs.Include(x => x.CatalogLanguages).Where(x => catalogIds.Contains(x.Id)).ToArray(); //Load product categories separately var categoryIds = retVal.Select(x => x.CategoryId).Where(x => !String.IsNullOrEmpty(x)).Distinct().ToArray(); var categories = Categories.Where(x => categoryIds.Contains(x.Id)).ToArray(); if ((respGroup & coreModel.ItemResponseGroup.Links) == coreModel.ItemResponseGroup.Links) { var relations = CategoryItemRelations.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if ((respGroup & coreModel.ItemResponseGroup.ItemProperties) == coreModel.ItemResponseGroup.ItemProperties) { var propertyValues = PropertyValues.Where(x => itemIds.Contains(x.ItemId)).ToArray(); //Load categories with all properties for property inheritance categories = GetCategoriesByIds(categoryIds, coreModel.CategoryResponseGroup.WithProperties); } if ((respGroup & coreModel.ItemResponseGroup.ItemAssets) == coreModel.ItemResponseGroup.ItemAssets) { var assets = Assets.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if ((respGroup & coreModel.ItemResponseGroup.ItemEditorialReviews) == coreModel.ItemResponseGroup.ItemEditorialReviews) { var editorialReviews = EditorialReviews.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if ((respGroup & coreModel.ItemResponseGroup.Variations) == coreModel.ItemResponseGroup.Variations) { var variationIds = Items.Where(x => itemIds.Contains(x.ParentId)).Select(x => x.Id).ToArray(); //For variations loads only info and images var variations = Items.Include(x => x.Images).Where(x => variationIds.Contains(x.Id)).ToArray(); //load variations property values separately var variationPropertyValues = PropertyValues.Where(x => variationIds.Contains(x.ItemId)).ToArray(); } if ((respGroup & coreModel.ItemResponseGroup.ItemAssociations) == coreModel.ItemResponseGroup.ItemAssociations) { var assosiationGroups = AssociationGroups.Include(x => x.Associations).ToArray(); var assosiatedItemIds = assosiationGroups.SelectMany(x => x.Associations).Select(x => x.ItemId).Distinct().ToArray(); var assosiationItems = GetItemByIds(assosiatedItemIds, coreModel.ItemResponseGroup.ItemInfo); } //Load parents var parentIds = retVal.Where(x => x.Parent == null && x.ParentId != null).Select(x => x.ParentId).ToArray(); var parents = GetItemByIds(parentIds, respGroup); return(retVal); }
public IHttpActionResult GetProduct(string store, string product, [FromUri] coreModel.ItemResponseGroup responseGroup = coreModel.ItemResponseGroup.ItemLarge, string language = "en-us") { var products = InnerGetProductsByIds(new string[] { product }, responseGroup); var retVal = products.FirstOrDefault(); if (retVal != null) { return(Ok(retVal)); } return(NotFound()); }
public coreModel.CatalogProduct[] GetByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup) { // TODO: Optimize performance (Sasha) // 1. Catalog should be cached and not retrieved every time from the db // 2. SEO info can be retrieved for all items at once instead of one by one // 3. Optimize how main variation is loaded // 4. Associations shouldn't be loaded always and must be optimized as well // 5. No need to get properties meta data to just retrieve property ID var retVal = new List <coreModel.CatalogProduct>(); using (var repository = _catalogRepositoryFactory()) { var dbItems = repository.GetItemByIds(itemIds, respGroup); SeoInfo[] seoInfos = null; if ((respGroup & coreModel.ItemResponseGroup.Seo) == coreModel.ItemResponseGroup.Seo) { seoInfos = _commerceService.GetObjectsSeo(dbItems.Select(x => x.Id).ToArray()).ToArray(); } var categoriesIds = dbItems.SelectMany(x => x.CategoryLinks).Select(x => x.CategoryId).Distinct().ToArray(); var dbCategories = repository.GetCategoriesByIds(categoriesIds); foreach (var dbItem in dbItems) { var associatedProducts = new List <coreModel.CatalogProduct>(); if ((respGroup & coreModel.ItemResponseGroup.ItemAssociations) == coreModel.ItemResponseGroup.ItemAssociations) { if (dbItem.AssociationGroups.Any()) { foreach (var association in dbItem.AssociationGroups.SelectMany(x => x.Associations)) { var associatedProduct = GetById(association.ItemId, coreModel.ItemResponseGroup.ItemAssets); associatedProducts.Add(associatedProduct); } } } var dbCatalog = repository.GetCatalogById(dbItem.CatalogId); var catalog = dbCatalog.ToCoreModel(); coreModel.Category category = null; if (dbItem.Category != null) { category = dbItem.Category.ToCoreModel(catalog); } var item = dbItem.ToCoreModel(catalog: catalog, category: category, associatedProducts: associatedProducts.ToArray()); item.SeoInfos = seoInfos != null?seoInfos.Where(x => x.ObjectId == dbItem.Id).ToList() : null; retVal.Add(item); } } return(retVal.ToArray()); }
public coreModel.CatalogProduct[] GetByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup) { // TODO: Optimize performance (Sasha) // Associations shouldn't be loaded always and must be optimized as well var retVal = new List <coreModel.CatalogProduct>(); using (var repository = _catalogRepositoryFactory()) { var dbItems = repository.GetItemByIds(itemIds, respGroup); SeoInfo[] seoInfos = null; if ((respGroup & coreModel.ItemResponseGroup.Seo) == coreModel.ItemResponseGroup.Seo) { seoInfos = _commerceService.GetObjectsSeo(dbItems.Select(x => x.Id).ToArray()).ToArray(); } var categoriesIds = dbItems.SelectMany(x => x.CategoryLinks).Select(x => x.CategoryId).Distinct().ToArray(); var dbCategories = repository.GetCategoriesByIds(categoriesIds); foreach (var dbItem in dbItems) { var associatedProducts = new List <coreModel.CatalogProduct>(); if ((respGroup & coreModel.ItemResponseGroup.ItemAssociations) == coreModel.ItemResponseGroup.ItemAssociations) { if (dbItem.AssociationGroups.Any()) { foreach (var association in dbItem.AssociationGroups.SelectMany(x => x.Associations)) { var associatedProduct = GetById(association.ItemId, coreModel.ItemResponseGroup.ItemAssets); associatedProducts.Add(associatedProduct); } } } var dbCatalog = repository.GetCatalogById(dbItem.CatalogId); var catalog = dbCatalog.ToCoreModel(); coreModel.Category category = null; if (dbItem.Category != null) { var allParents = repository.GetAllCategoryParents(dbItem.Category).ToArray(); category = dbItem.Category.ToCoreModel(catalog, null, allParents); } var item = dbItem.ToCoreModel(catalog: catalog, category: category, associatedProducts: associatedProducts.ToArray()); item.SeoInfos = seoInfos != null?seoInfos.Where(x => x.ObjectId == dbItem.Id).ToList() : null; retVal.Add(item); } } return(retVal.ToArray()); }
public IHttpActionResult Get(string id, [FromUri] coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge) { var item = _itemsService.GetById(id, respGroup); if (item == null) { return(NotFound()); } base.CheckCurrentUserHasPermissionForObjects(CatalogPredefinedPermissions.Read, item); var retVal = item.ToWebModel(_blobUrlResolver); retVal.SecurityScopes = base.GetObjectPermissionScopeStrings(item); return(Ok(retVal)); }
public IHttpActionResult GetProduct(string store, string product, [FromUri] coreModel.ItemResponseGroup responseGroup = coreModel.ItemResponseGroup.ItemLarge, string language = "en-us") { var fullLoadedStore = GetStoreById(store); if (fullLoadedStore == null) { throw new NullReferenceException(store + " not found"); } var products = InnerGetProductsByIds(fullLoadedStore, new [] { product }, responseGroup); var retVal = products.FirstOrDefault(); if (retVal != null) { return(Ok(retVal)); } return(NotFound()); }
public IHttpActionResult GetProductByIds([FromUri] string[] ids, [FromUri] coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge) { var items = _itemsService.GetByIds(ids, respGroup); if (items == null) { return(NotFound()); } CheckCurrentUserHasPermissionForObjects(CatalogPredefinedPermissions.Read, items); var retVal = items.Select(x => x.ToWebModel(_blobUrlResolver)).ToArray(); foreach (var product in retVal) { product.SecurityScopes = GetObjectPermissionScopeStrings(product); } return(Ok(retVal)); }
public dataModel.Item[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge) { if (!itemIds.Any()) { return(new dataModel.Item[] { }); } //Used breaking query EF performance concept https://msdn.microsoft.com/en-us/data/hh949853.aspx#8 var retVal = Items.Include(x => x.Catalog).Include(x => x.Category).Include(x => x.Images).Where(x => itemIds.Contains(x.Id)).ToArray(); if ((respGroup & coreModel.ItemResponseGroup.Categories) == coreModel.ItemResponseGroup.Categories) { var relations = CategoryItemRelations.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if ((respGroup & coreModel.ItemResponseGroup.ItemProperties) == coreModel.ItemResponseGroup.ItemProperties) { var propertyValues = ItemPropertyValues.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if ((respGroup & coreModel.ItemResponseGroup.ItemAssets) == coreModel.ItemResponseGroup.ItemAssets) { var assets = Assets.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if ((respGroup & coreModel.ItemResponseGroup.ItemEditorialReviews) == coreModel.ItemResponseGroup.ItemEditorialReviews) { var editorialReviews = EditorialReviews.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if ((respGroup & coreModel.ItemResponseGroup.ItemAssociations) == coreModel.ItemResponseGroup.ItemAssociations) { var associations = Associations.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if ((respGroup & coreModel.ItemResponseGroup.Variations) == coreModel.ItemResponseGroup.Variations) { var variationIds = Items.Where(x => itemIds.Contains(x.ParentId)).Select(x => x.Id).ToArray(); var variations = GetItemByIds(variationIds, respGroup); } //Load parents var parentIds = retVal.Where(x => x.Parent == null && x.ParentId != null).Select(x => x.ParentId).ToArray(); var parents = GetItemByIds(parentIds, respGroup); return(retVal); }
public coreModel.CatalogProduct[] GetByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup) { var retVal = new List <coreModel.CatalogProduct>(); using (var repository = _catalogRepositoryFactory()) { var dbItems = repository.GetItemByIds(itemIds, respGroup); retVal.AddRange(dbItems.Select(x => x.ToCoreModel())); //Populate product seo if ((respGroup & coreModel.ItemResponseGroup.Seo) == coreModel.ItemResponseGroup.Seo) { var expandedProducts = retVal.Concat(retVal.Where(x => x.Variations != null).SelectMany(x => x.Variations)).ToArray(); var allCategories = expandedProducts.Select(x => x.Category).ToArray(); var allSeoObjects = expandedProducts.OfType <ISeoSupport>().Concat(allCategories.OfType <ISeoSupport>()).ToArray(); _commerceService.LoadSeoForObjects(allSeoObjects); } } return(retVal.ToArray()); }
public coreModel.CatalogProduct GetById(string itemId, coreModel.ItemResponseGroup respGroup) { var results = this.GetByIds(new[] { itemId }, respGroup); return(results.Any() ? results.First() : null); }
public dataModel.ItemEntity[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge) { if (itemIds == null) { throw new ArgumentNullException(nameof(itemIds)); } if (!itemIds.Any()) { return(new dataModel.ItemEntity[] { }); } // Use breaking query EF performance concept https://msdn.microsoft.com/en-us/data/hh949853.aspx#8 var retVal = Items.Include(x => x.Images).Where(x => itemIds.Contains(x.Id)).ToArray(); if (respGroup.HasFlag(coreModel.ItemResponseGroup.Outlines)) { respGroup |= coreModel.ItemResponseGroup.Links; } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemProperties)) { var propertyValues = PropertyValues.Include(x => x.DictionaryItem.DictionaryItemValues).Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.Links)) { var relations = CategoryItemRelations.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssets)) { var assets = Assets.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemEditorialReviews)) { var editorialReviews = EditorialReviews.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.Variations)) { // TODO: Call GetItemByIds for variations recursively (need to measure performance and data amount first) var variationIds = Items.Where(x => itemIds.Contains(x.ParentId)).Select(x => x.Id).ToArray(); // Always load info, images and property values for variations var variations = Items.Include(x => x.Images).Where(x => variationIds.Contains(x.Id)).ToArray(); var variationPropertyValues = PropertyValues.Where(x => variationIds.Contains(x.ItemId)).ToArray(); if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssets)) { var variationAssets = Assets.Where(x => variationIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemEditorialReviews)) { var variationEditorialReviews = EditorialReviews.Where(x => variationIds.Contains(x.ItemId)).ToArray(); } } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssociations)) { var assosiations = Associations.Where(x => itemIds.Contains(x.ItemId)).ToArray(); var assosiatedProductIds = assosiations.Where(x => x.AssociatedItemId != null) .Select(x => x.AssociatedItemId).Distinct().ToArray(); var assosiatedItems = GetItemByIds(assosiatedProductIds, coreModel.ItemResponseGroup.ItemInfo | coreModel.ItemResponseGroup.ItemAssets); var assosiatedCategoryIdsIds = assosiations.Where(x => x.AssociatedCategoryId != null).Select(x => x.AssociatedCategoryId).Distinct().ToArray(); var associatedCategories = GetCategoriesByIds(assosiatedCategoryIdsIds, coreModel.CategoryResponseGroup.Info | CategoryResponseGroup.WithImages); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ReferencedAssociations)) { var referencedAssociations = Associations.Where(x => itemIds.Contains(x.AssociatedItemId)).ToArray(); var referencedProductIds = referencedAssociations.Select(x => x.ItemId).Distinct().ToArray(); var referencedProducts = GetItemByIds(referencedProductIds, coreModel.ItemResponseGroup.ItemInfo); } // Load parents var parentIds = retVal.Where(x => x.Parent == null && x.ParentId != null).Select(x => x.ParentId).ToArray(); var parents = GetItemByIds(parentIds, respGroup); return(retVal); }
public dataModel.ItemEntity[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge) { var result = Array.Empty <dataModel.ItemEntity>(); if (!itemIds.IsNullOrEmpty()) { result = Items.Include(x => x.Images).Where(x => itemIds.Contains(x.Id)).ToArray(); if (result.Any()) { itemIds = result.Select(x => x.Id).ToArray(); if (respGroup.HasFlag(coreModel.ItemResponseGroup.Outlines)) { respGroup |= coreModel.ItemResponseGroup.Links; } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemProperties)) { var propertyValues = PropertyValues.Include(x => x.DictionaryItem.DictionaryItemValues).Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.Links)) { var relations = CategoryItemRelations.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssets)) { var assets = Assets.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemEditorialReviews)) { var editorialReviews = EditorialReviews.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.Variations)) { // TODO: Call GetItemByIds for variations recursively (need to measure performance and data amount first) var variationIds = Items.Where(x => itemIds.Contains(x.ParentId)).Select(x => x.Id).ToArray(); if (!variationIds.IsNullOrEmpty()) { // Always load info, images and property values for variations var variations = Items.Include(x => x.Images).Where(x => variationIds.Contains(x.Id)).ToArray(); if (variations.Any()) { variationIds = variations.Select(x => x.Id).ToArray(); var variationPropertyValues = PropertyValues.Include(x => x.DictionaryItem.DictionaryItemValues).Where(x => variationIds.Contains(x.ItemId)).ToArray(); if (respGroup.HasFlag(ItemResponseGroup.ItemAssets)) { var variationAssets = Assets.Where(x => variationIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(ItemResponseGroup.ItemEditorialReviews)) { var variationEditorialReviews = EditorialReviews.Where(x => variationIds.Contains(x.ItemId)).ToArray(); } } } } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssociations)) { var assosiations = Associations.Where(x => itemIds.Contains(x.ItemId)).ToArray(); var assosiatedProductIds = assosiations.Where(x => x.AssociatedItemId != null) .Select(x => x.AssociatedItemId).Distinct().ToArray(); var assosiatedItems = GetItemByIds(assosiatedProductIds, coreModel.ItemResponseGroup.ItemInfo | coreModel.ItemResponseGroup.ItemAssets); var assosiatedCategoryIdsIds = assosiations.Where(x => x.AssociatedCategoryId != null).Select(x => x.AssociatedCategoryId).Distinct().ToArray(); var associatedCategories = GetCategoriesByIds(assosiatedCategoryIdsIds, coreModel.CategoryResponseGroup.Info | CategoryResponseGroup.WithImages); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ReferencedAssociations)) { var referencedAssociations = Associations.Where(x => itemIds.Contains(x.AssociatedItemId)).ToArray(); var referencedProductIds = referencedAssociations.Select(x => x.ItemId).Distinct().ToArray(); var referencedProducts = GetItemByIds(referencedProductIds, coreModel.ItemResponseGroup.ItemInfo); } // Load parents var parentIds = result.Where(x => x.Parent == null && x.ParentId != null).Select(x => x.ParentId).ToArray(); var parents = GetItemByIds(parentIds, respGroup); } } return(result); }
public dataModel.ItemEntity[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge) { var result = Array.Empty <dataModel.ItemEntity>(); if (!itemIds.IsNullOrEmpty()) { result = Items.Include(x => x.Images).Where(x => itemIds.Contains(x.Id)).ToArray(); if (result.Any()) { itemIds = result.Select(x => x.Id).ToArray(); if (respGroup.HasFlag(coreModel.ItemResponseGroup.Outlines)) { respGroup |= coreModel.ItemResponseGroup.Links; } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemProperties)) { var propertyValues = PropertyValues.Include(x => x.DictionaryItem.DictionaryItemValues).Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.Links)) { var relations = CategoryItemRelations.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssets)) { var assets = Assets.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemEditorialReviews)) { var editorialReviews = EditorialReviews.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.Variations)) { LoadVariationsByParentIds(itemIds, respGroup); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssociations)) { var assosiations = Associations.Where(x => itemIds.Contains(x.ItemId)).ToArray(); var assosiatedProductIds = assosiations.Where(x => x.AssociatedItemId != null) .Select(x => x.AssociatedItemId).Distinct().ToArray(); var assosiatedItems = GetItemByIds(assosiatedProductIds, coreModel.ItemResponseGroup.ItemInfo | coreModel.ItemResponseGroup.ItemAssets); var assosiatedCategoryIdsIds = assosiations.Where(x => x.AssociatedCategoryId != null).Select(x => x.AssociatedCategoryId).Distinct().ToArray(); var associatedCategories = GetCategoriesByIds(assosiatedCategoryIdsIds, coreModel.CategoryResponseGroup.Info | CategoryResponseGroup.WithImages); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ReferencedAssociations)) { var referencedAssociations = Associations.Where(x => itemIds.Contains(x.AssociatedItemId)).ToArray(); var referencedProductIds = referencedAssociations.Select(x => x.ItemId).Distinct().ToArray(); var referencedProducts = GetItemByIds(referencedProductIds, coreModel.ItemResponseGroup.ItemInfo); } // Load parents var parentIds = result.Where(x => x.Parent == null && x.ParentId != null).Select(x => x.ParentId).ToArray(); var parents = GetItemByIds(parentIds, respGroup); } } return(result); }
private IEnumerable <moduleModel.CatalogProduct> Search(CatalogIndexedSearchCriteria criteria, out CatalogItemSearchResults results, moduleModel.ItemResponseGroup responseGroup) { var items = new List <moduleModel.CatalogProduct>(); var itemsOrderedList = new List <string>(); var foundItemCount = 0; var dbItemCount = 0; var searchRetry = 0; //var myCriteria = criteria.Clone(); var myCriteria = criteria; do { // Search using criteria, it will only return IDs of the items var scope = _searchConnection.Scope; var searchResults = _searchProvider.Search(scope, criteria) as SearchResults; var itemKeyValues = searchResults.GetKeyAndOutlineFieldValueMap <string>(); results = new CatalogItemSearchResults(myCriteria, itemKeyValues, searchResults); searchRetry++; if (results.Items == null) { continue; } //Get only new found itemIds var uniqueKeys = results.Items.Keys.Except(itemsOrderedList).ToArray(); foundItemCount = uniqueKeys.Length; if (!results.Items.Any()) { continue; } itemsOrderedList.AddRange(uniqueKeys); // Now load items from repository var currentItems = _itemService.GetByIds(uniqueKeys.ToArray(), responseGroup, criteria.Catalog); var orderedList = currentItems.OrderBy(i => itemsOrderedList.IndexOf(i.Id)); items.AddRange(orderedList); dbItemCount = currentItems.Length; //If some items where removed and search is out of sync try getting extra items if (foundItemCount > dbItemCount) { //Retrieve more items to fill missing gap myCriteria.RecordsToRetrieve += (foundItemCount - dbItemCount); } }while (foundItemCount > dbItemCount && results.Items.Any() && searchRetry <= 3 && (myCriteria.RecordsToRetrieve + myCriteria.StartingRecord) < results.TotalCount); return(items); }
public dataModel.Item[] GetItemByIds(string[] itemIds, coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge) { if (itemIds == null) { throw new ArgumentNullException("itemIds"); } if (!itemIds.Any()) { return(new dataModel.Item[] { }); } // Use breaking query EF performance concept https://msdn.microsoft.com/en-us/data/hh949853.aspx#8 var retVal = Items.Include(x => x.Images).Where(x => itemIds.Contains(x.Id)).ToArray(); var propertyValues = PropertyValues.Where(x => itemIds.Contains(x.ItemId)).ToArray(); // Load product catalogs separately var catalogIds = retVal.Select(x => x.CatalogId).Distinct().ToArray(); var catalogs = Catalogs.Include(x => x.CatalogLanguages).Where(x => catalogIds.Contains(x.Id)).ToArray(); // Load product categories separately var categoryIds = retVal.Select(x => x.CategoryId).Where(x => !string.IsNullOrEmpty(x)).Distinct().ToArray(); var categoriesReponseGroup = coreModel.CategoryResponseGroup.WithParents; if (respGroup.HasFlag(coreModel.ItemResponseGroup.Outlines)) { categoriesReponseGroup |= coreModel.CategoryResponseGroup.WithLinks; } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemProperties)) { // Load categories with all properties for property inheritance categoriesReponseGroup |= coreModel.CategoryResponseGroup.WithProperties; } var categories = GetCategoriesByIds(categoryIds, categoriesReponseGroup); if (respGroup.HasFlag(coreModel.ItemResponseGroup.Links) || respGroup.HasFlag(coreModel.ItemResponseGroup.Outlines)) { var relations = CategoryItemRelations.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } // Load all properties meta information and data for inheritance from parent categories and catalog if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemProperties)) { // Load catalogs with properties for products not belongs to any category (EF auto populated all Catalog nav properties for all objects) foreach (var catalogId in retVal.Where(x => x.CategoryId == null).Select(x => x.CatalogId)) { var catalog = GetCatalogById(catalogId); } } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssets)) { var assets = Assets.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemEditorialReviews)) { var editorialReviews = EditorialReviews.Where(x => itemIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.Variations)) { var variationIds = Items.Where(x => itemIds.Contains(x.ParentId)).Select(x => x.Id).ToArray(); // For variations load only info and images var variations = Items.Include(x => x.Images).Include(x => x.Assets).Where(x => variationIds.Contains(x.Id)).ToArray(); // Load variations property values separately var variationPropertyValues = PropertyValues.Where(x => variationIds.Contains(x.ItemId)).ToArray(); } if (respGroup.HasFlag(coreModel.ItemResponseGroup.ItemAssociations)) { var assosiationGroups = AssociationGroups.Include(x => x.Associations).ToArray(); var assosiatedItemIds = assosiationGroups.SelectMany(x => x.Associations).Select(x => x.ItemId).Distinct().ToArray(); var assosiationItems = GetItemByIds(assosiatedItemIds, coreModel.ItemResponseGroup.ItemInfo); } // Load parents var parentIds = retVal.Where(x => x.Parent == null && x.ParentId != null).Select(x => x.ParentId).ToArray(); var parents = GetItemByIds(parentIds, respGroup); return(retVal); }
public IHttpActionResult GetProductsByIds([FromUri] string[] ids, [FromUri] coreModel.ItemResponseGroup responseGroup = coreModel.ItemResponseGroup.ItemInfo) { var retVal = InnerGetProductsByIds(ids, responseGroup); return(Ok(retVal)); }
public IHttpActionResult GetProductsByIds(string store, [FromUri] string[] ids, [FromUri] coreModel.ItemResponseGroup responseGroup = coreModel.ItemResponseGroup.ItemInfo) { var fullLoadedStore = GetStoreById(store); if (fullLoadedStore == null) { throw new NullReferenceException(store + " not found"); } var retVal = InnerGetProductsByIds(fullLoadedStore, ids, responseGroup); return(Ok(retVal)); }
public IHttpActionResult Search(string store, string[] priceLists, [ModelBinder(typeof(SearchParametersBinder))] SearchParameters parameters, [FromUri] coreModel.ItemResponseGroup responseGroup = coreModel.ItemResponseGroup.ItemMedium, [FromUri] string outline = "", string language = "en-us", string currency = "USD") { var context = new Dictionary <string, object> { { "StoreId", store }, }; var fullLoadedStore = GetStoreById(store); if (fullLoadedStore == null) { throw new NullReferenceException(store + " not found"); } var catalog = fullLoadedStore.Catalog; string categoryId = null; var criteria = new CatalogIndexedSearchCriteria { Locale = language, Catalog = catalog.ToLowerInvariant() }; if (!string.IsNullOrWhiteSpace(outline)) { criteria.Outlines.Add(String.Format("{0}/{1}*", catalog, outline)); categoryId = outline.Split(new[] { '/' }).Last(); context.Add("CategoryId", categoryId); } #region Filters // Now fill in filters var filters = _browseFilterService.GetFilters(context); // Add all filters foreach (var filter in filters) { criteria.Add(filter); } // apply terms if (parameters.Terms != null && parameters.Terms.Count > 0) { foreach (var term in parameters.Terms) { var filter = filters.SingleOrDefault(x => x.Key.Equals(term.Key, StringComparison.OrdinalIgnoreCase) && (!(x is PriceRangeFilter) || ((PriceRangeFilter)x).Currency.Equals(currency, StringComparison.OrdinalIgnoreCase))); var appliedFilter = _browseFilterService.Convert(filter, term.Value); criteria.Apply(appliedFilter); } } #endregion #region Facets // apply facet filters var facets = parameters.Facets; if (facets.Count != 0) { foreach (var key in facets.Select(f => f.Key)) { var filter = filters.SingleOrDefault( x => x.Key.Equals(key, StringComparison.OrdinalIgnoreCase) && (!(x is PriceRangeFilter) || ((PriceRangeFilter)x).Currency.Equals(currency, StringComparison.OrdinalIgnoreCase))); var appliedFilter = _browseFilterService.Convert(filter, facets.FirstOrDefault(f => f.Key == key).Value); criteria.Apply(appliedFilter); } } #endregion //criteria.ClassTypes.Add("Product"); criteria.RecordsToRetrieve = parameters.PageSize == 0 ? 10 : parameters.PageSize; criteria.StartingRecord = parameters.StartingRecord; criteria.Pricelists = priceLists; criteria.Currency = currency; criteria.StartDateFrom = parameters.StartDateFrom; criteria.SearchPhrase = parameters.FreeSearch; #region sorting if (!string.IsNullOrEmpty(parameters.Sort)) { var isDescending = "desc".Equals(parameters.SortOrder, StringComparison.OrdinalIgnoreCase); SearchSort sortObject = null; switch (parameters.Sort.ToLowerInvariant()) { case "price": if (criteria.Pricelists != null) { sortObject = new SearchSort( criteria.Pricelists.Select( priceList => new SearchSortField(String.Format("price_{0}_{1}", criteria.Currency.ToLower(), priceList.ToLower())) { IgnoredUnmapped = true, IsDescending = isDescending, DataType = SearchSortField.DOUBLE }) .ToArray()); } break; case "position": sortObject = new SearchSort( new SearchSortField(string.Format("sort{0}{1}", catalog, categoryId).ToLower()) { IgnoredUnmapped = true, IsDescending = isDescending }); break; case "name": sortObject = new SearchSort("name", isDescending); break; case "rating": sortObject = new SearchSort(criteria.ReviewsAverageField, isDescending); break; case "reviews": sortObject = new SearchSort(criteria.ReviewsTotalField, isDescending); break; default: sortObject = CatalogIndexedSearchCriteria.DefaultSortOrder; break; } criteria.Sort = sortObject; } #endregion //Load ALL products var searchResults = _browseService.SearchItems(criteria, responseGroup); // populate inventory if ((responseGroup & ItemResponseGroup.ItemProperties) == ItemResponseGroup.ItemProperties) { PopulateInventory(fullLoadedStore.FulfillmentCenter, searchResults.Items); } return(this.Ok(searchResults)); }
public IHttpActionResult GetProductByKeyword(string store, [FromUri] string keyword, [FromUri] coreModel.ItemResponseGroup responseGroup = coreModel.ItemResponseGroup.ItemLarge, string language = "en-us") { var fullLoadedStore = GetStoreById(store); if (fullLoadedStore == null) { throw new NullReferenceException(store + " not found"); } var searchCriteria = new SearchCriteria { ResponseGroup = ResponseGroup.WithProducts | ResponseGroup.WithVariations, SeoKeyword = keyword, //CatalogId = fullLoadedStore.Catalog }; var result = _searchService.Search(searchCriteria); if (result.Products != null && result.Products.Any()) { var products = InnerGetProductsByIds(fullLoadedStore, new [] { result.Products.First().Id }, responseGroup); var retVal = products.FirstOrDefault(); if (retVal != null) { return(Ok(retVal)); } } return(NotFound()); }
public IHttpActionResult GetProductByPlentyIds([FromBody] string[] ids, [FromUri] coreModel.ItemResponseGroup respGroup = coreModel.ItemResponseGroup.ItemLarge) { return(GetProductByIds(ids, respGroup)); }
public IHttpActionResult GetProductByCode(string store, [FromUri] string code, [FromUri] coreModel.ItemResponseGroup responseGroup = coreModel.ItemResponseGroup.ItemLarge, string language = "en-us") { var searchCriteria = new SearchCriteria { ResponseGroup = ResponseGroup.WithProducts | ResponseGroup.WithVariations, Code = code, }; var result = _searchService.Search(searchCriteria); if (result.Products != null && result.Products.Any()) { var products = InnerGetProductsByIds(new string[] { result.Products.First().Id }, responseGroup); var retVal = products.FirstOrDefault(); if (retVal != null) { return(Ok(retVal)); } } return(NotFound()); }
public moduleModel.SearchResult SearchItems(CatalogIndexedSearchCriteria criteria, moduleModel.ItemResponseGroup responseGroup) { CatalogItemSearchResults results; var items = Search(criteria, out results, responseGroup); var response = new moduleModel.SearchResult(); response.Products.AddRange(items); response.ProductsTotalCount = results.TotalCount; // TODO need better way to find applied filter values var appliedFilters = criteria.CurrentFilters.SelectMany(x => x.GetValues()).Select(x => x.Id).ToArray(); if (results.FacetGroups != null) { response.Aggregations = results.FacetGroups.Select(g => g.ToModuleModel(appliedFilters)).ToArray(); } return(response); }
public ProductSearchResult SearchItems(CatalogIndexedSearchCriteria criteria, moduleModel.ItemResponseGroup responseGroup = moduleModel.ItemResponseGroup.ItemSmall) { CatalogItemSearchResults results; var items = Search(criteria, out results, responseGroup); var catalogItems = new List <Product>(); // go through items foreach (var item in items) { var searchTags = results.Items[item.Id.ToLower()]; var currentOutline = this.GetItemOutlineUsingContext( searchTags[criteria.OutlineField].ToString(), criteria.Catalog); var catalogItem = item.ToWebModel(_blobUrlResolver) as Product; catalogItem.Outline = this.StripCatalogFromOutline(currentOutline, criteria.Catalog); int reviewTotal; if (searchTags.ContainsKey(criteria.ReviewsTotalField) && int.TryParse(searchTags[criteria.ReviewsTotalField].ToString(), out reviewTotal)) { catalogItem.ReviewsTotal = reviewTotal; } double reviewAvg; if (searchTags.ContainsKey(criteria.ReviewsAverageField) && double.TryParse(searchTags[criteria.ReviewsAverageField].ToString(), out reviewAvg)) { catalogItem.Rating = reviewAvg; } catalogItems.Add(catalogItem); } var response = new ProductSearchResult(); response.Items.AddRange(catalogItems); response.TotalCount = results.TotalCount; //TODO need better way to find applied filter values var appliedFilters = criteria.CurrentFilters.SelectMany(x => x.GetValues()).Select(x => x.Id).ToArray(); response.Facets = results.FacetGroups == null ? null : results.FacetGroups.Select(g => g.ToWebModel(appliedFilters)).ToArray(); return(response); }