static string ImportCategoriesFromXML() { string path = "../../../Files/categories.xml"; string xmlString = File.ReadAllText(path); var xmlDoc = XDocument.Parse(xmlString); var elements = xmlDoc.Root.Elements(); var categories = new List <Category>(); foreach (var e in elements) { var category = new Category { Name = e.Element("name").Value }; categories.Add(category); } ; using (var db = new ProductShopDbContext()) { db.Categories.AddRange(categories); db.SaveChanges(); } return($"{categories.Count()} categories were added!"); }
private static void ImportUsers(IMapper mapper) { var xmlString = File.ReadAllText("../../../XML/Inner/users.xml"); var serializer = new XmlSerializer(typeof(UserDto_02[]), new XmlRootAttribute("users")); var deserializedUsers = (UserDto_02[])serializer.Deserialize(new StringReader(xmlString)); List <User> users = new List <User>(); foreach (var userDto in deserializedUsers) { if (!IsValid(userDto)) { continue; } var user = mapper.Map <User>(userDto); users.Add(user); } var ctx = new ProductShopDbContext(); ctx.Users.AddRange(users); ctx.SaveChanges(); }
private static void ImportProducts(ProductShopDbContext context) { if (!context.Users.Any()) { ImportUsers(context); } HashSet <User> users = context.Users.ToHashSet(); using (StreamReader productsJSON = File.OpenText(@"..\..\..\Resources\products.json")) { Product[] products = JsonConvert.DeserializeObject <Product[]>(productsJSON.ReadToEnd()); List <int> sellerIds = users.Select(u => u.Id).Take(users.Count / 2).ToList(); List <int> buyerIds = users.Select(u => u.Id).Skip(sellerIds.Count).ToList(); Random rng = new Random(); HashSet <Product> existingProducts = context.Products.ToHashSet(); for (int i = 0; i < products.Length; i++) { Product product = products[i]; if (IsObjectValid(product) && !existingProducts.Any(p => p.Name == product.Name)) { product.SellerId = sellerIds[rng.Next(0, sellerIds.Count)]; if (i % 5 != 0) { product.BuyerId = buyerIds[rng.Next(0, buyerIds.Count)]; } context.Products.Add(product); existingProducts.Add(product); } } context.SaveChanges(); } }
static string ImportCategoriesFromXml() { string path = "Files/categories.xml"; var xmlStr = File.ReadAllText(path); var xmlDoc = XDocument.Parse(xmlStr); var elements = xmlDoc.Root.Elements(); var categories = new List <Category>(); foreach (var e in elements) { string catName = e.Element("name").Value; Category cat = new Category() { Name = catName }; categories.Add(cat); } using (var db = new ProductShopDbContext()) { db.Categories.AddRange(categories); db.SaveChanges(); } return($"{categories.Count()} categories were imported successfully from {path}!"); }
private static void SeedCategoriesProducts(ProductShopDbContext context) { if (!context.Categories.Any()) { ImportCategories(context); } List <int> categoryIds = context.Categories.Select(c => c.Id).ToList(); if (!context.Products.Any()) { ImportProducts(context); } List <int> productIds = context.Products.Select(p => p.Id).OrderBy(id => id).ToList(); Random rng = new Random(); HashSet <CategoryProduct> categoryProducts = context.CategoryProducts.ToHashSet(); foreach (int productId in productIds) { CategoryProduct categoryProduct = new CategoryProduct() { ProductId = productId }; if (!categoryProducts.Any(cp => cp.ProductId == productId)) { categoryProduct.CategoryId = categoryIds[rng.Next(0, categoryIds.Count - 1)]; context.CategoryProducts.Add(categoryProduct); categoryProducts.Add(categoryProduct); } } context.SaveChanges(); }
private static void ImportCategories(IMapper mapper) { var xmlString = File.ReadAllText("../../../XML/Inner/categories.xml"); var serializer = new XmlSerializer(typeof(CategoryDto_02[]), new XmlRootAttribute("categories")); var deserializedCategories = (CategoryDto_02[])serializer.Deserialize(new StringReader(xmlString)); List <Category> categories = new List <Category>(); foreach (var categoryDto in deserializedCategories) { if (!IsValid(categoryDto)) { continue; } var category = mapper.Map <Category>(categoryDto); categories.Add(category); } var ctx = new ProductShopDbContext(); ctx.Categories.AddRange(categories); ctx.SaveChanges(); }
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(); }
private static void ReadCategoryProducts() { var config = new MapperConfiguration(cfg => { cfg.AddProfile <ProductShopProfile>(); }); var mapper = config.CreateMapper(); var categoryProducts = new List <CategoryProduct>(); for (int productId = 1; productId < 200; productId++) { var categoryId = new Random().Next(1, 11); var categoryProduct = new CategoryProduct() { ProductId = productId, CategoryId = categoryId }; categoryProducts.Add(categoryProduct); } var context = new ProductShopDbContext(); context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); }
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(); } }
static string ImportCategoriesFromJson() { string path = "../../../Files/categories.json"; Category[] categories = ImportJson <Category>(path); using (var db = new ProductShopDbContext()) { db.Categories.AddRange(categories); db.SaveChanges(); } return($"{categories.Length} categories were imported from file: {path}"); }
static string ImportUsersFromJson() { string path = "../../../Files/users.json"; User[] users = ImportJson <User>(path); using (var db = new ProductShopDbContext()) { db.Users.AddRange(users); db.SaveChanges(); } return($"{users.Length} users were imported from file: {path}"); }
static string ImportProductsFromXml() { Random rnd = new Random(); string path = "Files/products.xml"; string xmlStr = File.ReadAllText(path); var xml = XDocument.Parse(xmlStr); var elements = xml.Root.Elements(); var products = new List <Product>(); using (var db = new ProductShopDbContext()) { var userIds = db.Users.Select(x => x.UserId).ToList(); foreach (var e in elements) { int index = rnd.Next(0, userIds.Count()); int sellerId = userIds[index]; int?buyerId = sellerId; while (buyerId == sellerId) { int buyerIndex = rnd.Next(0, userIds.Count()); buyerId = userIds[buyerIndex]; } if (buyerId - sellerId < 5) { buyerId = null; } string name = e.Element("name").Value; decimal price = decimal.Parse(e.Element("price").Value); Product product = new Product() { Name = name, Price = price, SellerId = sellerId, BuyerId = buyerId }; products.Add(product); } db.Products.AddRange(products); db.SaveChanges(); } return($"{products.Count()} products were imported successfully from file: {path}"); }
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!"); }
private static void ImportCategories(ProductShopDbContext context) { using (StreamReader categoriesJSON = File.OpenText(@"..\..\..\Resources\categories.json")) { Category[] categories = JsonConvert.DeserializeObject <Category[]>(categoriesJSON.ReadToEnd()); HashSet <Category> existingCategories = context.Categories.ToHashSet(); foreach (Category category in categories) { if (IsObjectValid(category) && !existingCategories.Any(c => c.Name == category.Name)) { context.Categories.Add(category); existingCategories.Add(category); } } context.SaveChanges(); } }
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(); }
private static void ImportUsers(ProductShopDbContext context) { using (StreamReader usersJSON = File.OpenText(@"..\..\..\Resources\users.json")) { User[] users = JsonConvert.DeserializeObject <User[]>(usersJSON.ReadToEnd()); HashSet <User> existingUsers = context.Users.ToHashSet(); foreach (User user in users) { if (IsObjectValid(user) && !existingUsers.Any(u => u.LastName == user.LastName && u.FirstName == user.FirstName && u.Age == user.Age)) { context.Users.Add(user); existingUsers.Add(user); } } context.SaveChanges(); } }
static string ImportProductsFromJson() { string path = "Files/products.json"; Product[] products = ImportJson <Product>(path); Random rnd = new Random(); using (var db = new ProductShopDbContext()) { int[] userIds = db.Users.Select(x => x.UserId).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; } db.Products.AddRange(products); db.SaveChanges(); } return($"{products.Length} were imported successfully from file: {path}"); }
private static void ImportProducts(IMapper mapper) { var xmlString = File.ReadAllText("../../../XML/Inner/products.xml"); var serializer = new XmlSerializer(typeof(ProductDto_02[]), new XmlRootAttribute("products")); var deserializedProducts = (ProductDto_02[])serializer.Deserialize(new StringReader(xmlString)); List <Product> products = new List <Product>(); int counter = 1; foreach (var productDto in deserializedProducts) { if (!IsValid(productDto)) { continue; } var product = mapper.Map <Product>(productDto); var buyerId = new Random().Next(1, 30); var sellerId = new Random().Next(30, 57); product.BuyerId = buyerId; product.SellerId = sellerId; if (counter == 4) { product.BuyerId = null; counter = 0; } counter++; products.Add(product); } var ctx = new ProductShopDbContext(); ctx.Products.AddRange(products); ctx.SaveChanges(); }
private static void ImportCategories(ProductShopDbContext context, IMapper mapper) { string categoriesXml = File.ReadAllText(@"..\..\..\Resources\categories.xml"); var serializer = new XmlSerializer(typeof(CategoryDto[]), new XmlRootAttribute("categories")); var categoryDtos = (CategoryDto[])serializer.Deserialize(new StringReader(categoriesXml)); for (int i = 0; i < categoryDtos.Length; i++) { CategoryDto categoryDto = categoryDtos[i]; if (IsObjectValid(categoryDto)) { Category category = mapper.Map <Category>(categoryDto); if (!context.Categories.Local.Any(c => c.Name == category.Name) && !context.Categories.Any(c => c.Name == category.Name)) { context.Categories.Add(category); } } } context.SaveChanges(); }
private static void ImportUsers(ProductShopDbContext context, IMapper mapper) { string usersXml = File.ReadAllText(@"..\..\..\Resources\users.xml"); var serializer = new XmlSerializer(typeof(UserDto[]), new XmlRootAttribute("users")); var userDtos = (UserDto[])serializer.Deserialize(new StringReader(usersXml)); for (int i = 0; i < userDtos.Length; i++) { UserDto userDto = userDtos[i]; if (IsObjectValid(userDto)) { User user = mapper.Map <User>(userDto); if (!context.Users.Local.Any(u => u.FirstName == user.FirstName && u.LastName == user.LastName && u.Age == user.Age) && !context.Users.Any(u => u.FirstName == user.FirstName && u.LastName == user.LastName && u.Age == user.Age)) { context.Users.Add(user); } } } context.SaveChanges(); }
private static void ReadCategoriesXml() { var config = new MapperConfiguration(x => { x.AddProfile <ProductShopProfile>(); }); var mapper = config.CreateMapper(); var xmlString = File.ReadAllText("Xml/categories.xml"); var serializer = new XmlSerializer(typeof(CategoryDto[]), new XmlRootAttribute("categories")); var deserializedCategories = (CategoryDto[])serializer.Deserialize(new StringReader(xmlString)); var categories = new List <Categories>(); foreach (var categoryDto in deserializedCategories) { if (!IsValid(categoryDto)) { continue; } var category = mapper.Map <Categories>(categoryDto); categories.Add(category); } var categoryProducts = new List <CategoryProducts>(); for (int productId = 201; productId <= 400; productId++) { var categoryId = new Random().Next(1, 12); var categoryProduct = new CategoryProducts() { ProductId = productId, CategoryId = categoryId }; categoryProducts.Add(categoryProduct); } var context = new ProductShopDbContext(); context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); }
private static void ImportCategoryProducts() { List <CategoryProduct> cps = new List <CategoryProduct>(); for (int i = 1; i <= 200; i++) { var categoryId = new Random().Next(1, 12); var categoryProduct = new CategoryProduct() { ProductId = i, CategoryId = categoryId }; cps.Add(categoryProduct); } var ctx = new ProductShopDbContext(); ctx.CategoryProducts.AddRange(cps); ctx.SaveChanges(); }
private static void ImportProducts(ProductShopDbContext context, IMapper mapper) { List <User> users = context.Users.ToList(); if (users.Count == 0) { ImportUsers(context, mapper); users = context.Users.ToList(); } string productsXml = File.ReadAllText(@"..\..\..\Resources\products.xml"); var serializer = new XmlSerializer(typeof(ProductDto[]), new XmlRootAttribute("products")); var productDtos = (ProductDto[])serializer.Deserialize(new StringReader(productsXml)); List <int> sellerIds = users.Select(u => u.Id).Take(users.Count / 2).ToList(); List <int> buyers = users.Select(u => u.Id).Skip(sellerIds.Count).ToList(); Random rng = new Random(); for (int i = 0; i < productDtos.Length; i++) { ProductDto productDto = productDtos[i]; if (IsObjectValid(productDto)) { Product product = mapper.Map <Product>(productDto); int sellerId = rng.Next(sellerIds.Min(), sellerIds.Max()); int? buyerId = null; if (i % 5 != 0) { buyerId = rng.Next(buyers.Min(), buyers.Max()); } product.SellerId = sellerId; product.BuyerId = buyerId; if (!context.Products.Local.Any(p => p.Name == product.Name) && !context.Products.Any(p => p.Name == product.Name)) { context.Products.Add(product); } } } context.SaveChanges(); }
static void SetCategories() { using (var db = new ProductShopDbContext()) { var productIds = db.Products .Select(p => p.Id) .ToArray(); var categoryIds = db.Categories .Select(c => c.Id) .ToArray(); int categoryCount = categoryIds.Length; Random rnd = new Random(); var categoryPoducts = new List <CategoryProduct>(); foreach (var p in productIds) { for (int i = 0; i < 3; i++) { int index = rnd.Next(0, categoryCount); while (categoryPoducts.Any(cp => cp.ProductId == p && cp.CategoryId == categoryIds[index])) { index = rnd.Next(0, categoryIds.Length); } var catPr = new CategoryProduct { ProductId = p, CategoryId = categoryIds[index] }; categoryPoducts.Add(catPr); } } db.CategoryProducts.AddRange(categoryPoducts); db.SaveChanges(); } }
static void Main(string[] args) { var xmlString = File.ReadAllText("Xml/users.xml"); var context = new ProductShopDbContext(); var config = new MapperConfiguration(cfg => { cfg.AddProfile <ProductShopProfile>(); }); var mapper = config.CreateMapper(); var serializer = new XmlSerializer(typeof(UserDto[]), new XmlRootAttribute("users")); var desirializedUser = (UserDto[])serializer.Deserialize(new StringReader(xmlString)); var sb = new StringBuilder(); List <Users> users = new List <Users>(); foreach (var userdto in desirializedUser) { if (!IsValid(userdto)) { sb.AppendLine("Invalid data"); continue; } var userMap = mapper.Map <Users>(userdto); users.Add(userMap); sb.AppendLine("Success"); } context.Users.AddRange(users); context.SaveChanges(); }
static void SetCategories() { using (var db = new ProductShopDbContext()) { var products = db.Products.Select(x => x.ProductId).ToList(); var categories = db.Categories.Select(x => x.CategoryId).ToList(); Random rnd = new Random(); var catProducts = new List <CategoryProduct>(); foreach (var prod in products) { for (int i = 0; i < 3; i++) { int index = rnd.Next(0, categories.Count()); CategoryProduct cp = new CategoryProduct() { CategoryId = categories[index], ProductId = prod }; if (catProducts.Any(x => x.ProductId == prod && x.CategoryId == categories[index])) { index = rnd.Next(0, categories.Count()); i--; } else { catProducts.Add(cp); } } } db.CategoriesProducts.AddRange(catProducts); db.SaveChanges(); } }
private static void SeedCategoriesProducts(ProductShopDbContext context, IMapper mapper) { List <Category> categories = context.Categories.ToList(); if (categories.Count == 0) { ImportCategories(context, mapper); categories = context.Categories.ToList(); } List <Product> products = context.Products.ToList(); if (products.Count == 0) { ImportProducts(context, mapper); products = context.Products.ToList(); } List <int> productIds = products.Select(p => p.Id).OrderBy(id => id).ToList(); List <int> categoryIds = categories.Select(c => c.Id).ToList(); Random rng = new Random(); foreach (int productId in productIds) { int categoryId = rng.Next(categoryIds.Min(), categoryIds.Max()); var categoryProduct = new CategoryProduct() { ProductId = productId, CategoryId = categoryId }; if (!context.CategoryProducts.Local.Any(cp => cp.ProductId == productId) && !context.CategoryProducts.Any(cp => cp.ProductId == productId)) { context.CategoryProducts.Add(categoryProduct); } } context.SaveChanges(); }
static string ImportUsersFromXml() { string path = "Files/users.xml"; string xmlStr = File.ReadAllText(path); var xml = XDocument.Parse(xmlStr); var elements = xml.Root.Elements(); var users = new List <User>(); foreach (var e in elements) { string firstName = e.Attribute("firstName")?.Value; string lastName = e.Attribute("lastName").Value; int?age = null; if (e.Attribute("age") != null) { age = int.Parse(e.Attribute("age").Value); } User user = new User() { FirstName = firstName, LastName = lastName, Age = age }; users.Add(user); } using (var db = new ProductShopDbContext()) { db.Users.AddRange(users); db.SaveChanges(); } return($"{users.Count()} users were imported successfully from file: {path}"); }