Exemplo n.º 1
0
        private void LoadProducts(IEnumerable<XElement> items)
        {
            int i = 0;
            foreach (var item in items)
            {
                int id = Convert.ToInt32(item.Attribute("id").Value);
                int categoryId = Convert.ToInt32(item.Element("categoryId").Value);

                var vendorName = ReadAttribute(item, "vendor");
                if (!vendorNames.ContainsKey(vendorName))
                {
                    VendorName vendor = new VendorName() { Name = vendorName };
                    vendorNames[vendorName] = vendor;
                    dataContext.VendorNames.Add(vendor);
                }

                var vendorCode = ReadAttribute(item, "vendorCode");
                if (!vendors.ContainsKey(vendorCode))
                {
                    Vendor vendor = new Vendor() { VendorCode = vendorCode, VendorName = vendorNames[vendorName] };
                    vendors[vendorCode] = vendor;
                    dataContext.Vendors.Add(vendor);
                }

                var listPicture = new List<Picture>();
                foreach (XElement pic in item.Elements("picture"))
                {
                    Picture picture = new Picture
                    {
                        PictureUrl = pic.Value,
                        ProductId = id
                    };
                    dataContext.Pictures.Add(picture);
                    listPicture.Add(picture);
                }

                var product = new Product()
                {
                    ProductId = id,
                    Barcode = item.GetStringValue("barcode"),
                    CategoryId = categoryId,
                    Currency = item.GetEntity("currencyId", currencies),
                    Description = item.GetStringValue("description"),
                    Discount = 0.0,
                    Name = item.GetStringValue("name"),
                    Price = Convert.ToDouble(item.Attribute("price")),
                    Url = item.GetStringValue("url"),
                    Vendor = vendors[vendorCode],
                    Picture = listPicture
                };
                dataContext.Products.Add(product);
                dataContext.SaveChanges();

                foreach (XElement element in item.Elements("param"))
                {
                    var paramName = element.Attribute("name").Value;
                    if (!paramNames.ContainsKey(paramName))
                    {
                        ParamName param = new ParamName() { Name = paramName };
                        dataContext.ParamNames.Add(param);
                        dataContext.SaveChanges();
                        paramNames[paramName] = dataContext.ParamNames.First(x => x.Name == paramName);
                    }
                    var paramValue = element.Value;
                    if (!paramValues.ContainsKey(paramValue))
                    {
                        ParamValue param = new ParamValue() { Name = paramValue };
                        dataContext.ParamValues.Add(param);
                        dataContext.SaveChanges();
                        paramValues[paramValue] = dataContext.ParamValues.First(x => x.Name == paramValue);
                    }

                    dataContext.Parameters.Add(new Parameter()
                    {
                        ParamNameId = paramValues[paramValue].ParamValueId,
                        ParamValueId = paramNames[paramName].ParamNameId,
                        ProductId = id
                    });
                }
                dataContext.SaveChanges();
                if (++i % 100 != 0)
                {
                    continue;
                }
                Console.WriteLine("Commit books: {0}", i);
            }
            dataContext.SaveChanges();
        }
Exemplo n.º 2
0
        public static void AddProduct()
        {
            using (var dbContext = new DataContext("DBShop1"))
            {
                Category cat1 = new Category() { CategoryId = 1, Name = category1, Parent = null };
                dbContext.Categories.Add(cat1);
                dbContext.SaveChanges();

                int productId = 123123;
                int categoryId = 1;
                string vendorName = "Sumsung";
                VendorName vendorN = dbContext.VendorNames.FirstOrDefault(x=>x.Name == vendorName);
                if (vendorN == null)
                {
                    VendorName vendor = new VendorName() { Name = vendorName };
                    dbContext.VendorNames.Add(vendor);
                }

                var vendorCode = "GT-5000";
                Vendor vendorV = dbContext.Vendors.FirstOrDefault(x=>x.VendorCode == vendorCode);
                if (vendorV == null)
                {
                    vendorV = new Vendor() { VendorCode = vendorCode, VendorName = vendorN };
                    dbContext.Vendors.Add(vendorV);
                }

                var listPicture = new List<Picture>();
                for(int i = 0; i<3; i++)
                {
                    Picture picture = new Picture { PictureUrl = "pic" + i, ProductId = productId };
                    dbContext.Pictures.Add(picture);
                    listPicture.Add(picture);
                }

                var product = new Product()
                {
                    ProductId = productId,
                    Barcode = "barcode",
                    CategoryId = categoryId,
                    Currency = dbContext.Currencies.Find(1),
                    Description = "description",
                    Discount = 0.0,
                    Name = "name",
                    Price = 123.6,
                    Url = "url\\",
                    Vendor = vendorV,
                    Picture = listPicture
                };
                dbContext.Products.Add(product);
                dbContext.SaveChanges();

                for (int i = 0; i < 4; i++ )
                {
                    var paramName = "name" + i;
                    ParamName paramN = dbContext.ParamNames.FirstOrDefault(x => x.Name == paramName);
                    if (paramN == null)
                    {
                        paramN = new ParamName() { Name = paramName };
                        dbContext.ParamNames.Add(paramN);
                        dbContext.SaveChanges();
                    }
                    var paramValue = "Value" + i;
                    ParamValue paramV = dbContext.ParamValues.FirstOrDefault(x => x.Name == paramValue);
                    if (paramV == null)
                    {
                        paramV= new ParamValue() { Name = paramValue };
                        dbContext.ParamValues.Add(paramV);
                        dbContext.SaveChanges();
                    }

                    dbContext.Parameters.Add(new Parameter()
                    {
                        ParamNameId = paramV.ParamValueId,
                        ParamValueId = paramN.ParamNameId,
                        ProductId = productId
                    });
                }
                dbContext.SaveChanges();

                Assert.AreEqual(1, dbContext.Products.Count());
                Assert.AreEqual(3, dbContext.Pictures.Count());
                Assert.AreEqual(4, dbContext.Parameters.Count());
                Assert.AreEqual(1, dbContext.Vendors.Count());
                Assert.AreEqual(1, dbContext.VendorNames.Count());
                Assert.AreEqual(1, dbContext.Categories.Count());

                dbContext.Products.Remove(product);
                dbContext.SaveChanges();

                Assert.AreEqual(0, dbContext.Products.Count());
                Assert.AreEqual(0, dbContext.Pictures.Count());
                Assert.AreEqual(0, dbContext.Parameters.Count());
                Assert.AreEqual(1, dbContext.Categories.Count());

                dbContext.Categories.Remove(cat1);
                dbContext.SaveChanges();
                Assert.AreEqual(0, dbContext.Categories.Count());
            }
        }
