Beispiel #1
0
        public static void Add()
        {
            Header();
            Console.WriteLine("Add a Book ========================= \n");
            Console.WriteLine("To Add a Book, press <Enter> to proceed, or press <Escape> to return to the Main Menu.  \n");

            //read user key from console
            ConsoleKeyInfo cki;

            cki = Console.ReadKey(true);
            if (cki.Key != ConsoleKey.Escape && cki.Key == ConsoleKey.Enter)
            {
                Console.Clear();
                Console.WriteLine("==== Add a Book ==== \n");

                do
                {
                    Console.WriteLine("Please enter the following information: \n");
                    Console.Write("Title:");
                    string title = Console.ReadLine();

                    Console.Write("Author:");
                    string author = Console.ReadLine();

                    Console.Write("Summary:");
                    string summary = Console.ReadLine();


                    using var libContext = new LibContext();
                    var std = new Book()
                    {
                        title   = title,
                        author  = author,
                        summary = summary
                    };
                    libContext.Books.Add(std);

                    libContext.SaveChanges();
                    Console.WriteLine($"Book[{std.ID}] saved.\n");
                    ViewAll();
                    break;
                }while (true);
            }
        }
Beispiel #2
0
        public static void Edit()
        {
            Console.WriteLine("Edit a Book ======================== \n");


            var libContext    = new LibContext();
            var entireLibrary = libContext.Books
                                .ToList();

            var count = entireLibrary.Count();

            if (count > 0)
            {
                foreach (var book in entireLibrary)
                {
                    Console.WriteLine("[" + book.ID + "]" + book.title + "\n");
                }
                Console.WriteLine("To edit the details of a Book, enter the Book's ID number; to return to the Main Menu press <Enter>.");
                Console.Write("Enter ID Number:");
            }
            else
            {
                Console.WriteLine("Nothing found in the Library.\n");
                Console.WriteLine("To Add an Item, press <Enter> to return to the Main Menu and select Add a Book.\n");
            }


            if (count > 0)
            {
                while (true)
                {
                    var editBookIdInput = Console.ReadLine();
                    if (int.TryParse(editBookIdInput, out int id))
                    {
                        var editBookById = libContext.Books
                                           .Where(b => b.ID == id)
                                           .Single();

                        Console.WriteLine("\nID:" + editBookById.ID + "\n");
                        Console.WriteLine("Input the following information. To leave a field unchanged, hit <Enter> ");

                        Console.Write($"Title[{editBookById.title}]:");
                        var titleEdit = Console.ReadLine();
                        Console.Write($"Author[{editBookById.author}]:");
                        var authorEdit = Console.ReadLine();
                        Console.Write($"Summary[{editBookById.summary}]:");
                        var summaryEdit = Console.ReadLine();

                        if (!String.IsNullOrEmpty(titleEdit))
                        {
                            editBookById.title = titleEdit; //check if title is empty, if not, save, else skip
                            libContext.SaveChanges();
                        }

                        if (!String.IsNullOrEmpty(authorEdit))
                        {
                            editBookById.author = authorEdit; //check if author is empty, if not, save, else skip
                            libContext.SaveChanges();
                        }

                        if (!String.IsNullOrEmpty(summaryEdit))
                        {
                            editBookById.summary = summaryEdit;
                            libContext.SaveChanges(); //check if summary is empty, if not, save, else skip
                        }

                        Console.WriteLine("Book Saved.");
                        Console.WriteLine("To view details enter the book ID, to return to the Main Menu press <Enter>.");
                        Console.Write("Enter ID Number:");
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }