private static void Task3() { var context = new AlbumDatabaseContext(); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); using (context) { var categoryInfo = context.Categories .Select(x => new CategoriesByProductsCountDto() { Name = x.Name, ProductsCount = x.categoryProducts.Count(), AveragePrice = x.categoryProducts.Select(e => e.products.Price) .DefaultIfEmpty(0).Average(), TotalRevenue = x.categoryProducts.Sum(e => e.products.Price) }) .OrderByDescending(x => x.ProductsCount).ToArray(); var serializer = new XmlSerializer(typeof(CategoriesByProductsCountDto[]), new XmlRootAttribute("categories")); var usersFile = new StreamWriter("../../../categoriesProducts.xml"); using (usersFile) { serializer.Serialize(usersFile, categoryInfo, namespaces); } } }
private static void Task2() { var context = new AlbumDatabaseContext(); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); using (context) { var users = context.Users.Include(x => x.Sold) .Where(x => x.Sold.Count > 1) .OrderBy(x => x.LastName).ThenBy(x => x.FirstName) .Select(x => new UsersSoldDto { FirstName = x.FirstName, LastName = x.LastName, productSolds = x.Sold.Select(e => new ProductSoldDto { Name = e.Name, Price = e.Price }).ToArray() }).ToArray(); var serializer = new XmlSerializer(typeof(UsersSoldDto[]), new XmlRootAttribute("users")); var usersFile = new StreamWriter("../../../usersSoldItems"); using (usersFile) { serializer.Serialize(usersFile, users, namespaces); } } }
private static void Task1() { var context = new AlbumDatabaseContext(); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); using (context) { var products = context.Products.Include(x => x.Bought) .Where(x => x.Price >= 1000 && x.Price <= 2000 && x.Bought != null).OrderBy(x => x.Price) .Select(x => new ProductInRangeDto { Name = x.Name, Price = x.Price.ToString(), BuyerName = x.Bought.FirstName + " " + x.Bought.LastName ?? " " + x.Bought.LastName }) .ToArray(); var serializer = new XmlSerializer(typeof(ProductInRangeDto[]), new XmlRootAttribute("products")); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), products, namespaces); File.WriteAllText("../../../products-in-range.xml", sb.ToString()); } }
private static void Task4() { var context = new AlbumDatabaseContext(); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); using (context) { var users = new UserProductRootDto() { Count = context.Users.Count(), Users = context.Users .Where(x => x.Sold.Count >= 1).Select(x => new UserDto() { Firstname = x.FirstName, Lastname = x.LastName, Age = x.Age.ToString(), soldProducts = new SoldProductsDto { Count = x.Sold.Count(), Products = x.Sold.Select(e => new ProductDto { Name = e.Name, Price = e.Price }).ToArray() } }).OrderByDescending(x => x.soldProducts.Count).ToArray() }; var serializer = new XmlSerializer(typeof(UserProductRootDto)); var usersFile = new StreamWriter("../../../soldProducts1.xml"); using (usersFile) { serializer.Serialize(usersFile, users, namespaces); } } }
static void Main(string[] args) { Console.WriteLine(); Console.WriteLine($"Welcome to my record label's interactive client catalogue. Please select one of the following options:"); var userHasChosenToQuit = false; while (userHasChosenToQuit == false) { Console.WriteLine(); Console.WriteLine("Choose:"); Console.WriteLine("(A)dd a new band"); Console.WriteLine("(V)iew all the bands"); Console.WriteLine("A(d)d an album for a band"); Console.WriteLine("(L)et a band go"); Console.WriteLine("(R)esign a band"); Console.WriteLine("(E)nter a band's name to view all its albums"); Console.WriteLine("V(i)ew all albums ordered by release date"); Console.WriteLine("Vie(w) all bands that are signed"); Console.WriteLine("View all (b)ands that are not signed"); Console.WriteLine(); var choice = PromptForString("Choice: "); Console.WriteLine(); if (choice == "Q") { userHasChosenToQuit = true; } if (choice == "A") { var context = new BandDatabaseContext(); var newName = PromptForString("Name: "); var newCountryofOrigin = PromptForString("Country of Origin: "); var newNumberOfMembers = PromptForInteger("Number of Members: "); var newWeb = PromptForString("Email Address: "); var newStyle = PromptForString("Musical Style: "); var newIsSigned = PromptForBool("Is the band signed? (Answer as true or false): "); var newContactName = PromptForString("Name of Band Contact: "); var newContactPhone = PromptForString("Band Contact Phone Number: "); var newBandItem = new BandItem { Name = newName, CountryOfOrigin = newCountryofOrigin, NumberOfMembers = newNumberOfMembers, Web = newWeb, Style = newStyle, IsSigned = newIsSigned, ContactName = newContactName, ContactPhone = newContactPhone, }; context.Band.Add(newBandItem); context.SaveChanges(); } if (choice == "V") { var context = new BandDatabaseContext(); var allTheBands = context.Band; foreach (var band in allTheBands) { Console.WriteLine($"Band ID Number: {band.Id}, Name: {band.Name}, Country of Origin: {band.CountryOfOrigin}, Number of Members: {band.NumberOfMembers}, Email: {band.Web}, Signed?: {band.IsSigned}, Contact Name: {band.ContactName}, Contact Phone Number: {band.ContactPhone}"); } } if (choice == "d") { var context = new AlbumDatabaseContext(); var newTitle = PromptForString("Title: "); var newIsExplicit = PromptForBool("Is the album explicit? (Answer as true or false): "); var newReleaseDate = PromptForDateTime("What is the Album's release date in the format of MM/DD/YYYY?: "); var newBandId = PromptForInteger("What is the ID number for the band that recorded this album: "); var newAlbumItem = new AlbumItem { Title = newTitle, IsExplicit = newIsExplicit, ReleaseDate = newReleaseDate, BandId = newBandId }; context.Album.Add(newAlbumItem); context.SaveChanges(); } if (choice == "L") { var context = new BandDatabaseContext(); var bandWithNewSignedStatus = PromptForString("Which band would you like to select? "); var retrieveBandItem = context.Band.FirstOrDefault(entry => entry.Name == $"{bandWithNewSignedStatus}"); if (retrieveBandItem == null) { Console.WriteLine("Sorry, that band does not exist in this database."); } else { retrieveBandItem.IsSigned = false; context.SaveChanges(); } } if (choice == "R") { var context = new BandDatabaseContext(); var bandWithNewSignedStatus = PromptForString("Which band would you like to select? "); var retrieveBandItem = context.Band.FirstOrDefault(entry => entry.Name == $"{bandWithNewSignedStatus}"); if (retrieveBandItem == null) { Console.WriteLine("Sorry, that band does not exist in this database."); } else { retrieveBandItem.IsSigned = true; context.SaveChanges(); } } if (choice == "E") { var bandContext = new BandDatabaseContext(); var albumContext = new AlbumDatabaseContext(); var bandToSearchFor = PromptForString("Enter the name of the band you'd like to search for? "); var retrieveBandItem = bandContext.Band.FirstOrDefault(entry => entry.Name == $"{bandToSearchFor}"); if (retrieveBandItem == null) { Console.WriteLine("Sorry, that band does not exist in this database."); } else { var findAlbums = albumContext.Album.Where(entry => entry.BandId == retrieveBandItem.Id); foreach (var album in findAlbums) { Console.WriteLine(); Console.WriteLine($"Title: {album.Title}, Is Explicit?: {album.IsExplicit}, Release Date: {album.ReleaseDate}"); } } } if (choice == "i") { var context = new AlbumDatabaseContext(); var allTheAlbums = context.Album.OrderBy(entry => entry.ReleaseDate); foreach (var album in allTheAlbums) { Console.WriteLine($"Album Title: {album.Title}, Release Date: {album.ReleaseDate}"); } } if (choice == "w") { var context = new BandDatabaseContext(); var allTheBands = context.Band.Where(band => band.IsSigned == true); foreach (var band in allTheBands) { Console.WriteLine(band.Name); } } if (choice == "b") { var context = new BandDatabaseContext(); var allTheBands = context.Band.Where(band => band.IsSigned == false); foreach (var band in allTheBands) { Console.WriteLine(band.Name); } } } }
static void Main(string[] args) { List <Users> users = new List <Users>(); List <Categories> categories = new List <Categories>(); List <Products> products = new List <Products>(); string readstring = File.ReadAllText("users.xml"); string categorystring = File.ReadAllText("categories.xml"); string productstring = File.ReadAllText("products.xml"); // var serializer1 = new XmlSerializer(typeof(UserDto[]),new XmlRootAttribute("users")); var serializer2 = new XmlSerializer(typeof(CategoriesDto[]), new XmlRootAttribute("categories")); var serializer = new XmlSerializer(typeof(ProductsDto[]), new XmlRootAttribute("products")); // var desirializer1 = (UserDto[])serializer1.Deserialize(new StringReader(readstring)); var desirializer2 = (CategoriesDto[])serializer2.Deserialize(new StringReader(categorystring)); var desirializer = (ProductsDto[])serializer.Deserialize(new StringReader(productstring)); var config = new MapperConfiguration(cfg => { cfg.AddProfile <AutoMapperProfile>(); cfg.CreateMap <UserDto, Users>(); cfg.CreateMap <CategoriesDto, Categories>(); cfg.CreateMap <ProductsDto, Products>(); }); var mapper = config.CreateMapper(); /* foreach (var user in desirializer1) * { * * bool Valid = isValid(user); * * if (Valid == false) * { * continue; * } * * var currentUser = mapper.Map<Users>(user); * * users.Add(currentUser); * } * * foreach (var category in desirializer2) * { * bool Valid = isValid(category); * * if (Valid == false) * { * continue; * } * * var currentCategory = mapper.Map<Categories>(category); * * * categories.Add(currentCategory); * } * */ int counter = 1; foreach (var product in desirializer) { bool Valid = isValid(product); if (Valid == false) { continue; } var currentproduct = mapper.Map <Products>(product); int boughtId = new Random().Next(57, 87); int soldId = new Random().Next(87, 112); currentproduct.Sellerid = soldId; currentproduct.BuyerId = boughtId; if (counter == 4) { currentproduct.BuyerId = null; counter = 1; } counter++; products.Add(currentproduct); } var context = new AlbumDatabaseContext(); List <CategoryProducts> categoryProducts = new List <CategoryProducts>(); for (int i = 0; i < 11; i++) { int categoryID = new Random().Next(1, 10); int productID = new Random().Next(3662, 3697); var currentCattegoryProducts = new CategoryProducts() { CategoryId = categoryID, ProductId = productID }; categoryProducts.Add(currentCattegoryProducts); } using (context) { // context.Users.AddRange(users); // context.Products.AddRange(products); // context.Categories.AddRange(categories); context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); } }