Exemplo n.º 3
0
        private void LoadProducts(IEnumerable<XElement> items)
        {
            int i = 0;
            String StringTags = "";
            foreach (var item in items)
            {
                int id = Convert.ToInt32(item.Attribute("id").Value);
                int categoryId = Convert.ToInt32(item.Element("categoryId").Value);

                var vendorName = ReadAttribute(item, "vendor");
                if (!vendorNames.ContainsKey(vendorName))
                {
                    VendorName vendor = new VendorName() { Name = vendorName };
                    vendorNames[vendorName] = vendor;
                    dataContext.VendorNames.Add(vendor);
                }

                var vendorCode = ReadAttribute(item, "vendorCode");
                if (!vendors.ContainsKey(vendorCode))
                {
                    Vendor vendor = new Vendor() { VendorCode = vendorCode, VendorName = vendorNames[vendorName] };
                    vendors[vendorCode] = vendor;
                    dataContext.Vendors.Add(vendor);
                }

                var listPicture = new List<Picture>();
                foreach (XElement pic in item.Elements("picture"))
                {
                    Picture picture = new Picture
                    {
                        PictureUrl = pic.Value,
                        ProductId = id
                    };
                    dataContext.Pictures.Add(picture);
                    listPicture.Add(picture);
                }

                StringTags += dataContext.Categories.Find(categoryId).Name + " ";
                StringTags += vendorName;

                List<Tag> tags = new List<Tag>();
                foreach (var tag in new HelpTableTag().GetTagsFromString(StringTags))
                {
                    tags.Add(dataContext.Tags.Find(tag.TagId));
                }
                double price =0;

                try
                {
                    price = Convert.ToDouble(ReadAttribute(item, "price"));
                }
                catch { }

                var product = new Product()
                {
                    ProductId = id,
                    Barcode = item.GetStringValue("barcode"),
                    CategoryId = categoryId,
                    Currency = item.GetEntity("currencyId", currencies),
                    Description = item.GetStringValue("description"),
                    Discount = 0.0,
                    Name = item.GetStringValue("name"),
                    Price = price,
                    Url = item.GetStringValue("url"),
                    Vendor = vendors[vendorCode],
                    Picture = listPicture,
                    Tags = tags
                };

                StringTags = "";
                dataContext.Products.Add(product);
                dataContext.SaveChanges();

                foreach (XElement element in item.Elements("param"))
                {
                    var paramName = element.Attribute("name").Value;
                    if (!paramNames.ContainsKey(paramName))
                    {
                        ParamName param = new ParamName() { Name = paramName };
                        dataContext.ParamNames.Add(param);
                        dataContext.SaveChanges();
                        paramNames[paramName] = dataContext.ParamNames.First(x => x.Name == paramName);
                    }
                    var paramValue = element.Value;
                    if (!paramValues.ContainsKey(paramValue))
                    {
                        ParamValue param = new ParamValue() { Name = paramValue };
                        dataContext.ParamValues.Add(param);
                        dataContext.SaveChanges();
                        paramValues[paramValue] = dataContext.ParamValues.First(x => x.Name == paramValue);
                    }

                    dataContext.Parameters.Add(new Parameter()
                    {
                        ParamValueId = paramValues[paramValue].ParamValueId,
                        ParamNameId = paramNames[paramName].ParamNameId,
                        ProductId = id
                    });
                }
                if (price != 0)
                    dataContext.Store.Add(new Store() { ProductId = id, Quantity = 2 });

                dataContext.SaveChanges();

                if (++i % 10!= 0)
                {
                    continue;
                }
                Console.WriteLine("Commit product: {0}", i);
                if(i==2000)
                    break;
            }
            dataContext.SaveChanges();
        }
