コード例 #1
0
        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();
            }
        }