public CatalogData( IApplicationConfiguration configuration, Catalog catalog, Product[] products) { // set the root category Categories = new Category { Key = new string[] {"catalog"}, Slug = "catalog", Name = "Catalog", Products = new List<CatalogProduct>(products.Length), }; // convert products Products = new Dictionary<string, CatalogProduct>(products.Length); foreach (var p in products) { // find/build the category var category = Categories; for (int i = 1; i < p.Category.Length; i++) { var slug = p.Category[i].ToSlug(); if (null == category.Children) { category.Children = new Dictionary<string, Category>(); } if (!category.Children.ContainsKey(slug)) { category.Children[slug] = new Category { Key = category.Key.Concat(new[] {slug}).ToArray(), Name = p.Category[i], Parent = category, Slug = slug, Products = new List<CatalogProduct>(100), }; } category = category.Children[slug]; } // tranform the product var x = new CatalogProduct { Catalog = catalog, Id = p.ProductId, Name = p.Name, Sku = p.Sku, Manufacturer = p.Manufacturer, Brand = p.Brand, Model = p.Model, Description = p.Description, Features = p.Features, Warranty = p.Warranty, CountryOfOrigin = p.CountryOfOrigin, Category = category, Tags = p.Tags, Price = (int)Math.Ceiling(catalog.GetPrice(p) * configuration.PointsPerDollar), Options = null == p.Options ? new List<CatalogProductOption>() : p.Options.Select(i => { var source = i.GetBestSource(); var price = null == source ? null : source.Pricing; return new CatalogProductOption { Sku = i.Sku, Name = i.Name, Price = price == null ? null : (int?)Math.Ceiling(configuration.PointsPerDollar * catalog.GetPrice(p, i.Name)), }; }).ToList(), }; Products[x.Id] = x; // add to various categories do { category.Products.Add(x); category = category.Parent; } while (category != null); } }
public CatalogData( IApplicationConfiguration configuration, Catalog catalog, Product[] products) { // set the root category Categories = new Category { Key = new string[] { "catalog" }, Slug = "catalog", Name = "Catalog", Products = new List <CatalogProduct>(products.Length), }; // convert products Products = new Dictionary <string, CatalogProduct>(products.Length); foreach (var p in products) { // find/build the category var category = Categories; for (int i = 1; i < p.Category.Length; i++) { var slug = p.Category[i].ToSlug(); if (null == category.Children) { category.Children = new Dictionary <string, Category>(); } if (!category.Children.ContainsKey(slug)) { category.Children[slug] = new Category { Key = category.Key.Concat(new[] { slug }).ToArray(), Name = p.Category[i], Parent = category, Slug = slug, Products = new List <CatalogProduct>(100), }; } category = category.Children[slug]; } // tranform the product var x = new CatalogProduct { Catalog = catalog, Id = p.ProductId, Name = p.Name, Sku = p.Sku, Manufacturer = p.Manufacturer, Brand = p.Brand, Model = p.Model, Description = p.Description, Features = p.Features, Warranty = p.Warranty, CountryOfOrigin = p.CountryOfOrigin, Category = category, Tags = p.Tags, Price = (int)Math.Ceiling(catalog.GetPrice(p) * configuration.PointsPerDollar), Options = null == p.Options ? new List <CatalogProductOption>() : p.Options.Select(i => { var source = i.GetBestSource(); var price = null == source ? null : source.Pricing; return(new CatalogProductOption { Sku = i.Sku, Name = i.Name, Price = price == null ? null : (int?)Math.Ceiling(configuration.PointsPerDollar * catalog.GetPrice(p, i.Name)), }); }).ToList(), }; Products[x.Id] = x; // add to various categories do { category.Products.Add(x); category = category.Parent; } while (category != null); } }
public bool Contains(CatalogProduct product) { return product.Category == this || ( Key.Length < product.Category.Key.Length && Key.Take(product.Category.Key.Length).SequenceEqual(product.Category.Key) ); }