Exemplo n.º 1
0
        public async Task ProcessSampleProducts(string filename, ILogger logger)
        {
            logger.LogInformation("Processing Sample Products");
            List <PriceSchedule>         prices                 = new List <PriceSchedule>();
            List <Product>               products               = new List <Product>();
            List <Spec>                  specs                  = new List <Spec>();
            List <SpecOption>            specOptions            = new List <SpecOption>();
            List <SpecProductAssignment> specProductAssignments = new List <SpecProductAssignment>();
            List <VariantPlaceholder>    variantPlaceholders    = new List <VariantPlaceholder>();


            // Map Customer Provided Product Data to OrderCloud Headstart Classes
            var binDirectory  = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var rootDirectory = Path.GetFullPath(Path.Combine(binDirectory, ".."));
            SampleProductList productsFromFile = JsonConvert.DeserializeObject <SampleProductList>(File.ReadAllText(rootDirectory + filename));

            foreach (SampleProduct product in productsFromFile.products)
            {
                // Build list of Price Schedules
                var priceSchedule = ProductMapping.MapOCPriceSchedule(product);
                prices.Add(priceSchedule);

                // Build list of Products
                var tempProduct = ProductMapping.MapOCProduct(product);
                products.Add(tempProduct);

                // Build Specs if more than 1 product in group
                if (product.options.Count > 0)
                {
                    foreach (SampleOption option in product.options)
                    {
                        var tempSpec = ProductMapping.MapOCProductSpec(option);
                        specs.Add(tempSpec);

                        foreach (string val in option.values)
                        {
                            var tempOption = ProductMapping.MapOCProductSpecOption(product.id, option.name, val);
                            specOptions.Add(tempOption);

                            // The sample data provided will generate a simple list of variants based solely on color options.
                            // Real world products will require more robust data mapping for variants if the need to modify them exists.
                            var tempVariant = ProductMapping.MapOCProductVariant(product, option, val);
                            variantPlaceholders.Add(tempVariant);
                        }

                        var tempSPA = ProductMapping.MapOCProductSpecAssignment(product.id, option.name);
                        specProductAssignments.Add(tempSPA);
                    }
                }
            }

            // Build PriceShcedule
            await Throttler.RunAsync(prices, 100, 20, price => BuildPriceScheduleOC(price, logger));

            // Build Product
            await Throttler.RunAsync(products, 100, 20, product => BuildProductOC(product, logger));

            if (specs.Count > 0)
            {
                // Build Specs
                await Throttler.RunAsync(specs, 100, 20, spec => BuildSpecOC(spec, logger));

                // Build Spec Options
                await Throttler.RunAsync(specOptions, 100, 20, specoption => BuildSpecOptionOC(specoption, logger));

                // Assign Specs to Product
                await Throttler.RunAsync(specProductAssignments, 100, 20, specprodassignment => BuildSpecProductAssignmentOC(specprodassignment, logger));

                // Generate Variants
                var variantGroups = variantPlaceholders.GroupBy(v => v.ProductID);
                foreach (IList <VariantPlaceholder> variantGroup in variantGroups)
                {
                    // Allow the system to generate variants based on selection specs
                    await GenerateOCVariants(variantGroup[0].ProductID, logger);

                    //Modify the generated specs to use custom variant id's
                    await Throttler.RunAsync(variantGroup, 500, 1, variant => UpdateProductVariantsOC(variant, logger));
                }
            }
            logger.LogInformation($"Process BrandwearProducts Complete.");
        }