コード例 #1
0
        private static void SeedCategories()
        {
            using (var context = new ProductsShopContext())
            {
                var serializer = new XmlSerializer(typeof(CategoryDto[]), new XmlRootAttribute("categories"));


                var stringCategories = File.ReadAllText("Import/categories.xml");

                var categoriesDto = (CategoryDto[])serializer.Deserialize(new StringReader(stringCategories));

                var categories = new List <Category>();
                foreach (var dto in categoriesDto)
                {
                    if (!IsValid(dto))
                    {
                        continue;
                    }

                    var category = Mapper.Map <Category>(dto);

                    categories.Add(category);
                }

                context.AddRange(categories);
                context.SaveChanges();
            }
        }
コード例 #2
0
        private static void SeedUsers()
        {
            using (var context = new ProductsShopContext())
            {
                var serializer = new XmlSerializer(typeof(UserDto[]), new XmlRootAttribute("users"));
                var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") });

                var stringUsers = File.ReadAllText("Import/users.xml");

                var usersDto = (UserDto[])serializer.Deserialize(new StringReader(stringUsers));

                var users = new List <User>();
                foreach (var userDto in usersDto)
                {
                    if (!IsValid(userDto))
                    {
                        continue;
                    }

                    var user = Mapper.Map <User>(userDto);

                    users.Add(user);
                }

                context.AddRange(users);
                context.SaveChanges();
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: RumenNovachkov/DbAdvance
        static string ImportProductsXml()
        {
            var path = "products.xml";

            string xmlString = File.ReadAllText(path);

            var xmlDoc = XDocument.Parse(xmlString);

            var elements = xmlDoc.Root.Elements();

            using (var db = new ProductsShopContext())
            {
                var categoryProducts = new List <CategoryProducts>();

                var userIds     = db.Users.Select(u => u.UserId).OrderBy(u => u).ToArray();
                var categoryIds = db.Categories.Select(c => c.CategoryId).OrderBy(c => c).ToArray();

                var rnd = new Random();

                foreach (var e in elements)
                {
                    int userIndex     = rnd.Next(0, userIds.Length);
                    int sellerId      = userIds[userIndex];
                    int categoryIndex = rnd.Next(0, categoryIds.Length);
                    int categoryId    = categoryIds[categoryIndex];

                    var product = new Product()
                    {
                        Name     = e.Element("name").Value,
                        Price    = decimal.Parse(e.Element("price").Value),
                        SellerId = sellerId
                    };

                    var buyerId = rnd.Next(0, userIds.Length);

                    if (product.BuyerId != userIds[buyerId])
                    {
                        product.BuyerId = userIds[buyerId];
                    }

                    if (product.BuyerId - product.SellerId < 5 && product.BuyerId - product.SellerId > 0)
                    {
                        product.BuyerId = null;
                    }

                    var catProduct = new CategoryProducts()
                    {
                        Product    = product,
                        CategoryId = categoryId
                    };

                    categoryProducts.Add(catProduct);
                }

                db.AddRange(categoryProducts);
                db.SaveChanges();
                return($"{categoryProducts.Count} categories and products were imported from file: {path}");
            }
        }
コード例 #4
0
        static string ImportProductFromXml()
        {
            string path = "Files/products.xml";

            var xmlString = File.ReadAllText(path);

            var xmlDoc = XDocument.Parse(xmlString);

            var element = xmlDoc.Root.Elements();

            var catProducts = new List <CategoryProduct>();

            using (var context = new ProductsShopContext())
            {
                var userIds      = context.Users.Select(u => u.Id).ToArray();
                var categoriesId = context.Categories.Select(c => c.Id).ToArray();

                Random rnd = new Random();

                foreach (var e in element)
                {
                    string  name  = e.Element("name").Value;
                    decimal price = decimal.Parse(e.Element("price").Value);

                    int sellerIndex = rnd.Next(0, userIds.Length);

                    int sellerId = userIds[sellerIndex];

                    var product = new Product()
                    {
                        Name     = name,
                        Price    = price,
                        SellerId = sellerId
                    };

                    int cattegoryIndex = rnd.Next(0, categoriesId.Length);

                    int categoryId = categoriesId[cattegoryIndex];

                    var catProduct = new CategoryProduct()
                    {
                        Product    = product,
                        CategoryId = categoryId
                    };

                    catProducts.Add(catProduct);
                }

                context.AddRange(catProducts);
                context.SaveChanges();
            }

            return($"{catProducts.Count} products were imported from file :{path}");
        }
コード例 #5
0
        static string ImportCategoriesFromJson()
        {
            Category[] categories = ImportJason <Category>("Files/categories.json");

            using (var context = new ProductsShopContext())
            {
                context.AddRange(categories);
                context.SaveChanges();
            }

            return($"{categories.Length} categories were imported");
        }
        static string ImportCategoriesFromJson()
        {
            var path = "Files/categories.json";

            var categories = ImportJson <Category>(path);

            using (var context = new ProductsShopContext())
            {
                context.AddRange(categories);
                context.SaveChanges();
            }
            return($"{categories.Length} categories were successfuly imported from {path}");
        }
コード例 #7
0
        static string ImportUsersFromJson()
        {
            string path = "Files/users.json";

            User[] users = ImportJson <User>(path);

            using (var context = new ProductsShopContext())
            {
                context.AddRange(users);

                context.SaveChanges();
            }

            return($"{users.Length} users were imported from file: {path}");
        }
コード例 #8
0
        static string InportCategoriesFromJson()
        {
            string path = "Files/categories.json";

            Category[] categories = ImportJson <Category>(path);

            using (var context = new ProductsShopContext())
            {
                context.AddRange(categories);

                context.SaveChanges();
            }

            return($"{categories.Length} categories were imported from file: {path}");
        }
コード例 #9
0
        static string ImportProductsFromJson()
        {
            string path = "Files/products.json";

            //понеже в файла няма id-та за salerId, трябва сложим случайни от users:
            Random rnd = new Random();

            Product[] products = ImportJson <Product>(path);

            using (var context = new ProductsShopContext())
            {
                int[] userIds = context.Users.Select(u => u.Id).ToArray();


                foreach (var p in products)
                {
                    int index    = rnd.Next(0, userIds.Length);
                    int sellerId = userIds[index];

                    int?buyerId = sellerId;         // !
                    while (buyerId == sellerId)
                    {
                        int buyerIndex = rnd.Next(0, userIds.Length);
                        buyerId = userIds[buyerIndex];
                    }

                    if (buyerId - sellerId < 5 && buyerId - sellerId > 0)
                    {
                        buyerId = null;
                    }

                    p.SellerId = sellerId;
                    p.BuyerId  = buyerId;
                }

                context.AddRange(products);

                context.SaveChanges();
            }

            return($"{products.Length} products were imported from file: {path}");
        }
        static string ImportProductsFromJson()
        {
            var path = "Files/products.json";

            var rnd = new Random();

            var products = ImportJson <Product>(path);

            using (var context = new ProductsShopContext())
            {
                var userIds = context.Users.Select(u => u.Id).ToArray();


                foreach (var p in products)
                {
                    var sellerIndex = rnd.Next(0, userIds.Length);
                    var sellerId    = userIds[sellerIndex];

                    int?buyerId = sellerId;
                    while (buyerId == sellerId)
                    {
                        int buyerIndex = rnd.Next(0, userIds.Length);
                        buyerId = userIds[buyerIndex];
                    }
                    if (buyerId > sellerId)
                    {
                        buyerId = null;
                    }
                    p.SellerId = sellerId;
                    p.BuyerId  = buyerId;
                }

                context.AddRange(products);
                context.SaveChanges();
            }
            return($"{products.Length} products were successfuly imported from {path}");
        }
コード例 #11
0
        static string ImportProductsFromJson()
        {
            Product[] products = ImportJason <Product>("Files/products.json");

            Random rnd = new Random();

            using (var context = new ProductsShopContext())
            {
                int[] userIds = context.Users.Select(u => u.Id).ToArray();

                foreach (var p in products)
                {
                    int sellerId = userIds[rnd.Next(0, userIds.Length)];

                    int?buyerId = sellerId;

                    while (buyerId == sellerId)
                    {
                        int buyerIndex = rnd.Next(0, userIds.Length);
                        buyerId = userIds[buyerIndex];
                    }

                    if (buyerId - sellerId < 4)
                    {
                        buyerId = null;
                    }

                    p.SellerId = sellerId;
                    p.BuyerId  = buyerId;
                }

                context.AddRange(products);
                context.SaveChanges();
            }

            return($"{products.Length} products were imported");
        }
        static string ImportProductsXML()
        {
            var path = "Files/products.xml";

            var xmlString = File.ReadAllText(path);

            var xmlDoc = XDocument.Parse(xmlString);

            var elements = xmlDoc.Root.Elements();

            var catProducts = new List <CategoryProduct>();

            using (var context = new ProductsShopContext())
            {
                var rnd         = new Random();
                var userIds     = context.Users.Select(u => u.Id).ToArray();
                var categoryIds = context.Categories.Select(u => u.Id).ToArray();

                foreach (var e in elements)
                {
                    var     name  = e.Element("name").Value;
                    decimal price = decimal.Parse(e.Element("price").Value);

                    var sellerIndex = rnd.Next(0, userIds.Length);
                    var sellerId    = userIds[sellerIndex];

                    var categoryIndex = rnd.Next(0, categoryIds.Length);
                    var categoryId    = categoryIds[categoryIndex];

                    int?buyerId = sellerId;
                    while (buyerId == sellerId)
                    {
                        int buyerIndex = rnd.Next(0, userIds.Length);
                        buyerId = userIds[buyerIndex];
                    }
                    if (buyerId > sellerId)
                    {
                        buyerId = null;
                    }

                    var product = new Product()
                    {
                        Name     = name,
                        Price    = price,
                        BuyerId  = buyerId,
                        SellerId = sellerId,
                    };


                    var categoryProduct = new CategoryProduct
                    {
                        Product    = product,
                        CategoryId = categoryId
                    };

                    catProducts.Add(categoryProduct);
                }
                context.AddRange(catProducts);
                context.SaveChanges();
            }
            return($"{catProducts.Count} products imported successfully from {path}");
        }
コード例 #13
0
        static string ImportProductsFromXML()
        {
            var    path      = "../../../Files/products.xml";
            string xmlString = File.ReadAllText(path);
            var    xmlDoc    = XDocument.Parse(xmlString);

            var elements = xmlDoc.Root.Elements();

            var catProducts = new List <CategoryProduct>();

            using (var db = new ProductsShopContext())
            {
                var usersId     = db.Users.Select(u => u.Id).ToArray();
                var categoryIds = db.Categories.Select(u => u.Id).ToArray();

                Random rnd = new Random();

                foreach (var x in elements)
                {
                    string  name  = x.Element("name").Value;
                    decimal price = decimal.Parse(x.Element("price").Value);

                    int sellerindex = rnd.Next(0, usersId.Length);
                    int sellerId    = usersId[sellerindex];
                    int?buyerId     = sellerId; //

                    while (buyerId == sellerId) //
                    {
                        int buyerIndex = rnd.Next(0, usersId.Length);

                        buyerId = usersId[buyerIndex];
                    }

                    if (buyerId - sellerId < 5 && buyerId - sellerId > 0)//
                    {
                        buyerId = null;
                    }

                    var product = new Product()
                    {
                        Name     = name,
                        Price    = price,
                        SellerId = sellerId,
                        BuyerId  = buyerId//
                    };

                    int categoryIndex = rnd.Next(0, categoryIds.Length);
                    int categoryId    = categoryIds[categoryIndex];

                    var catProduct = new CategoryProduct()
                    {
                        Product    = product,
                        CategoryId = categoryId
                    };

                    catProducts.Add(catProduct);
                }

                db.AddRange(catProducts);
                db.SaveChanges();
            }

            return($"{catProducts.Count} products were imported from file :{path}");
        }
コード例 #14
0
        static string ImportProductsFromXml()
        {
            string xmlString = File.ReadAllText("Files/products.xml");

            var xmlDoc = XDocument.Parse(xmlString);

            var elements = xmlDoc.Root.Elements();

            var catProducts = new List <CategoryProduct>();

            using (var db = new ProductsShopContext())
            {
                var userIds     = db.Users.Select(u => u.Id).ToArray();
                var categoryIds = db.Categories.Select(c => c.Id).ToArray();

                Random random = new Random();

                foreach (var e in elements)
                {
                    string  name  = e.Element("name").Value;
                    decimal price = decimal.Parse(e.Element("price").Value);

                    int sellIndex = random.Next(0, userIds.Length);
                    int sellerId  = userIds[sellIndex];

                    int?buyerId = sellerId;
                    while (buyerId == sellerId)
                    {
                        int buyerIndex = random.Next(0, userIds.Length);
                        buyerId = userIds[buyerIndex];

                        if (buyerId % 6 == 0 || buyerId % 5 == 0 || buyerId % 13 == 0)
                        {
                            buyerId = null;
                        }
                    }

                    var product = new Product()
                    {
                        Name     = name,
                        Price    = price,
                        SellerId = sellerId,
                        BuyerId  = buyerId
                    };

                    int catIndex   = random.Next(0, categoryIds.Length);
                    int categoryId = categoryIds[catIndex];

                    var catProduct = new CategoryProduct()
                    {
                        Product    = product,
                        CategoryId = categoryId
                    };

                    catProducts.Add(catProduct);
                }

                db.AddRange(catProducts);
                db.SaveChanges();
            }
            return($"{catProducts.Count} products were imported");
        }