Пример #1
0
            public void WhenProductAdded_ThenItCanBeReturned()
            {
                var sut          = new ProductsRepository();
                var addedProduct = new ParentProduct(3, 5);

                sut.AddProduct(addedProduct);

                var retrievedProduct = sut.GetProduct(3);

                Assert.Equal(addedProduct, retrievedProduct);
            }
Пример #2
0
        public void Apply()
        {
            Product product;

            if (!_parentId.HasValue)
            {
                product = new ParentProduct(Id, _stock);
            }
            else
            {
                var parent = (ParentProduct)ProductsRepository.GetProduct(_parentId.Value);
                if (parent == null)
                {
                    throw new ArgumentException($"Unexisting parentId {_parentId}");
                }
                else
                {
                    product = new ChildProduct(Id, _stock, parent);
                    parent.Children.Add((ChildProduct)product);
                }
            }
            ProductsRepository.AddProduct(product);
        }
Пример #3
0
        public static void LoadProducts(string productFile)
        {
            // Check for required loaded data
            if (IData.Categories == null)
            {
                throw new Exception("Categories Not Ready for Products");
            }
            if (IData.Options == null)
            {
                throw new Exception("Options Not Ready for Products");
            }
            if (IData.Vendors == null)
            {
                throw new Exception("Vendors Not Ready for Products");
            }

            // Initialize Products
            if (IData.Products == null)
            {
                IData.Products = new List <ParentProduct>();
            }

            // The raw data from the CSV file
            List <Dictionary <string, string> > theList = ProcessData(productFile);
            ParentProduct productItem;

            // process the list
            foreach (var item in theList)
            {
                string id = "";
                string value;

                // Product Code [string] productcode
                if (item.TryGetValue("productcode", out value))
                {
                    id = value;
                }
                if (id != "")
                {
                    productItem = new ParentProduct(value);

                    // List of Category IDs assigned to the shoe [List<Category>] categoryids
                    if (item.TryGetValue("categoryids", out value))
                    {
                        var             catids         = value.Split(',');
                        List <Category> itemCategories = new List <Category>();
                        foreach (var catid in catids)
                        {
                            int cid;
                            if (int.TryParse(catid, out cid))
                            {
                                var cat = IData.Categories.Find(x => x.ID == cid);
                                if (cat != null)
                                {
                                    itemCategories.Add(cat);
                                }
                            }
                        }
                        productItem.Categories = itemCategories;
                    }

                    // List of Option IDs assigned to the shoe [List<Option>] optionids
                    if (item.TryGetValue("optionids", out value))
                    {
                        var           optids      = value.Split(',');
                        List <Option> itemOptions = new List <Option>();
                        foreach (var optid in optids)
                        {
                            int oid;
                            if (int.TryParse(optid, out oid))
                            {
                                var opt = IData.Options.Find(x => x.ID == oid);
                                if (opt != null)
                                {
                                    itemOptions.Add(opt);
                                }
                            }
                        }
                        productItem.Options = itemOptions;
                    }

                    // Product Name [string] productname
                    if (item.TryGetValue("productname", out value))
                    {
                        productItem.Name = value;
                    }

                    // Date product first displayed [DateTime] displaybegindate
                    if (item.TryGetValue("displaybegindate", out value))
                    {
                        DateTime beginDate;
                        if (DateTime.TryParse(value, out beginDate))
                        {
                            productItem.DisplayBeginDate = beginDate;
                        }
                    }

                    // Is the product taxable [bool] taxableproduct
                    if (item.TryGetValue("taxableproduct", out value))
                    {
                        if (value != "")
                        {
                            productItem.Taxable = value.Equals("Y");
                        }
                    }

                    // Is the product hidden [bool] hideproduct
                    if (item.TryGetValue("hideproduct", out value))
                    {
                        if (value != "")
                        {
                            productItem.Hidden = value.Equals("Y");
                        }
                    }

                    // Shortened version of the product name [string] productnameshort
                    if (item.TryGetValue("productnameshort", out value))
                    {
                        productItem.ShortName = value;
                    }

                    // Short statement of product (mens / womens / accessories) [string] productdescriptionshort
                    if (item.TryGetValue("productdescriptionshort", out value))
                    {
                        productItem.ShortDescription = value;
                    }

                    // SEO Title [string] metatag_title
                    if (item.TryGetValue("metatag_title", out value))
                    {
                        productItem.MetaTagTitle = value;
                    }

                    // SEO Description [string] metatag_description
                    if (item.TryGetValue("metatag_description", out value))
                    {
                        productItem.MetaTagDescription = value;
                    }

                    // SEO Photo alternative text [string] photo_alttext
                    if (item.TryGetValue("photo_alttext", out value))
                    {
                        productItem.PhotoAltText = value;
                    }

                    // Is the product designated as a sale product [string] customfield1
                    if (item.TryGetValue("customfield1", out value))
                    {
                        productItem.SaleText = value;
                    }

                    // Manufacturer of the product [Vendor] productmanufacturer
                    if (item.TryGetValue("productmanufacturer", out value))
                    {
                        productItem.ItemVendor = value;
                        //productItem.ItemVendor = IData.Vendors.Find(x => x.Title == value);
                    }

                    // Price of product [float] ProductPrice productprice
                    if (item.TryGetValue("productprice", out value))
                    {
                        float price;
                        if (float.TryParse(value, out price))
                        {
                            productItem.ProductPrice = price;
                        }
                    }

                    // Determine if this item is a parent or a child product

                    // List of Child Products assigned to the shoe [List] ChildProducts ischildofproductcode
                    //   ---   Number of shoes in stock [int] stockstatus
                    if (item.TryGetValue("ischildofproductcode", out value))
                    {
                        if (value != "") // if there is a value within here
                        {
                            // copy the current item as a child item
                            ChildProduct childItem = new ChildProduct(productItem);

                            // Number of shoes in stock [int] stockstatus
                            string stockstatus;
                            if (item.TryGetValue("stockstatus", out stockstatus))
                            {
                                int status;
                                if (int.TryParse(stockstatus, out status))
                                {
                                    childItem.StockStatus = status;
                                }
                            }
                            var parentIndex = IData.Products.FindIndex(x => x.ID == value);
                            if (parentIndex != -1)
                            {
                                IData.Products[parentIndex].ChildProducts.Add(childItem);
                                continue;
                            }
                        }
                    }

                    // HTML of product description [string] productdescription
                    if (item.TryGetValue("productdescription", out value))
                    {
                        productItem.Description = value;
                    }
                    // Determine if seen on home page [bool?] homepage_section
                    if (item.TryGetValue("homepage_section", out value))
                    {
                        if (value != "")
                        {
                            productItem.OnHomePage = value.Equals("Y");
                        }
                    }
                    // Keywords for SEO [string] metatag_keywords
                    if (item.TryGetValue("metatag_keywords", out value))
                    {
                        productItem.MetaTagKeywords = value;
                    }

                    // each item must be unique
                    var productIndex = IData.Products.FindIndex(x => x.ID == productItem.ID);
                    if (productIndex == -1)
                    {
                        IData.Products.Add(productItem);
                    }
                    else
                    {
                        IData.Products[productIndex] = productItem;
                    }
                }
            }
        }