private static void ImportCategoriesJson() { var categoriesJson = File.ReadAllText("Import/categories.json"); var categories = JsonConvert.DeserializeObject <Category[]>(categoriesJson); Random rnd = new Random(); using (ProductsShopContext context = new ProductsShopContext()) { context.Categories.AddRange(categories); context.SaveChanges(); var categoryIds = context.Categories.Select(c => c.Id).ToArray(); var productsIds = context.Products.Select(p => p.Id).ToArray(); var categoryProducts = new List <CategoryProduct>(); foreach (var pId in productsIds) { var index = rnd.Next(0, categoryIds.Length); var categoryId = categoryIds[index]; var currentCategoryProduct = new CategoryProduct() { ProductId = pId, CategoryId = categoryId }; categoryProducts.Add(currentCategoryProduct); } context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); } }
private static void ImportProductsXml() { XDocument document = XDocument.Load("Import/products.xml"); var products = document.Root.Elements(); var productsToDatabase = new List <Product>(); Random rnd = new Random(); using (ProductsShopContext context = new ProductsShopContext()) { var userIds = context.Users.Select(u => u.Id).ToArray(); foreach (var product in products) { var name = product.Element("name").Value; var price = decimal.Parse(product.Element("price").Value); var currentProduct = new Product() { Name = name, Price = price }; var index = rnd.Next(0, userIds.Length); var sellerId = userIds[index]; currentProduct.SellerId = sellerId; productsToDatabase.Add(currentProduct); } context.Products.AddRange(productsToDatabase); context.SaveChanges(); var productBuyer = context.Products.ToArray(); for (int i = 0; i < productBuyer.Length / 4; i++) { var currentProduct = productBuyer[i]; var index = rnd.Next(0, userIds.Length); var buyerId = userIds[index]; while (currentProduct.SellerId == buyerId) { index = rnd.Next(0, userIds.Length); buyerId = userIds[index]; } currentProduct.BuyerId = buyerId; } context.SaveChanges(); } }
private static void ImportProductsFromXml() { var productsXml = XDocument.Load("Resources/products.xml"); var rnd = new Random(); var elements = productsXml.Root.Elements(); var products = new List <Product>(); using (var context = new ProductsShopContext()) { var userIds = context.Users.Select(u => u.Id).ToArray(); var categoryIds = context.Categories.Select(c => c.Id).ToArray(); foreach (var e in elements) { int sellerIndex = rnd.Next(0, userIds.Length); var name = e.Element("name").Value; var price = decimal.Parse(e.Element("price").Value); int sellerId = userIds[sellerIndex]; int?buyerId = null; if (rnd.Next(0, 3) == 1) { int buyerIndex = rnd.Next(0, userIds.Length); buyerId = userIds[buyerIndex]; } var product = new Product { BuyerId = buyerId, Name = name, Price = price, SellerId = sellerId, }; products.Add(product); } context.Products.AddRange(products); context.SaveChanges(); var categoryProducts = GetCategories(rnd); context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); } }
private static void SeedProducts(ProductsShopContext ctx) { XDocument xmlData = XDocument.Load("../../../Import-XML-Resources/products.xml"); ICollection <Product> products = new HashSet <Product>(); Random rand = new Random(); int usersCount = ctx.Users.Count(); xmlData.Root.Elements().ToList().ForEach(p => { string name = p.Element("name").Value; decimal price = decimal.Parse(p.Element("price").Value); Product product = new Product { Name = name, Price = price, }; double shouldHaveBuyer = rand.NextDouble(); product.SellerId = rand.Next(1, usersCount + 1); if (shouldHaveBuyer <= 0.9) { product.BuyerId = rand.Next(1, usersCount + 1); } products.Add(product); }); ctx.Products.AddRange(products); ctx.SaveChanges(); }
public static void ImportCategories(string categoriesPath) { using (ProductsShopContext context = new ProductsShopContext()) { XDocument xmlDocument = XDocument.Load(categoriesPath); //var categories = xmlDocument.Root.Elements().Select(ParseCategory).ToList(); var categories = xmlDocument.XPathSelectElements("categories/category").Select(ParseCategory).ToList(); int countOfProducts = context.Products.Count(); Random rnd = new Random(); foreach (Category category in categories) { for (int i = 0; i < countOfProducts / 3; i++) { Product product = context.Products.Find(rnd.Next(1, countOfProducts + 1)); category.Products.Add(product); } } context.Categories.AddRange(categories); context.SaveChanges(); } }
public static void SeedData(ProductsShopContext ctx) { SeedUsers(ctx); SeedProducts(ctx); SeedCategories(ctx); ctx.SaveChanges(); }
internal static string ImportCategoriesFromXml() { var path = "Inputs/categories.xml"; var importedCategories = ImportXml <Category>(path); var categories = new List <Category>(); foreach (var impCategory in importedCategories) { var name = impCategory.Element("name").Value; var category = new Category() { Name = name }; categories.Add(category); } using (var db = new ProductsShopContext()) { db.Categories.AddRange(categories); db.SaveChanges(); } return($"{categories.Count} categories was imported from XML file: {path}"); }
private static void ImportCategoriesFromXml() { var categoriesXml = XDocument.Load("Resources/categories.xml"); var elements = categoriesXml.Root.Elements(); var categories = new List <Category>(); foreach (var e in elements) { var name = e.Element("name").Value; var category = new Category { Name = name }; categories.Add(category); } using (var context = new ProductsShopContext()) { context.Categories.AddRange(categories); context.SaveChanges(); } }
public static void ImportProductsFromXml(string path) { var stringXml = File.ReadAllText(path); var elements = XDocument.Parse(stringXml).Root.Elements(); var products = new List <Product>(); foreach (var xElement in elements) { var currentProduct = new Product { Name = xElement.Element("name").Value, Price = decimal.Parse(xElement.Element("price").Value) }; products.Add(currentProduct); } using (var context = new ProductsShopContext()) { var random = new Random(); var userIds = context.Users.Select(u => u.Id).ToList(); var index = random.Next(0, userIds.Count); var sellerId = userIds[index]; int?buyerId = sellerId; while (buyerId == sellerId) { var buyerIndex = random.Next(0, userIds.Count); buyerId = userIds[buyerIndex]; } foreach (var product in products) { index = random.Next(0, userIds.Count); sellerId = userIds[index]; product.SellerId = sellerId; if (buyerId - sellerId < 5 && buyerId - sellerId > 0) { buyerId = null; } product.BuyerId = buyerId; } context.Products.AddRange(products); var categoryIds = context.Categories.Select(c => c.Id).ToList(); foreach (var product in products) { index = random.Next(0, categoryIds.Count); product.CategoryProductses.Add(new CategoryProducts { CategoryId = categoryIds[index], ProductId = product.Id }); } context.SaveChanges(); } }
static string ImportCategoriesXML() { var path = "Files/categories.xml"; var xmlString = File.ReadAllText(path); var xmlDoc = XDocument.Parse(xmlString); var elements = xmlDoc.Root.Elements(); var categories = new List <Category>(); foreach (var element in elements) { var category = new Category() { Name = element.Element("name").Value }; categories.Add(category); } using (var context = new ProductsShopContext()) { context.Categories.AddRange(categories); context.SaveChanges(); } return($"{categories.Count} categories imported succesfully from {path}"); }
private static void ImportProductsJson() { var productsJson = File.ReadAllText("Import/products.json"); var products = JsonConvert.DeserializeObject <Product[]>(productsJson); Random rnd = new Random(); using (ProductsShopContext context = new ProductsShopContext()) { var userIds = context.Users.Select(u => u.Id).ToArray(); foreach (var product in products) { var index = rnd.Next(0, userIds.Length); var sellerId = userIds[index]; product.SellerId = sellerId; } for (int i = 0; i < products.Length / 4; i++) { var index = rnd.Next(0, userIds.Length); while (products[i].SellerId == userIds[index]) { index = rnd.Next(0, userIds.Length); } products[i].BuyerId = userIds[index]; } context.Products.AddRange(products); context.SaveChanges(); } }
private static void ImportUsersXml() { XDocument document = XDocument.Load("Import/users.xml"); var users = document.Root.Elements(); using (ProductsShopContext context = new ProductsShopContext()) { var usersToDatabase = new List <User>(); foreach (var user in users) { var firstName = user.Attribute("firstName")?.Value; var lastName = user.Attribute("lastName")?.Value; var ageAttribute = user.Attribute("age")?.Value; int?parsedAge = int.TryParse(ageAttribute, out int parseResult) ? parseResult : default(int?); var currentUser = new User() { FirstName = firstName, LastName = lastName, Age = parsedAge }; usersToDatabase.Add(currentUser); } context.Users.AddRange(usersToDatabase); context.SaveChanges(); } }
private static void SeedProducts() { Console.WriteLine("Seeding Products (with random seller & buyer, null buyer for some products)"); using (ProductsShopContext context = new ProductsShopContext()) { XDocument xmlDoc = XDocument.Load("../../Import/products.xml"); var products = xmlDoc.Root.Elements(); int usersCount = context.Users.Count(); Random rnd = new Random(); foreach (XElement product in products) { string name = product.Element("name").Value; decimal price = decimal.Parse(product.Element("price").Value); int sellerId = rnd.Next(1, usersCount + 1); // random seller int?buyerId = rnd.Next(-usersCount / 3, usersCount + 1); // random buyer if (buyerId < 1) { buyerId = null; // leaving some products without buyer } Product newProduct = new Product() { Name = name, Price = price, SellerId = sellerId, // random seller BuyerId = buyerId // random buyer (leaving some products without a buyer) }; context.Products.Add(newProduct); } context.SaveChanges(); } }
public static void AssignRandomCategoriesToProducts(ProductsShopContext context) { int[] productsIds = context.Products.Select(p => p.Id).ToArray(); int[] categoriesIds = context.Categories.Select(c => c.Id).ToArray(); Random rand = new Random(); ICollection <CategoryProduct> cateogoriesProductsForDb = new HashSet <CategoryProduct>(); foreach (int productId in productsIds) { int categoryIndex = rand.Next(0, categoriesIds.Length); int randomCategoryId = categoriesIds[categoryIndex]; while (cateogoriesProductsForDb.Any(cp => cp.CategoryId == randomCategoryId && cp.ProductId == productId)) { categoryIndex = rand.Next(0, categoriesIds.Length); randomCategoryId = categoriesIds[categoryIndex]; } CategoryProduct categoryProduct = new CategoryProduct { CategoryId = randomCategoryId, ProductId = productId }; cateogoriesProductsForDb.Add(categoryProduct); } context.CategoryProducts.AddRange(cateogoriesProductsForDb); context.SaveChanges(); }
public override string Execute(ProductsShopContext context) { string jsonFilePath = Helpers.TryLocateFileForImport(); try { ICollection <Category> categories = Helpers.ImportFromJson <Category>(jsonFilePath); context.Categories.AddRange(categories); context.SaveChanges(); Helpers.AssignRandomCategoriesToProducts(context); string result = string.Format(Messages.SeveralEntitiesImportedFromFile, categories.Count, nameof(categories), jsonFilePath); if (categories.Count == 1) { result = string.Format(Messages.OneEntityImportedFromFile, "category", jsonFilePath); } return(result); } catch (Exception) { throw new InvalidOperationException(Messages.InvalidJsonStructure); } }
private static void ImportCategoriesProducts() { using (var db = new ProductsShopContext()) { var products = db.Products .ToArray(); var categories = db.Categories.ToArray(); var categoriesProducts = new List <CategoryProduct>(); Random r = new Random(); foreach (var p in products) { CategoryProduct categoryProduct = new CategoryProduct() { CategoryId = categories[r.Next(0, categories.Length)].Id, ProductId = p.Id, }; categoriesProducts.Add(categoryProduct); } db.CategoriesProducts.AddRange(categoriesProducts); db.SaveChanges(); } }
private static void ImportProducts() { var productsAsString = File.ReadAllText("products.json"); var products = JsonConvert.DeserializeObject <Product[]>(productsAsString); using (var db = new ProductsShopContext()) { var users = db.Users.ToArray(); int counter = 1; Random r = new Random(); foreach (var p in products) { if (counter % 3 != 0) { p.Buyer = users[r.Next(0, 56)]; p.Seller = users[r.Next(0, 56)]; } else { p.Seller = users[r.Next(0, 56)]; p.Buyer = null; } counter++; } db.Products.AddRange(products); db.SaveChanges(); } }
public static string ImportCategoriesFromXml() { var path = "Files/categories.xml"; var xmlString = File.ReadAllText(path); var xmlDoc = XDocument.Parse(xmlString); var xmlElement = xmlDoc.Root.Elements(); var categories = new List <Category>(); foreach (var e in xmlElement) { var catName = e.Element("name").Value; var category = new Category { Name = catName }; categories.Add(category); } using (var db = new ProductsShopContext()) { db.Categories.AddRange(categories); db.SaveChanges(); } return($"{categories.Count()} categories has been inserted from file: {path}"); }
public static void ImportUsersFromXml(string path) { var xmlString = File.ReadAllText(path); var elements = XDocument.Parse(xmlString).Root.Elements(); var users = new List <User>(); foreach (var xElement in elements) { var firstName = xElement.Attribute("firstName")?.Value; var lastName = xElement.Attribute("lastName").Value; int?age = null; if (xElement.Attribute("age") != null) { age = int.Parse(xElement.Attribute("age").Value); } var user = new User(firstName, lastName, age); users.Add(user); } using (var context = new ProductsShopContext()) { context.Users.AddRange(users); context.SaveChanges(); } }
public static void SetProductCategories() { using (var db = new ProductsShopContext()) { int[] productsIds = db.Products.AsNoTracking().Select(e => e.Id).ToArray(); int[] categoriesIds = db.Categories.AsNoTracking().Select(e => e.Id).ToArray(); var categoryproducts = new List <CategoryProduct>(); foreach (var productId in productsIds) { for (int i = 0; i < 3; i++) { var random = new Random(); var categoryId = random.Next(1, categoriesIds.Length + 1); while (categoryproducts.Any(e => e.ProductId == productId && e.CategoryId == categoryId)) { categoryId = random.Next(1, categoriesIds.Length + 1); } var categoryProduct = new CategoryProduct() { ProductId = productId, CategoryId = categoryId }; categoryproducts.Add(categoryProduct); } } db.CategoryProducts.AddRange(categoryproducts); db.SaveChanges(); } }
public static string InsertProducts() { var productsPath = "Files/products.json"; var products = ImprotJson <Product>(productsPath); var random = new Random(); using (var db = new ProductsShopContext()) { foreach (var product in products) { int[] userIds = db.Users.Select(e => e.Id).ToArray(); var sellerId = random.Next(1, userIds.Length + 1); int? buyerId = random.Next(1, userIds.Length + 1); if (buyerId == sellerId || buyerId - sellerId < 10) { buyerId = null; } product.SellerId = sellerId; product.BuyerId = buyerId; } db.Products.AddRange(products); db.SaveChanges(); } var result = $"{products.Length} has been added from file: {productsPath}"; return(result); }
// Import and Export -> XML private static void ImportUsersFromXml(ProductsShopContext context) { const string path = "Resources/users.xml"; var xmlString = File.ReadAllText(path); var xml = XDocument.Parse(xmlString); var root = xml.Root.Elements(); var users = new List <User>(); foreach (var xElement in root) { var firstName = xElement.Attribute("firstName")?.Value; var lastName = xElement.Attribute("lastName").Value; int?age = null; if (xElement.Attribute("age")?.Value != null) { age = int.Parse(xElement.Attribute("age")?.Value); } var user = new User { FirstName = firstName, LastName = lastName, Age = age }; users.Add(user); } context.Users.AddRange(users); context.SaveChanges(); Console.WriteLine($"{users.Count} users were imported from file: {path}"); }
private static void ImportUsersFromXml() { var usersXml = XDocument.Load("Resources/users.xml"); var elements = usersXml.Root.Elements(); var users = new List <User>(); foreach (var e in elements) { var firstName = e.Attribute("firstName")?.Value; var lastName = e.Attribute("lastName").Value; int?age = null; if (e.Attribute("age") != null) { age = int.Parse(e.Attribute("age").Value); } var user = new User { FirstName = firstName, LastName = lastName, Age = age }; users.Add(user); } using (var context = new ProductsShopContext()) { context.Users.AddRange(users); context.SaveChanges(); } }
private static void ImportCategoriesFromXml(ProductsShopContext context) { const string path = "Resources/categories.xml"; var xmlString = File.ReadAllText(path); var xml = XDocument.Parse(xmlString); var root = xml.Root.Elements(); var categories = new List <Category>(); foreach (var xElement in root) { var name = xElement.Element("name").Value; var category = new Category { Name = name }; categories.Add(category); } context.Categories.AddRange(categories); context.SaveChanges(); Console.WriteLine($"{categories.Count} categories were imported from file: {path}"); }
public static string ImportCategoryProducts(ProductsShopContext context, string inputJson) { var categoryProducts = new List <CategoryProduct>(); try { categoryProducts = JsonConvert.DeserializeObject <List <CategoryProduct> >(File.ReadAllText(inputJson)); } catch (Exception e) { throw e.InnerException; } try { context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); } catch (Exception) { throw; } return($"Successfully imported {context.CategoryProducts.Count()}"); }
static string ImportCategoriesFromXML() { var 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 x in elements) { var category = new Category() { Name = x.Element("name").Value }; categories.Add(category); } using (var db = new ProductsShopContext()) { db.Categories.AddRange(categories); db.SaveChanges(); } return($"{categories.Count} categories were imported from file: {path}"); }
public static void ImportProducts(string productsPath) { using (ProductsShopContext context = new ProductsShopContext()) { XDocument xmlDocument = XDocument.Load(productsPath); //var products = xmlDocument.Root.Elements().Select(ParseProduct).ToList(); var products = xmlDocument.XPathSelectElements("products/product").Select(ParseProduct).ToList(); int countOfUsers = context.Users.Count(); Random rnd = new Random(); foreach (Product product in products) { product.SellerId = rnd.Next(1, countOfUsers + 1); if (product.SellerId % 5 != 0 && product.SellerId % 10 != 0) { product.BuyerId = rnd.Next(1, countOfUsers + 1); } } context.Products.AddRange(products); context.SaveChanges(); } }
private static void ImportCategoriesXml() { XDocument xDoc = XDocument.Load("categories.xml"); var categoriesXml = xDoc.Root.Elements(); var categories = new List <Category>(); foreach (var c in categoriesXml) { string name = c.Element("name").Value; Category category = new Category() { Name = name, }; categories.Add(category); } using (var db = new ProductsShopContext()) { db.Categories.AddRange(categories); db.SaveChanges(); } }
private 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) { string name = e.Element("name")?.Value; var category = new Category() { Name = name }; categories.Add(category); } using (var context = new ProductsShopContext()) { context.Categories.AddRange(categories); context.SaveChanges(); } return($"{categories.Count} categories were imported from: {path}"); }
private static void SeedCategories(ProductsShopContext ctx) { XDocument xmlData = XDocument.Load("../../../Import-XML-Resources/categories.xml"); ICollection <Category> categories = new HashSet <Category>(); Random rand = new Random(); int productsCount = ctx.Products.Count(); var products = ctx.Products; xmlData.Root.Elements().ToList().ForEach(p => { string name = p.Element("name").Value; Category category = new Category { Name = name }; for (int i = 0; i < productsCount / 3; i++) { Product product = products.Find(rand.Next(1, productsCount + 1)); category.Products.Add(product); } categories.Add(category); }); ctx.Categories.AddRange(categories); ctx.SaveChanges(); }