Exemplo n.º 4
0
        public static void AddOrder()
        {
            num++;
            using (var dbContext = new DataContext(dataBase+num.ToString()))
            {
                Adress adress1 = new Adress() { Country = "12", Street = "12", Town = "12" };
                Adress adress2 = new Adress() { Country = "13", Street = "13", Town = "13" };

                dbContext.Adresses.Add(adress1);
                dbContext.Adresses.Add(adress2);

                List<Adress> adresses = new List<Adress>();
                adresses.Add(adress1);
                adresses.Add(adress2);

                User user = new User() { UserName = "******", Email = "mail.ru", Phone = "12-12-12", Login = "******", Adress = adresses };
                dbContext.Users.Add(user);

                Category cat5 = new Category() { CategoryId = 25, Name = "category25", Parent = null };
                dbContext.Categories.Add(cat5);

                dbContext.Currencies.Add(new Currency() { Name = "RUR", Rate = 1 });
                dbContext.SaveChanges();

                int productId = 1231234;
                int categoryId = 25;
                string vendorName = "Sumsung";
                VendorName vendorN = dbContext.VendorNames.FirstOrDefault(x => x.Name == vendorName);
                if (vendorN == null)
                {
                    VendorName vendor = new VendorName() { Name = vendorName };
                    dbContext.VendorNames.Add(vendor);
                }

                var vendorCode = "GT-5000";
                Vendor vendorV = dbContext.Vendors.FirstOrDefault(x => x.VendorCode == vendorCode);
                if (vendorV == null)
                {
                    vendorV = new Vendor() { VendorCode = vendorCode, VendorName = vendorN };
                    dbContext.Vendors.Add(vendorV);
                }

                var listPicture = new List<Picture>();
                for (int i = 0; i < 3; i++)
                {
                    Picture picture = new Picture { PictureUrl = "pic" + i, ProductId = productId };
                    dbContext.Pictures.Add(picture);
                    listPicture.Add(picture);
                }

                var product = new Product()
                {
                    ProductId = productId,
                    Barcode = "barcode",
                    CategoryId = categoryId,
                    Currency = dbContext.Currencies.Find(1),
                    Description = "description",
                    Discount = 0.0,
                    Name = "name",
                    Price = 123.6,
                    Url = "url\\",
                    Vendor = vendorV,
                    Picture = listPicture
                };
                dbContext.Products.Add(product);
                dbContext.SaveChanges();

                Basket basket1 = new Basket() { ProductId = 1231234, UserId = 1 };
                dbContext.Baskets.Add(basket1);
                Basket basket2 = new Basket() { ProductId = 1231234, UserId = 1 };
                dbContext.Baskets.Add(basket2);
                dbContext.SaveChanges();

                Assert.AreEqual(2, dbContext.Baskets.Count());
                Order order = new Order() { AdressId = 1, CurrencyId = 1, OrderData = DateTime.Now, UserId = 1, Currency = dbContext.Currencies.Find(1), TotalPrice = 2345 };
                dbContext.Orders.Add(order);

                int k = dbContext.Baskets.Where(m => m.UserId == 1).Count();
                for (int i = 0; i <k ; i++)
                {
                    Basket basket = dbContext.Baskets.First(x => x.UserId == 1);
                    OrderProduct orderProduct = new OrderProduct() { OrderId = 1, ProductId = basket.ProductId, Quantity = 2 };
                    dbContext.OrderProducts.Add(orderProduct);
                    dbContext.Baskets.Remove(basket);
                    dbContext.SaveChanges();
                }
                dbContext.SaveChanges();

                Assert.AreEqual(1, dbContext.Users.Count());
                Assert.AreEqual(2, dbContext.Adresses.Count());
                Assert.AreEqual(1, dbContext.Products.Count());
                Assert.AreEqual(1, dbContext.Categories.Count());
                Assert.AreEqual(1, dbContext.Currencies.Count());
                Assert.AreEqual(1, dbContext.Orders.Count());
                Assert.AreEqual(2, dbContext.OrderProducts.Count());
                Assert.AreEqual(0, dbContext.Baskets.Count());

                dbContext.Categories.Remove(cat5);
                dbContext.Products.Remove(product);
                dbContext.SaveChanges();

                Assert.AreEqual(0, dbContext.Products.Count());
                Assert.AreEqual(0, dbContext.Categories.Count());
                Assert.AreEqual(1, dbContext.Orders.Count());
                Assert.AreEqual(0, dbContext.OrderProducts.Count());
                Assert.AreEqual(1, dbContext.Users.Count());
                Assert.AreEqual(2, dbContext.Adresses.Count());

                dbContext.Users.Remove(user);
                dbContext.SaveChanges();
                Assert.AreEqual(0, dbContext.Users.Count());
                Assert.AreEqual(0, dbContext.Adresses.Count());
                Assert.AreEqual(0, dbContext.Orders.Count());
                Assert.AreEqual(0, dbContext.OrderProducts.Count());
            }
        }