public static string UsersWithSoldProducts(XmlProductShopContext context) { var sb = new StringBuilder(); var users = context.Users .Where(x => x.ProductsToSell.Count > 0) .OrderBy(x => x.LastName) .ThenBy(x => x.FirstName) .Select(x => new UserWithSoldProductDto { FirstName = x.FirstName, LastName = x.LastName, SoldProduct = x.ProductsToSell .Select(s => new SoldProductDto { Name = s.Name, Price = s.Price }) .ToArray() }) .ToArray(); var serializer = new XmlSerializer(typeof(UserWithSoldProductDto[]), new XmlRootAttribute("users")); serializer.Serialize(new StringWriter(sb), users, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty })); var result = sb.ToString().TrimEnd(); return(result); }
public static string ImportUsers(XmlProductShopContext context, string xmlString) { var sb = new StringBuilder(); var ser = new XmlSerializer(typeof(UserDto[]), new XmlRootAttribute("users")); var deserializedUsers = (UserDto[])ser.Deserialize(new StringReader(xmlString)); var validUsers = new List <User>(); foreach (var userDto in deserializedUsers) { if (!IsValid(userDto)) { continue; } var user = Mapper.Map <User>(userDto); validUsers.Add(user); sb.AppendLine($"User {user.LastName} was added"); } //var xmlDoc = XDocument.Parse(xmlString); //var elements = xmlDoc.Root.Elements(); //foreach (var element in elements) //{ // int? age=null; // if (element.Attribute("age")?.Value != null) // { // age = int.Parse(element.Attribute("age").Value); // } // UserDto userDto = new UserDto // { // FirstName = element.Attribute("firstName")?.Value, // LastName = element.Attribute("lastName")?.Value, // Age = age // }; // if (!IsValid(userDto)) // { // sb.AppendLine("Invalid input"); // continue; // } // var user = Mapper.Map<User>(userDto); // validUsers.Add(user); // sb.AppendLine($"User: {userDto.LastName} was added!"); //} context.Users.AddRange(validUsers); context.SaveChanges(); var result = sb.ToString().TrimEnd(); return(result); }
static void Main(string[] args) { using (var context = new XmlProductShopContext()) { ResedDatabase(context); Mapper.Initialize(cfg => cfg.AddProfile <XmlProduckShopProfile>()); //var config = new MapperConfiguration(cfg=> //{ // cfg.AddProfile<XmlProduckShopProfile>(); //}); //var mapper = config.CreateMapper(); ImportEntities(context); ExportEntities(context); } }
private static void ImportEntities(XmlProductShopContext context, string baseDir = @"..\..\..\Resources\") { const string exportDir = @"..\..\..\Resources\ImporResults\"; string users = DataProcessing.Deserializer.ImportUsers(context, File.ReadAllText(baseDir + "users.xml")); ExportToFile(users, exportDir + "Users.txt"); string categories = DataProcessing.Deserializer.ImportCategories(context, File.ReadAllText(baseDir + "categories.xml")); ExportToFile(categories, exportDir + "Categories.txt"); string products = DataProcessing.Deserializer.ImportProducts(context, File.ReadAllText(baseDir + "products.xml")); ExportToFile(products, exportDir + "Products.txt"); }
public static string ImportCategories(XmlProductShopContext context, string xmlString) { var sb = new StringBuilder(); var serializer = new XmlSerializer(typeof(CategoryDto[]), new XmlRootAttribute("categories")); var deserializedCategories = (CategoryDto[])serializer.Deserialize(new StringReader(xmlString)); var validCategories = new List <Category>(); foreach (var categotyDto in deserializedCategories) { if (!IsValid(categotyDto)) { continue; } var category = Mapper.Map <Category>(categotyDto); validCategories.Add(category); sb.AppendLine($"Category {category.Name} was added!"); } //var xmlDoc = XDocument.Parse(xmlString); //var elements = xmlDoc.Root.Elements(); //foreach (var element in elements) //{ // string name = element.Element("name").Value; // if (name.Length<3||name.Length>15) // { // sb.AppendLine("Invalid category."); // continue; // } // var category = new Category // { // Name = name // }; // validCategories.Add(category); // sb.AppendLine($"Categoty: {name} was added."); //} context.Categories.AddRange(validCategories); context.SaveChanges(); var result = sb.ToString().TrimEnd(); return(result); }
public static string UsersWithSoldProducts(XmlProductShopContext context, int productSold) { var sb = new StringBuilder(); var users = new UsersDtoForth { Count = context.Users.Count(), Users = context.Users .Where(x => x.ProductsToSell.Count >= 1) .Select(x => new UserDtoFourProb { FirstName = x.FirstName, LastName = x.LastName, Age = x.Age.ToString(), SoldProduct = new SoldProductForth { Count = x.ProductsToSell.Count(), ProductDto = x.ProductsToSell.Select(k => new ProductDtoForth { Name = k.Name, Price = k.Price }) .ToArray() } }) .ToArray() }; var xmlNamspaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }); var serializer = new XmlSerializer(typeof(UsersDtoForth), new XmlRootAttribute("users")); serializer.Serialize(new StringWriter(sb), users, xmlNamspaces); var result = sb.ToString().TrimEnd(); return(result); }
private static void ExportEntities(XmlProductShopContext context) { const string exportDir = @"..\..\..\Resources\ExportResults\"; string xmlProductInRange = DataProcessing.Serializer.ProductInRange(context, 1000m, 2000m); Console.WriteLine(xmlProductInRange); File.WriteAllText(exportDir + "products-in-range.xml", xmlProductInRange); string usersWithSoldProducts = DataProcessing.Serializer.UsersWithSoldProducts(context); Console.WriteLine(usersWithSoldProducts); File.WriteAllText(exportDir + "uers-sold-products-V3.xml", usersWithSoldProducts); string categoryByCount = DataProcessing.Serializer.CategoryByProductCount(context); Console.WriteLine(categoryByCount); File.WriteAllText(exportDir + "categories-by-products.xml", categoryByCount); string usersAndProducts = DataProcessing.Serializer.UsersWithSoldProducts(context, 1); Console.WriteLine(usersAndProducts); File.WriteAllText(exportDir + "users-and-products.xml", usersAndProducts); }
public static string CategoryByProductCount(XmlProductShopContext context) { var sb = new StringBuilder(); var categories = context.Categories .OrderByDescending(c => c.CategoryProducts.Count) .Select(c => new CategoryCountDto { Name = c.Name, Count = c.CategoryProducts.Count, TotalRevenue = c.CategoryProducts.Sum(x => x.Product.Price).ToString("F2"), AvgPrice = c.CategoryProducts.Average(x => x.Product.Price) // If category is empty will throw exception TO BE //.....CategoryProducts.Select(s=>s.Product.Price).DefaultIfEmpty(0).Average() }) .ToArray(); var ser = new XmlSerializer(typeof(CategoryCountDto[]), new XmlRootAttribute("categories")); ser.Serialize(new StringWriter(sb), categories, new XmlSerializerNamespaces(new [] { XmlQualifiedName.Empty })); var result = sb.ToString().TrimEnd(); return(result); }
public static string ProductInRange(XmlProductShopContext context, decimal minPrice, decimal maxPrice) { var sb = new StringBuilder(); var products = context.Products .Where(x => x.Price >= minPrice && x.Price <= maxPrice && x.Buyer != null) .OrderByDescending(x => x.Price) .Select(x => new ProductRangeDto { Name = x.Name, Price = x.Price.ToString("F2"), Buyer = ($"{x.Buyer.FirstName} {x.Buyer.LastName}").Trim() }) .ToArray(); var serializer = new XmlSerializer(typeof(ProductRangeDto[]), new XmlRootAttribute("products")); serializer.Serialize(new StringWriter(sb), products, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty })); var result = sb.ToString().TrimEnd(); return(result); }
private static void ResedDatabase(XmlProductShopContext context) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); }
public static string ImportProducts(XmlProductShopContext context, string xmlString) { var sb = new StringBuilder(); User[] users = context.Users.ToArray(); int usersCount = users.Length; var rnd = new Random(); var validProducts = new List <Product>(); var ser = new XmlSerializer(typeof(ProductDto[]), new XmlRootAttribute("products")); var desProducts = (ProductDto[])ser.Deserialize(new StringReader(xmlString)); int counter = 0; foreach (var prodDto in desProducts) { if (!IsValid(prodDto)) { continue; } var product = Mapper.Map <Product>(prodDto); var buyerId = rnd.Next(1, 30); var sellerId = rnd.Next(31, 56); product.BuyerId = buyerId; product.SellerId = sellerId; if (counter++ == 4) { product.BuyerId = null; counter = 0; } counter++; Category[] categories = context.Categories.ToArray(); int categoryCount = categories.Length; for (int i = 0; i < 3; i++) { int index = rnd.Next(0, categoryCount); var categoryId = categories[index].Id; if (product.CategoryProducts.Any(x => x.CategoryId == categoryId)) { continue; } product.CategoryProducts.Add(new CategoryProduct { CategoryId = categoryId }); } validProducts.Add(product); sb.AppendLine($"product {product.Name} was added"); } //var xmlDoc = XDocument.Parse(xmlString); //var elements = xmlDoc.Root.Elements(); //foreach (var element in elements) //{ // var name = element.Element("name").Value; // var price = decimal.Parse(element.Element("price").Value); // var seller = users[rnd.Next(0, usersCount)]; // var buyer = users[rnd.Next(0, usersCount)]; // if (Math.Abs(buyer.Id-seller.Id)<5) // { // buyer = null; // } // var productDto = new ProductDto // { // Name=name, // Price=price, // SellerId=seller.Id, // BuyerId=buyer?.Id // }; // if (!IsValid(productDto)) // { // sb.AppendLine("Invalid Input."); // continue; // } // var product = Mapper.Map<Product>(productDto); // Category[] categories = context.Categories.ToArray(); // int categoryCount = categories.Length; // for (int i = 0; i < 3; i++) // { // int index = rnd.Next(0, categoryCount ); // var categoryId = categories[index].Id; // if (product.CategoryProducts.Any(x=>x.CategoryId==categoryId)) // { // continue; // } // product.CategoryProducts.Add(new CategoryProduct // { // CategoryId = categoryId // }); // } // validProducts.Add(product); // sb.AppendLine($"Product{product.Name} was added."); //} context.Products.AddRange(validProducts); context.SaveChanges(); var result = sb.ToString().TrimEnd(); return(result); }