public static string CategoriesByProductsCount(ProductShoContext context) { var categories = context.Categories .Select(c => new CategoriesByProductCountExportDto { Name = c.Name, ProductCount = c.CategoryProducts.Count(), AveragePrice = c.CategoryProducts.Average(cp => cp.Product.Price), TotalRevenue = c.CategoryProducts .Where(cp => cp.Product.Buyer != null) .Sum(cp => cp.Product.Price) }) .OrderBy(c => c.ProductCount) .ToArray(); var serializer = new XmlSerializer(typeof(CategoriesByProductCountExportDto[]), new XmlRootAttribute("categories")); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), categories, namespaces); var result = sb.ToString(); return(result); }
public static string UsersSoldProducts(ProductShoContext context) { var usersSoldProductsDtos = context.Users .Where(u => u.Sold.Count > 0) .Select(u => new UserSoldProductExportDto { FirstName = u.FirstName, LastName = u.LastName, SoldProducts = u.Sold .Select(s => new ProductExportDto { Name = s.Name, Price = s.Price }) .ToArray() }) .ToArray(); var serializer = new XmlSerializer(typeof(UserSoldProductExportDto[]), new XmlRootAttribute("users")); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), usersSoldProductsDtos, namespaces); var result = sb.ToString(); return(result); }
public static string ProductsInRange(ProductShoContext context, decimal minPrice, decimal maxPrace) { var products = context.Products .Where(p => p.Price >= minPrice && p.Price <= maxPrace && p.Buyer != null ) .Select(p => new ProductInRangeExportDto { Name = p.Name, Price = p.Price, BuyerFullName = (p.Buyer.FirstName ?? string.Empty) + " " + p.Buyer.LastName }) .OrderBy(p => p.Price) .ToArray(); var serializer = new XmlSerializer(typeof(ProductInRangeExportDto[]), new XmlRootAttribute("products")); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), products, namespaces); var result = sb.ToString(); return(result); }
public static Product[] ImportProducts(ProductShoContext context, User[] users, string xmlString) { var serializer = new XmlSerializer(typeof(ProductImportDto[]), new XmlRootAttribute("products")); var productDtos = (ProductImportDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString))); Random random = new Random(); Product[] products = new Product[productDtos.Length]; for (int i = 0; i < productDtos.Length; i++) { products[i] = new Product { Name = productDtos[i].Name, Price = productDtos[i].Price, }; products[i].Seller = users[random.Next(users.Length - 1)]; int buyerIndex = random.Next(users.Length * 2); if (buyerIndex >= users.Length) { products[i].Buyer = null; } else { products[i].Buyer = users[buyerIndex]; } } return(products); }
public static void Main(string[] args) { var context = new ProductShoContext(); context.Database.EnsureDeleted(); context.Database.EnsureCreated(); ImportData(context); ExportData(context); }
private static void ImportData(ProductShoContext context, string baseDir = @"..\..\..\Resources\") { User[] users = DataProcessor.Deserializer.ImportUsers(context, File.ReadAllText(baseDir + "users.xml")); Product[] products = DataProcessor.Deserializer.ImportProducts(context, users, File.ReadAllText(baseDir + "products.xml")); Category[] categories = DataProcessor.Deserializer.ImportCategories(context, File.ReadAllText(baseDir + "categories.xml")); CategoryProducts[] categoryProducts = GenerateCategoryProducts(products, categories); context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); }
public static Category[] ImportCategories(ProductShoContext context, string xmlString) { var serializer = new XmlSerializer(typeof(CategoryImportDto[]), new XmlRootAttribute("categories")); var categoryDtos = (CategoryImportDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString))); Category[] categories = categoryDtos .Select(c => new Category { Name = c.Name }) .ToArray(); return(categories); }
private static void ExportData(ProductShoContext context, string baseDir = @"..\..\..\Results\") { string productsInRangeString = DataProcessor.Serializer.ProductsInRange(context, 1000, 2000); File.WriteAllText(baseDir + "products-in-range.xml", productsInRangeString); string usersSoldProducts = DataProcessor.Serializer.UsersSoldProducts(context); File.WriteAllText(baseDir + "users-sold-products.xml", usersSoldProducts); string categoriesByProductsCountString = DataProcessor.Serializer.CategoriesByProductsCount(context); File.WriteAllText(baseDir + "categories-by-products.xml", categoriesByProductsCountString); string usersAndProducts = DataProcessor.Serializer.UsersAndProducts(context); File.WriteAllText(baseDir + "users-and-products.xml", usersAndProducts); }
public static User[] ImportUsers(ProductShoContext context, string xmlString) { var serializer = new XmlSerializer(typeof(UserImportDto[]), new XmlRootAttribute("users")); var userDtos = (UserImportDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString))); User[] users = new User[userDtos.Length]; for (int i = 0; i < users.Length; i++) { users[i] = new User { FirstName = userDtos[i].FirstName, LastName = userDtos[i].LastName, Age = userDtos[i].Age, }; } return(users); }
public static string UsersAndProducts(ProductShoContext context) { var usersQuery = context.Users.Where(uc => uc.Sold.Count > 0); var users = new UserExportDto { Count = usersQuery.Count(), Users = usersQuery .Select(uc => new UserAndProductsDto { FirstName = uc.FirstName, LastName = uc.LastName, Age = uc.Age, SoldProducts = new SoldProductsExportDto { Count = uc.Sold.Count, Products = uc.Sold .Select(sp => new ProductByUserExportDto { Name = sp.Name, Price = sp.Price }) .ToArray() } }) .ToArray(), }; var serializer = new XmlSerializer(typeof(UserExportDto)); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), users, namespaces); var result = sb.ToString(); return(result); }