Exemplo n.º 1
0
            private static EntityDataServiceResponse <ProductDimension> GetProductDimensions(GetProductDimensionsDataRequest request)
            {
                var query = new SqlPagedQuery(request.QueryResultSettings)
                {
                    Distinct = true,  // Need to perform distinct to avoid retrieving a row for every variant type product combination.
                    Aliased  = true,
                    From     = ProductDimensionsViewName
                };

                using (DatabaseContext databaseContext = new DatabaseContext(request.RequestContext))
                    using (RecordIdTableType productIds = new RecordIdTableType(request.ProductIds, ProductIdVariableName))
                    {
                        query.Parameters[ProductIdsVariableName] = productIds;

                        return(new EntityDataServiceResponse <ProductDimension>(databaseContext.ReadEntity <ProductDimension>(query)));
                    }
            }
Exemplo n.º 2
0
            private static PagedResult <SimpleProduct> PopulateComplexProductProperties(long channelId, PagedResult <SimpleProduct> products, RequestContext context, SearchLocation searchLocation, bool?downloadedProductsFilter, bool calculatePrices)
            {
                // Retrieving all id collections needed to query the Data Service for complex properties.
                var productIds               = products.Results.Select(p => p.RecordId);
                var masterTypeProductIds     = products.Results.Where(p => p.ProductType == ProductType.Master).Select(p => p.RecordId);
                var kitVariantTypeProductIds = products.Results.Where(p => p.ProductType == ProductType.KitVariant).Select(v => v.RecordId);
                var kitMasterTypeProductIds  = products.Results.Where(p => p.ProductType == ProductType.KitMaster).Select(v => v.RecordId);
                var variantTypeProductIds    = products.Results.Where(p => p.ProductType == ProductType.Variant).Select(v => v.RecordId);

                // Products of types Master and KitMaster have dimensions that need to be retrieved.
                var idsOfProductsContainingDimensions = masterTypeProductIds.Concat(kitMasterTypeProductIds);

                IEnumerable <ProductComponent>      components        = new List <ProductComponent>();
                IEnumerable <ProductDimension>      productDimensions = new List <ProductDimension>();
                IEnumerable <ProductDimensionValue> dimensionValues   = new List <ProductDimensionValue>();

                // Products of type KitVariant have components that need to be retrieved.
                if (kitVariantTypeProductIds.Any())
                {
                    var getProductComponentsDataRequestColummnSet = ProductComponent.DefaultColumnSet;
                    getProductComponentsDataRequestColummnSet.Add("VARIANTPRODUCTID");
                    var getProductComponentsDataRequestSettings = new QueryResultSettings(getProductComponentsDataRequestColummnSet, PagingInfo.AllRecords);
                    var getProductComponentsDataRequest         = new GetProductComponentsForVariantProductsDataRequest(kitVariantTypeProductIds, getProductComponentsDataRequestSettings, downloadedProductsFilter);
                    components = context.Execute <EntityDataServiceResponse <ProductComponent> >(getProductComponentsDataRequest).PagedEntityCollection.Results;
                }

                if (idsOfProductsContainingDimensions.Any())
                {
                    var getDimensionsDataRequest = new GetProductDimensionsDataRequest(idsOfProductsContainingDimensions, QueryResultSettings.AllRecords);
                    productDimensions = context.Execute <EntityDataServiceResponse <ProductDimension> >(getDimensionsDataRequest).PagedEntityCollection.Results;
                }

                // Products of types Variant and KitVariant have dimension values that need to be retrieved.
                // This collection is populated after retrieving components so that dimension values of Variant type products can also be retrieved in the same transaction.
                var idsOfProductsContainingDimensionValues = variantTypeProductIds.Concat(kitVariantTypeProductIds).Concat(components.Where(c => c.ProductType == ProductType.Variant).Select(c => c.ProductId).Distinct());

                if (idsOfProductsContainingDimensionValues.Any())
                {
                    var getDimensionValuesDataRequest = new GetProductDimensionValuesForVariantProductsDataRequest(idsOfProductsContainingDimensionValues, QueryResultSettings.AllRecords, downloadedProductsFilter);
                    dimensionValues = context.Execute <EntityDataServiceResponse <ProductDimensionValue> >(getDimensionValuesDataRequest).PagedEntityCollection.Results;
                }

                var productIdsToFetchBehavior   = productIds.Concat(components.Select(c => c.ProductId).Distinct());
                var productBehaviorSettings     = new QueryResultSettings(ProductBehavior.DefaultColumnSet, PagingInfo.CreateWithExactCount(productIdsToFetchBehavior.Count(), skip: 0));
                var productsBehaviorDataRequest = new GetProductBehaviorDataRequest(productIdsToFetchBehavior, productBehaviorSettings, downloadedProductsFilter);
                PagedResult <ProductBehavior> productsBehavior = context.Execute <EntityDataServiceResponse <ProductBehavior> >(productsBehaviorDataRequest).PagedEntityCollection;

                var getLinkedProductRelationsDataRequest = new GetLinkedProductRelationsDataRequest(productIds, QueryResultSettings.AllRecords, downloadedProductsFilter);
                PagedResult <LinkedProductRelation> linkedProductRelations = context.Execute <EntityDataServiceResponse <LinkedProductRelation> >(getLinkedProductRelationsDataRequest).PagedEntityCollection;

                var linkedProductIds             = linkedProductRelations.Results.Select(r => r.LinkedProductId).Distinct();
                var getLinkedProductsDataRequest = new GetProductsServiceRequest(channelId, linkedProductIds, QueryResultSettings.AllRecords);

                getLinkedProductsDataRequest.SearchLocation = searchLocation;
                PagedResult <SimpleProduct> linkedProducts = context.Execute <GetProductsServiceResponse>(getLinkedProductsDataRequest).Products;

                PagedResult <ProductPrice> productPrices = PagedResult <ProductPrice> .Empty();

                if (calculatePrices)
                {
                    // Pricing APIs currently take Product instead of SimpleProduct. We manually build a Product object
                    // and populate the required field in the interim until uptake of SimpleProduct in pricing service.
                    List <Product> productsTransformedForPricing = ConvertSimpleProductsToProducts(products.Results).ToList();

                    var priceRequest = new GetProductPricesServiceRequest(productsTransformedForPricing);
                    productPrices = context.Execute <GetProductPricesServiceResponse>(priceRequest).ProductPrices;
                }

                return(InsertProductPropertiesIntoProduct(products, productsBehavior, productPrices, linkedProductRelations, linkedProducts, components, productDimensions, dimensionValues));
            }