private static void SeedProducts() { ProductShopContext context = new ProductShopContext(); string json = File.ReadAllText("../../../datasets/products.json"); IEnumerable <Product> products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json); Random rnd = new Random(); foreach (Product product in products) { double shouldHaveBuyer = rnd.NextDouble(); product.SelledId = rnd.Next(1, context.Users.Count() + 1); if (shouldHaveBuyer <= 0.7) { product.BuyerId = rnd.Next(1, context.Users.Count() + 1); } } context.Products.AddRange(products); context.SaveChanges(); }
private static void ImportDataInMappingTableCategoriesProducts(ProductShopContext context) { var categoryProducts = new List <CategoryProduct>(); for (int productId = 1; productId < 200; productId++) //200 types of products exist { var categoryId = new Random().Next(1, 12); //11 categories exist var categoryProduct = new CategoryProduct { CategoryId = categoryId, ProductId = productId }; categoryProducts.Add(categoryProduct); } context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); }
private static void SeedCategories() { ProductShopContext context = new ProductShopContext(); string json = File.ReadAllText("../../../datasets/categories.json"); IEnumerable <Category> categories = JsonConvert.DeserializeObject <IEnumerable <Category> >(json); 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(); }
private static void GenerateCategories(ProductShopContext context) { var listOfCategoryProducts = new List <CategoryProduct>(); for (int productId = 1; productId <= 200; productId++) { var categoryId = new Random().Next(1, 12); var categoryProduct = new CategoryProduct { ProductId = productId, CategoryId = categoryId }; listOfCategoryProducts.Add(categoryProduct); } context.CategoryProducts.AddRange(listOfCategoryProducts); context.SaveChanges(); }
private static void ImportCategories(ProductShopContext context) { string jsonString = File.ReadAllText("../../../Json/Import/categories.json"); Category[] deserializedCategories = JsonConvert.DeserializeObject <Category[]>(jsonString); var categories = new List <Category>(); foreach (Category category in deserializedCategories) { if (IsValid(category)) { categories.Add(category); } } context.Categories.AddRange(categories); context.SaveChanges(); }
private static void GenerateCategoryForProducts(IMapper mapper, ProductShopContext context) { List <CategoryProduct> categoryProducts = new List <CategoryProduct>(); for (int productId = 1; productId <= 200; productId++) { var categoryId = new Random().Next(1, 12); var categoryProduct = new CategoryProduct() { CategoryId = categoryId, ProductId = productId }; categoryProducts.Add(categoryProduct); } context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); }
public static void ImportCategories(IMapper mapper, ProductShopContext context) { string jsonString = File.ReadAllText("Json/categories.json"); var deserializedCategories = JsonConvert.DeserializeObject <Category[]>(jsonString); List <Category> categories = new List <Category>(); foreach (var category in deserializedCategories) { if (!IsValid(category)) { continue; } categories.Add(category); } context.Categories.AddRange(categories); context.SaveChanges(); }
private static void GenerateCategoriesProducts(List <Product> products, List <Category> categories) { var categoriesProducts = new List <CategoryProduct>(); foreach (var product in products) { var categoryId = new Random().Next(0, categories.Count); var categoryProduct = new CategoryProduct() { ProductId = product.Id, CategoryId = categories[categoryId].Id }; categoriesProducts.Add(categoryProduct); } using (var db = new ProductShopContext()) { db.CategoriesProducts.AddRange(categoriesProducts); db.SaveChanges(); } }
private static void ImportDataInMappingTableCategoriesProducts(ProductShopContext context) { List <CategoryProduct> categoryProducts = new List <CategoryProduct>(); for (int productId = 1; productId < 200; productId++) // There are 200 products in the database. { var categoryId = new Random().Next(1, 12); // There are 11 categories in the database. var categoryProduct = new CategoryProduct() { ProductId = productId, CategoryId = categoryId }; categoryProducts.Add(categoryProduct); } context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); }
private static void ImportCategoryProducts(ProductShopContext context) { var categoryProducts = new List <CategoryProduct>(); for (int productId = 1; productId <= 200; productId++) { var categoryId = new Random().Next(1, 12); var categoryProduct = new CategoryProduct { CategoryId = categoryId, ProductId = productId }; categoryProducts.Add(categoryProduct); } context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); }
private static void ImportUsers(ProductShopContext context) { string jsonString = File.ReadAllText("../../../Json/Import/users.json"); User[] deserializedUsers = JsonConvert.DeserializeObject <User[]>(jsonString); var users = new List <User>(); foreach (User user in deserializedUsers) { if (IsValid(user)) { users.Add(user); } } context.Users.AddRange(users); context.SaveChanges(); }
private static void InitializeCategoriesAndProducts(ProductShopContext context) { var categoryProducts = new List <CategoryProducts>(); for (int productId = 1; productId < 201; productId++) { var categoryId = new Random().Next(1, 12); var categoryProduct = new CategoryProducts() { ProductId = productId, CategoryId = categoryId }; categoryProducts.Add(categoryProduct); } context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); }
private void ImportProducts(IMapper mapper, ProductShopContext context, List <int> userIds) { XmlSerializer serializer = new XmlSerializer(typeof(ProductDto[]), new XmlRootAttribute("products")); var productDtos = (ProductDto[])serializer.Deserialize(new StreamReader("../../../../XmlImport/products.xml")); var products = new List <Product>(); var counter = 0; foreach (var productDto in productDtos) { if (!IsValid(productDto)) { Console.WriteLine("Product not valid!"); continue; } var product = mapper.Map <Product>(productDto); var sellerId = userIds[new Random().Next(0, userIds.Count)]; var buyerId = userIds[new Random().Next(0, userIds.Count)]; product.SellerId = sellerId; product.BuyerId = buyerId; if (counter == 4) { product.BuyerId = null; counter = 0; } counter++; products.Add(product); } context.Products.AddRange(products); context.SaveChanges(); }
static string ImportProductsFromXml() { 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 db = new ProductShopContext()) { var userIds = db.Users.Select(x => x.Id).ToArray(); var categoryIds = db.Categories.Select(x => x.Id).ToArray(); var rnd = new Random(); foreach (var element in elements) { var name = element.Element("name").Value; var price = decimal.Parse(element.Element("price").Value); var sellerIndex = rnd.Next(0, userIds.Length); var sellerId = userIds[sellerIndex]; var product = new Product() { Name = name, Price = price, SellerId = sellerId }; 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} categories were imported from {path}"); }
private static void ImportProducts(ProductShopContext context, IMapper mapper) { string xmlString = File.ReadAllText("../../../Xml/Import/products.xml"); var serializer = new XmlSerializer(typeof(ProductDto[]), new XmlRootAttribute("products")); var deserializedProducts = (ProductDto[])serializer.Deserialize(new StringReader(xmlString)); var products = new List <Product>(); int counter = 1; foreach (ProductDto productDto in deserializedProducts) { if (!IsValid(productDto)) { continue; } Product product = mapper.Map <Product>(productDto); int buyerId = new Random().Next(1, 29); int sellerId = new Random().Next(29, 57); product.BuyerId = buyerId; product.SellerId = sellerId; if (counter % 4 == 0) { product.BuyerId = null; } products.Add(product); counter++; } context.Products.AddRange(products); context.SaveChanges(); }
private static string ImportProductsFromJson() { var path = "Files/products.json"; var products = ImportJson <Product>(path); var random = new Random(); using (var context = new ProductShopContext()) { var userIds = context .Users .Select(u => u.Id) .ToList(); foreach (var product in products) { int index = random.Next(0, userIds.Count); int sellerId = userIds[index]; int?buyerId = sellerId; while (buyerId == sellerId) { int buyerIndex = random.Next(0, userIds.Count); buyerId = userIds[buyerIndex]; } if (buyerId - sellerId < 10 && buyerId - sellerId > 0) { buyerId = null; } product.SellerId = sellerId; product.BuyerId = buyerId; } context.Products.AddRange(products); context.SaveChanges(); } return($"{products.Length} products were imported from file: {path}"); }
private static void ImportProducts(ProductShopContext dbContext, IMapper mapper) { string xmlString = File.ReadAllText("../../../Files/Import/products.xml"); XmlSerializer serializer = new XmlSerializer(typeof(ProductDto[]), new XmlRootAttribute("products")); ProductDto[] deserializedProducts = (ProductDto[])serializer.Deserialize(new StringReader(xmlString)); List <Product> products = new List <Product>(); int[] userIds = dbContext .Users .Select(u => u.Id) .ToArray(); Random random = new Random(); foreach (ProductDto productDto in deserializedProducts) { if (!IsValid(productDto)) { continue; } int index = random.Next(0, userIds.Length); int sellerId = userIds[index]; int buyerId = sellerId; while (buyerId == sellerId) { int buyerIndex = random.Next(0, userIds.Length); buyerId = userIds[buyerIndex]; } Product product = mapper.Map <Product>(productDto); product.BuyerId = buyerId; product.SellerId = sellerId; products.Add(product); } dbContext.Products.AddRange(products); dbContext.SaveChanges(); }
private static void ImportUsers() { var jsonString = File.ReadAllText("../../../JSON/Inner/users.json"); var deserializedUsers = JsonConvert.DeserializeObject <User[]>(jsonString); List <User> users = new List <User>(); foreach (var user in deserializedUsers) { if (IsValid(user)) { users.Add(user); } } var ctx = new ProductShopContext(); ctx.Users.AddRange(users); ctx.SaveChanges(); }
private static void SeedCategories(ProductShopContext ctx) { string jsonFile = File.ReadAllText("../../../Import-Json-Resources/categories.json"); IEnumerable <Category> categories = JsonConvert.DeserializeObject <IEnumerable <Category> >(jsonFile); int productsCount = ctx.Products.Count(); Random rnd = new Random(); var products = ctx.Products; foreach (Category category in categories) { for (int i = 0; i < productsCount / 3; i++) { Product product = products.Find(rnd.Next(1, productsCount + 1)); category.Products.Add(product); } } ctx.Categories.AddRange(categories); ctx.SaveChanges(); }
private static void SeedProducts(ProductShopContext ctx) { string jsonFile = File.ReadAllText("../../../Import-Json-Resources/products.json"); IEnumerable <Product> products = JsonConvert.DeserializeObject <IEnumerable <Product> >(jsonFile); Random rand = new Random(); int usersCount = ctx.Users.Count(); foreach (Product product in products) { double shouldHaveBuyer = rand.NextDouble(); product.SelledId = rand.Next(1, usersCount + 1); if (shouldHaveBuyer <= 0.9) { product.BuyerId = rand.Next(1, usersCount + 1); } } ctx.Products.AddRange(products); ctx.SaveChanges(); }
private static void ImportProducts(ProductShopContext context) { string productsJson = File.ReadAllText("../../Import/products.json"); List <Product> products = JsonConvert.DeserializeObject <List <Product> >(productsJson); int n = 0; int usersCount = context.Users.Count(); foreach (var p in products) { p.SellerId = (n % usersCount) + 1; if (n % 3 != 0) { p.BuyerId = (n * 2 % usersCount) + 1; } n++; } context.Products.AddRange(products); context.SaveChanges(); }
private static void SeedUsers(ProductShopContext ctx) { XDocument xmlData = XDocument.Load("../../../Import-XML-Resources/users.xml"); ICollection <User> users = new HashSet <User>(); xmlData.Root.Elements().ToList().ForEach(u => { string firstName = u.Attribute("first-name")?.Value; string lastName = u.Attribute("last-name")?.Value; int age = (u.Attribute("age") != null) ? Convert.ToInt32(u.Attribute("age").Value) : 0; users.Add(new User { FirstName = firstName, LastName = lastName, Age = age }); }); ctx.Users.AddRange(users); ctx.SaveChanges(); }
private static void SeedProducts() { ProductShopContext context = new ProductShopContext(); string productsJson = File.ReadAllText("../../../datasets/products.json"); IEnumerable <Product> products = JsonConvert.DeserializeObject <IEnumerable <Product> >(productsJson); Random rnd = new Random(); int usersCount = context.Users.Count(); foreach (var product in products) { product.SellerId = rnd.Next(1, usersCount + 1); double hasBuyerFactor = rnd.NextDouble(); if (hasBuyerFactor <= 0.7) { product.BuyerId = rnd.Next(1, usersCount + 1); } } context.Products.AddRange(products); context.SaveChanges(); }
private static void ImportUsers(ProductShopContext context) { XDocument xmlDoc = XDocument.Load("../../Import/users.xml"); XElement users = xmlDoc.Root; foreach (var user in users.Elements()) { string FirstName = user.Attribute("first-name")?.Value; string LastName = user.Attribute("last-name").Value; int Age = int.Parse(user.Attribute("age")?.Value ?? "0"); User userToAdd = new User() { FirstName = FirstName, LastName = LastName, Age = Age, }; context.Users.Add(userToAdd); } context.SaveChanges(); }
public static void GenerateCategoryProducts() { var categoryProducts = new List <CategoryProduct>(); for (int productId = 1; productId <= 200; productId++) { var categoryId = new Random().Next(1, 12); var categoryProduct = new CategoryProduct() { CategoryId = categoryId, ProductId = productId }; categoryProducts.Add(categoryProduct); } var context = new ProductShopContext(); context.CategoriesProducts.AddRange(categoryProducts); context.SaveChanges(); }
private static void ImportCategoriesInDatabase(ProductShopContext context) { var jsonString = File.ReadAllText("../../../Json/categories.json"); var deserializedCategories = JsonConvert.DeserializeObject <Category[]>(jsonString); List <Category> categories = new List <Category>(); foreach (var category in deserializedCategories) { if (!IsValid(category)) { continue; } categories.Add(category); } context.Categories.AddRange(categories); context.SaveChanges(); }
public static void ImportCategories() { var jsonString = File.ReadAllText("JsonInput/categories.json"); var deserializedCategories = JsonConvert.DeserializeObject <Category[]>(jsonString); var categories = new List <Category>(); foreach (var category in deserializedCategories) { if (IsValid(category)) { categories.Add(category); } } var context = new ProductShopContext(); context.Categories.AddRange(categories); context.SaveChanges(); }
public static void ImportUsers() { var jsonString = File.ReadAllText("JsonInput/users.json"); var deserializedUsers = JsonConvert.DeserializeObject <User[]>(jsonString); var users = new List <User>(); foreach (var user in deserializedUsers) { if (IsValid(user)) { users.Add(user); } } var context = new ProductShopContext(); context.Users.AddRange(users); context.SaveChanges(); }
static string ImportProductsFromJson() { var path = "products.json"; Product[] products = ImportJson <Product>(path); using (var context = new ProductShopContext()) { Random rnd = new Random(); foreach (var p in products) { var usersId = context.Users.Select(u => u.UserId).ToArray(); var index = rnd.Next(0, usersId.Length); var sellerId = usersId[index]; //Randomly set id to buyerId 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; } //Add id to sellerId and id to buyerId p.SellerId = sellerId; p.BuyerId = buyerId; } context.Products.AddRange(products); context.SaveChanges(); } return($"{products.Length} products were imported from {path}."); }
private static void ImportProducts(IMapper mapper) { var xmlString = File.ReadAllText("./../../../Xml/products.xml"); var serializer = new XmlSerializer(typeof(Dto.Import.ProductDto[]), new XmlRootAttribute("products")); var deserializedUsers = (Dto.Import.ProductDto[])serializer.Deserialize(new StringReader(xmlString)); List <Product> products = new List <Product>(); int counter = 1; foreach (var productDto in deserializedUsers) { if (!IsValid(productDto)) { continue; } var product = mapper.Map <Product>(productDto); var buyerId = new Random().Next(1, 30); var sellarId = new Random().Next(31, 56); product.BuyerId = buyerId; product.SellarId = sellarId; if (counter++ % 4 == 0) { product.BuyerId = null; } products.Add(product); } var context = new ProductShopContext(); context.Products.AddRange(products); context.SaveChanges(); }