Пример #1
0
        public static void ViewAll()
        {
            Console.WriteLine("==== View Books by ID and Title ==== \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 view details enter the book ID, to return 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 bookIdInput = Console.ReadLine();
                    if (int.TryParse(bookIdInput, out int id))
                    {
                        var bookById = libContext.Books
                                       .Where(b => b.ID == id)
                                       .ToList();

                        foreach (var book in bookById)
                        {
                            Console.WriteLine("\nID:" + book.ID);
                            Console.WriteLine($"Title: {book.title}");
                            Console.WriteLine($"Author: {book.author}");
                            Console.WriteLine($"Summary: {book.summary}" + "\n");
                        }
                        Console.WriteLine("To view details enter the book ID, to return to the Main Menu press <Enter>.");
                        Console.Write("Enter ID Number:");
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Пример #2
0
        public static void Search()
        {
            Console.WriteLine("Search the Library ================= \n");
            Console.Write("Search:");
            string searchString = Console.ReadLine();

            var libContext   = new LibContext();
            var matchedItems = libContext.Books
                               .Where(b => b.title.Contains(searchString))
                               .ToList();

            var matchCount = matchedItems.Count();

            if (matchCount > 0)
            {
                Console.WriteLine("The following items matched your query. Enter the book ID to see more details, or <Enter> to return to the Main Menu.");
                foreach (var book in matchedItems)
                {
                    Console.WriteLine($"[{book.ID}]{book.title}");
                }
                Console.Write("Enter ID:");

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


                        Console.WriteLine("\nID:" + bookById.ID);
                        Console.WriteLine($"Title: {bookById.title}");
                        Console.WriteLine($"Author: {bookById.author}");
                        Console.WriteLine($"Summary: {bookById.summary}" + "\n");

                        Console.Write("Enter ID:");
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            {
                Console.WriteLine($"Nothing was found for the search term {searchString}");
                Console.Write("Press <Enter> to return to the Main Menu.");
                Console.ReadKey();
            }
        }
Пример #3
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);
            }
        }
Пример #4
0
        public static void WriteToFile()
        {
            string filePath      = @"C:\tmp\library.csv";
            var    libContext    = new LibContext();
            var    entireLibrary = libContext.Books
                                   .ToList();

            var count = entireLibrary.Count();

            if (count > 0)
            {
                using (var stream = new StreamWriter(filePath, false))
                {
                    foreach (var item in entireLibrary)
                    {
                        string row = string.Format("{0},{1},{2}, {3}", item.ID, item.title, item.author, item.summary);
                        stream.WriteLine(row);
                    }
                }

                Console.Write("Library Saved. Press <Enter>");
                Console.ReadKey();
            }
        }
Пример #5
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;
                    }
                }
            }
        }