public void Can_Get_A_ProductVariant_From_GetVariantForPurchase_Method_Given_A_List_Of_Attributes() { //// Arrange var att = new ProductAttribute("Choice1", "choice") { Key = Guid.NewGuid() }; var attCollection = new ProductAttributeCollection(); attCollection.Add(att); var expected = new ProductVariant(Guid.NewGuid(), attCollection, new CatalogInventoryCollection(), false, false, "Variant", "variant", 5M); _product.ProductOptions.Add(new ProductOption("Option1") { Key = Guid.NewGuid() }); _product.ProductOptions.First(x => x.Name == "Option1").Choices.Add(att); _product.ProductVariants.Add(expected); //// Act var variant = _product.GetProductVariantForPurchase(attCollection); //// Assert Assert.NotNull(variant); Assert.AreEqual(expected.Key, variant.Key); }
/// <summary> /// Creates a collection of <see cref="IProductVariant"/> that can be created based on unmapped product options. /// </summary> /// <param name="product">The <see cref="IProduct"/></param> /// <returns>A collection of <see cref="IProductVariant"/></returns> /// <remarks> /// /// This is an expensive method due to the potential number of database calls and should probably /// be refactored /// /// </remarks> public IEnumerable <IProductVariant> GetProductVariantsThatCanBeCreated(IProduct product) { var variants = new List <IProductVariant>(); if (!product.ProductOptions.Any()) { return(variants); } var possibleCombinations = GetPossibleProductAttributeCombinations(product); foreach (var combo in possibleCombinations) { var attributes = new ProductAttributeCollection(); foreach (var att in combo) { attributes.Add(att); } if (!ProductVariantWithAttributesExists(product, attributes)) { variants.Add(CreateProductVariant(product, attributes)); } } return(variants); }
public void Can_Serialize_A_Product_To_Xml() { //// Arrange var att = new ProductAttribute("Choice1", "choice") { Key = Guid.NewGuid() }; var attCollection = new ProductAttributeCollection(); attCollection.Add(att); var expected = new ProductVariant(Guid.NewGuid(), attCollection, new CatalogInventoryCollection(), false, false, "Variant", "variant", 5M); _product.ProductOptions.Add(new ProductOption("Option1") { Key = Guid.NewGuid() }); _product.ProductOptions.First(x => x.Name == "Option1").Choices.Add(att); _product.ProductVariants.Add(expected); //// Act var xml = _product.SerializeToXml(); Console.Write(xml.ToString()); //// Assert Assert.NotNull(xml); }
/// <summary> /// Converts an enumeration of ProductAttributes to a ProductAttributecollection /// </summary> /// <param name="attributes"> /// The attributes. /// </param> /// <returns> /// The <see cref="ProductAttributeCollection"/>. /// </returns> internal static ProductAttributeCollection ToProductAttributeCollection(this IEnumerable <IProductAttribute> attributes) { var collection = new ProductAttributeCollection(); foreach (var att in attributes) { collection.Add(att); } return(collection); }
private static ProductAttributeCollection DBMapping(DBProductAttributeCollection dbCollection) { if (dbCollection == null) { return(null); } ProductAttributeCollection collection = new ProductAttributeCollection(); foreach (DBProductAttribute dbItem in dbCollection) { ProductAttribute item = DBMapping(dbItem); collection.Add(item); } return(collection); }
public ProductVariantDisplay NewProductVariant(ProductVariantDisplay productVariant) { IProductVariant newProductVariant; try { var product = _productService.GetByKey(productVariant.ProductKey) as Product; if (product != null) { var productAttributes = new ProductAttributeCollection(); foreach (var attribute in productVariant.Attributes) { // TODO: This should be refactored into an extension method var productOption = product.ProductOptions.FirstOrDefault(x => x.Key == attribute.OptionKey) as ProductOption; if (productOption != null) { productAttributes.Add(productOption.Choices[attribute.Key]); } } newProductVariant = _productVariantService.CreateProductVariantWithKey(product, productAttributes); if (productVariant.TrackInventory) { newProductVariant.AddToCatalogInventory(_warehouseService.GetDefaultWarehouse().DefaultCatalog()); } newProductVariant = productVariant.ToProductVariant(newProductVariant); _productVariantService.Save(newProductVariant); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError)); } } catch (Exception e) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError)); } return(newProductVariant.ToProductVariantDisplay()); }
public void Can_Get_A_ProductVariant_From_GetVariantForPurchase_Method_Given_A_List_Of_Attributes() { //// Arrange var att = new ProductAttribute("Choice1", "choice") { Key = Guid.NewGuid() }; var attCollection = new ProductAttributeCollection(); attCollection.Add(att); var expected = new ProductVariant(Guid.NewGuid(), attCollection, new CatalogInventoryCollection(), false, "Variant", "variant", 5M); _product.ProductOptions.Add(new ProductOption("Option1") { Key = Guid.NewGuid() }); _product.ProductOptions.First(x => x.Name == "Option1").Choices.Add(att); _product.ProductVariants.Add(expected); //// Act var variant = _product.GetProductVariantForPurchase(attCollection); //// Assert Assert.NotNull(variant); Assert.AreEqual(expected.Key, variant.Key); }
private ProductAttributeCollection GetProductAttributeCollection(Guid optionKey) { var sql = new Sql(); sql.Select("*") .From <ProductAttributeDto>() .Where <ProductAttributeDto>(x => x.OptionKey == optionKey); var dtos = Database.Fetch <ProductAttributeDto>(sql); var attributes = new ProductAttributeCollection(); var factory = new ProductAttributeFactory(); foreach (var dto in dtos) { var attribute = factory.BuildEntity(dto); attributes.Add(attribute); } return(attributes); }
///// <summary> ///// Deletes all detached content for culture. ///// </summary> ///// <param name="cultureName"> ///// The culture name. ///// </param> //public void DeleteAllDetachedContentForCulture(string cultureName) //{ // Database.Execute( // "DELETE FROM merchProductVariantDetachedContent WHERE cultureName = @Cn", // new { @Cn = cultureName }); //} /// <summary> /// The get product attribute collection. /// </summary> /// <param name="productVariantKey"> /// The product variant key. /// </param> /// <returns> /// The <see cref="ProductAttributeCollection"/>. /// </returns> internal ProductAttributeCollection GetProductAttributeCollection(Guid productVariantKey) { var sql = new Sql(); sql.Select("*") .From <ProductVariant2ProductAttributeDto>() .InnerJoin <ProductAttributeDto>() .On <ProductVariant2ProductAttributeDto, ProductAttributeDto>(left => left.ProductAttributeKey, right => right.Key) .Where <ProductVariant2ProductAttributeDto>(x => x.ProductVariantKey == productVariantKey); var dtos = Database.Fetch <ProductVariant2ProductAttributeDto, ProductAttributeDto>(sql); var factory = new ProductAttributeFactory(); var collection = new ProductAttributeCollection(); foreach (var dto in dtos) { collection.Add(factory.BuildEntity(dto.ProductAttributeDto)); } return(collection); }
/// <summary> /// Creates a collection of <see cref="IProductVariant"/> that can be created based on unmapped product options. /// </summary> /// <param name="product">The <see cref="IProduct"/></param> /// <returns>A collection of <see cref="IProductVariant"/></returns> /// <remarks> /// /// This is an expensive method due to the potential number of database calls and should probably /// be refactored /// /// </remarks> public IEnumerable<IProductVariant> GetProductVariantsThatCanBeCreated(IProduct product) { var variants = new List<IProductVariant>(); if (!product.ProductOptions.Any()) return variants; var possibleCombinations = GetPossibleProductAttributeCombinations(product); foreach (var combo in possibleCombinations) { var attributes = new ProductAttributeCollection(); foreach(var att in combo) attributes.Add(att); if (!ProductVariantWithAttributesExists(product, attributes)) variants.Add(CreateProductVariant(product, attributes)); } return variants; }
internal static IProductVariant ToProductVariant(this ProductVariantDisplay productVariantDisplay, IProductVariant destination) { if (productVariantDisplay.Key != Guid.Empty) { destination.Key = productVariantDisplay.Key; } if (!String.IsNullOrEmpty(productVariantDisplay.Name)) { destination.Name = productVariantDisplay.Name; } if (!String.IsNullOrEmpty(productVariantDisplay.Sku)) { destination.Sku = productVariantDisplay.Sku; } destination.Price = productVariantDisplay.Price; destination.CostOfGoods = productVariantDisplay.CostOfGoods; destination.SalePrice = productVariantDisplay.SalePrice; destination.OnSale = productVariantDisplay.OnSale; destination.Manufacturer = productVariantDisplay.Manufacturer; destination.ManufacturerModelNumber = productVariantDisplay.ManufacturerModelNumber; destination.Weight = productVariantDisplay.Weight; destination.Length = productVariantDisplay.Length; destination.Width = productVariantDisplay.Width; destination.Height = productVariantDisplay.Height; destination.Barcode = productVariantDisplay.Barcode; destination.Available = productVariantDisplay.Available; destination.TrackInventory = productVariantDisplay.TrackInventory; destination.OutOfStockPurchase = productVariantDisplay.OutOfStockPurchase; destination.Taxable = productVariantDisplay.Taxable; destination.Shippable = productVariantDisplay.Shippable; destination.Download = productVariantDisplay.Download; destination.DownloadMediaId = productVariantDisplay.DownloadMediaId; destination.ProductKey = productVariantDisplay.ProductKey; // We need to refactor the CatalogInventories to not be immutable if we are // going to need to do operations like this. In the UI, the user "unchecks" a catalog that was // previously checked - so we need to remove it. var deletedCatalogKeys = destination.CatalogInventories.Where( x => !productVariantDisplay.CatalogInventories.Select(ci => ci.CatalogKey).Contains(x.CatalogKey)).Select(x => x.CatalogKey).ToArray(); foreach (var deletedCatalogKey in deletedCatalogKeys) { destination.RemoveFromCatalogInventory(deletedCatalogKey); } foreach (var catalogInventory in productVariantDisplay.CatalogInventories) { var catInv = destination.CatalogInventories.FirstOrDefault(x => x.CatalogKey == catalogInventory.CatalogKey); if (catInv != null) { var destinationCatalogInventory = catInv; destinationCatalogInventory = catalogInventory.ToCatalogInventory(destinationCatalogInventory); } else if (!Guid.Empty.Equals(catalogInventory.CatalogKey) && destination.HasIdentity) { //// Add to a new catalog destination.AddToCatalogInventory(catalogInventory.CatalogKey); } } foreach (var attribute in productVariantDisplay.Attributes) { IProductAttribute destinationProductAttribute; var attr = destination.Attributes.FirstOrDefault(x => x.Key == attribute.Key); if (attr != null) { destinationProductAttribute = attr; destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); } else { destinationProductAttribute = new ProductAttribute(attribute.Name, attribute.Sku); destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); ProductAttributeCollection variantAttributes = destination.Attributes as ProductAttributeCollection; variantAttributes.Add(destinationProductAttribute); } } destination.AddOrUpdateDetachedContent(productVariantDisplay); return(destination); }
internal static IProductVariant ToProductVariant(this ProductVariantDisplay productVariantDisplay, IProductVariant destination) { if (productVariantDisplay.Key != Guid.Empty) { destination.Key = productVariantDisplay.Key; } if (!String.IsNullOrEmpty(productVariantDisplay.Name)) { destination.Name = productVariantDisplay.Name; } if (!String.IsNullOrEmpty(productVariantDisplay.Sku)) { destination.Sku = productVariantDisplay.Sku; } destination.Price = productVariantDisplay.Price; destination.CostOfGoods = productVariantDisplay.CostOfGoods; destination.SalePrice = productVariantDisplay.SalePrice; destination.OnSale = productVariantDisplay.OnSale; destination.Manufacturer = productVariantDisplay.Manufacturer; destination.ManufacturerModelNumber = productVariantDisplay.ManufacturerModelNumber; destination.Weight = productVariantDisplay.Weight; destination.Length = productVariantDisplay.Length; destination.Width = productVariantDisplay.Width; destination.Height = productVariantDisplay.Height; destination.Barcode = productVariantDisplay.Barcode; destination.Available = productVariantDisplay.Available; destination.TrackInventory = productVariantDisplay.TrackInventory; destination.OutOfStockPurchase = productVariantDisplay.OutOfStockPurchase; destination.Taxable = productVariantDisplay.Taxable; destination.Shippable = productVariantDisplay.Shippable; destination.Download = productVariantDisplay.Download; destination.DownloadMediaId = productVariantDisplay.DownloadMediaId; destination.ProductKey = productVariantDisplay.ProductKey; foreach (var catalogInventory in productVariantDisplay.CatalogInventories) { ICatalogInventory destinationCatalogInventory; if (destination.CatalogInventories.Count() > 0) { var catInv = destination.CatalogInventories.Where(x => x.CatalogKey == catalogInventory.CatalogKey).First(); if (catInv != null) { destinationCatalogInventory = catInv; destinationCatalogInventory = catalogInventory.ToCatalogInventory(destinationCatalogInventory); } } } foreach (var attribute in productVariantDisplay.Attributes) { IProductAttribute destinationProductAttribute; var attr = destination.Attributes.First(x => x.Key == attribute.Key); if (attr != null) { destinationProductAttribute = attr; destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); } else { destinationProductAttribute = new ProductAttribute(attribute.Name, attribute.Sku); destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); ProductAttributeCollection variantAttributes = destination.Attributes as ProductAttributeCollection; variantAttributes.Add(destinationProductAttribute); } } return(destination); }
/// <summary> /// Converts an enumeration of ProductAttributes to a ProductAttributecollection /// </summary> /// <param name="attributes"> /// The attributes. /// </param> /// <returns> /// The <see cref="ProductAttributeCollection"/>. /// </returns> internal static ProductAttributeCollection ToProductAttributeCollection(this IEnumerable<IProductAttribute> attributes) { var collection = new ProductAttributeCollection(); foreach (var att in attributes) { collection.Add(att); } return collection; }
public ProductVariantDisplay NewProductVariant(ProductVariantDisplay productVariant) { IProductVariant newProductVariant; try { var product = _productService.GetByKey(productVariant.ProductKey) as Product; if (product != null) { var productAttributes = new ProductAttributeCollection(); foreach (var attribute in productVariant.Attributes) { // TODO: This should be refactored into an extension method var productOption = product.ProductOptions.FirstOrDefault(x => x.Key == attribute.OptionKey) as ProductOption; if (productOption != null) productAttributes.Add(productOption.Choices[attribute.Key]); } newProductVariant = _productVariantService.CreateProductVariantWithKey(product, productAttributes); if (productVariant.TrackInventory) { newProductVariant.AddToCatalogInventory(_warehouseService.GetDefaultWarehouse().DefaultCatalog()); } newProductVariant = productVariant.ToProductVariant(newProductVariant); _productVariantService.Save(newProductVariant); } else { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError)); } } catch (Exception e) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError)); } return newProductVariant.ToProductVariantDisplay(); }
public void Can_Serialize_A_Product_To_Xml() { //// Arrange var att = new ProductAttribute("Choice1", "choice") { Key = Guid.NewGuid() }; var attCollection = new ProductAttributeCollection(); attCollection.Add(att); var expected = new ProductVariant(Guid.NewGuid(), attCollection, new CatalogInventoryCollection(), false, "Variant", "variant", 5M); _product.ProductOptions.Add(new ProductOption("Option1") { Key = Guid.NewGuid() }); _product.ProductOptions.First(x => x.Name == "Option1").Choices.Add(att); _product.ProductVariants.Add(expected); //// Act var xml = _product.SerializeToXml(); Console.Write(xml.ToString()); //// Assert Assert.NotNull(xml); }
/// <inheritdoc/> public IProductOption Clone() { var choices = this.Choices.Select(x => x.Clone()).OrderBy(x => x.SortOrder); var o = (ProductOption)this.MemberwiseClone(); var atts = new ProductAttributeCollection(); foreach (var c in choices) { atts.Add(c); } o.Choices = atts; o.ResetDirtyProperties(); return o; }