Example #1
0
        private static void SoldProducts()
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <ProductShopProfile>();
            });
            var mapper = config.CreateMapper();

            var xmlString = File.ReadAllText("Xml/users.xml");

            var serializer = new XmlSerializer(typeof(UserDto[]), new XmlRootAttribute("users"));

            var deserializeUser = (UserDto[])serializer.Deserialize(new StringReader(xmlString));

            var users = new List <Users>();

            foreach (var userDto in deserializeUser)
            {
                if (!IsValid(userDto))
                {
                    continue;
                }
                var user = mapper.Map <Users>(userDto);
                users.Add(user);
            }

            var context = new ProductShopDbContext();

            context.AddRange(users);
            context.SaveChanges();
        }
Example #2
0
        private void Initialize()
        {
            var users = JsonConvert.DeserializeObject <List <User> >(File.ReadAllText("users.json"));

            db.AddRange(users);
            db.SaveChanges();

            var categories = JsonConvert.DeserializeObject <List <Category> >(File.ReadAllText("categories.json"));

            db.AddRange(categories);
            db.SaveChanges();

            var products = JsonConvert.DeserializeObject <List <Product> >(File.ReadAllText("products.json"));

            var usersCount      = users.Count;
            var categoriesCount = categories.Count;

            var rnd = new Random();

            foreach (var product in products)
            {
                int sellerId = GenerateId(usersCount, rnd);
                product.SellerId = sellerId;

                int buyerId = GenerateId(usersCount, rnd);

                if (Math.Abs(buyerId - sellerId) <= 2)
                {
                    product.BuyerId = null; //some null values
                }
                else
                {
                    product.BuyerId = buyerId;
                }

                product.CategoryProducts
                .Add(new CategoryProduct {
                    CategoryId = GenerateId(categoriesCount, rnd)
                });

                db.Products.Add(product);
                db.SaveChanges();
            }
        }
Example #3
0
        static string ImportProductsFromXml()
        {
            string 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 ProductShopDbContext())
            {
                var    userIds     = db.Users.Select(u => u.Id).ToArray();
                var    categoryIds = db.Categories.Select(u => u.Id).ToArray();
                Random rnd         = new Random();

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

                    int index    = rnd.Next(0, userIds.Length);
                    int sellerId = userIds[index];

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

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

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

                    catProducts.Add(catProd);
                }

                db.AddRange(catProducts);
                db.SaveChanges();
            }
            return($"{catProducts.Count} products were imported from XML!");
        }
Example #4
0
        private static void ReadProductsXml()
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile <ProductShopProfile>();
            });
            var mapper = config.CreateMapper();

            var xmlString = File.ReadAllText("Xml/products.xml");

            var serializer = new XmlSerializer(typeof(ProductDto[]), new XmlRootAttribute("products"));

            var deserializeUser = (ProductDto[])serializer.Deserialize(new StringReader(xmlString));

            var products = new List <Products>();
            int counter  = 1;

            foreach (var productDto in deserializeUser)
            {
                if (!IsValid(productDto))
                {
                    continue;
                }

                var product = mapper.Map <Products>(productDto);

                var buyerId  = new Random().Next(1, 30);
                var sellerId = new Random().Next(31, 56);

                product.BuyerId  = buyerId;
                product.SellerId = sellerId;

                if (counter == 4)
                {
                    product.BuyerId = null;
                    counter         = 0;
                }
                products.Add(product);
                counter++;
            }

            var context = new ProductShopDbContext();

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