static void Main(string[] args) { // instantiate an instance of the context BooksContext context = new BooksContext(); // makes sure that the table exists, //and creates it if it does not already exist context.Database.EnsureCreated(); // ask the user for a book to add Console.WriteLine("Enter the full Author and Title of the Book (sperated by , ex. The Martian, Andy Weir)"); String fullBook = Console.ReadLine(); // split the input into parts, and make sure // we have 2 parts only String[] parts = fullBook.Split(','); if (parts.Length >= 2) { // create a new book object, notce that we do not // select an id, we let the framework handle that Books newBook = new Books(parts[0], parts[1]); // add the newly created book instance to the context // notice how similar this is to adding a item to a list, context.books.Add(newBook); // ask the context to save any changes to the databse context.SaveChanges(); Console.WriteLine("Added the Book."); } else { Console.WriteLine("Invalid full Title and Author, did not add Book"); } Console.WriteLine("The Current List of Books are: "); IEnumerable <Books> bookCollection = context.books.OrderBy(book => book.Title); // use a for each loop to loop through the books in the context // notice how similar this is to looping through a list foreach (Books s in bookCollection) { Console.WriteLine("{0} - {1} |{2}", s.Id, s.Title, s.Author); } }
private static void Main(string[] args) { //instantiate an instance of the context first BooksContext context = new BooksContext(); //Ensures table context //creates one if it doesn't already exist context.Database.EnsureCreated(); Console.WriteLine("Enter in a book title and author separated by a comma, to add in the database."); Console.WriteLine("Example: \"Harry Potter, J.K. Rowling\""); string bookEntry = Console.ReadLine(); //Splits entry into parts string[] parts = bookEntry.Split(", "); if (parts.Length == 2) { //creates new book object book newBook = new book(parts[0], parts[1]); //adds newly created book instance to the context context.books.Add(newBook); //ask context to save any changes to the database context.SaveChanges(); Console.WriteLine("Successfully added the book to the database."); } else { Console.WriteLine("Invalid entry, only input title and author of the book."); } Console.WriteLine("The current list of books are: "); foreach (book b in context.books) { Console.WriteLine("{0} - {1} | {2}", b.Id, b.Title, b.Author); } }
static void Main(string[] args) { context.Database.EnsureCreated(); bool entering = true; while (entering) { Console.Clear(); Console.WriteLine($"Please enter an option between 1-5:\n1: To Add a Book\n2: To Update a Book\n3: To Remove a Book\n4: To see Book inventory List:\n5: To see a single Book:"); try { int userInput = Convert.ToInt32(Console.ReadLine()); if (userInput < 1 || userInput > 5) { Console.WriteLine($"Invalid selection, please choose between 1-5"); } else { switch (userInput) { case 1: if (entering) { Console.Clear(); context.Print(); Console.WriteLine("Enter the Title:"); string title = Console.ReadLine(); Console.WriteLine("Enter the Author:"); string author = Console.ReadLine(); Books book = new Books(title, author); context.Add(book); context.SaveChanges(); Console.Clear(); } break; case 2: Console.Clear(); Console.WriteLine("Please enter the ID of the Book you would like to update:"); int id = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the new Title of the book you wish to update"); string updateTitle = Console.ReadLine(); Console.WriteLine("Enter the new Author of the book you wish to update"); string updateAuthor = Console.ReadLine(); var updatingBook = GetBook(id); updatingBook.Title = updateTitle; updatingBook.Author = updateAuthor; context.Update(updatingBook); context.SaveChanges(); Console.Clear(); break; case 3: Console.Clear(); Console.WriteLine("Please enter the ID of the Book you wish to Remove:"); int removeID = Convert.ToInt32(Console.ReadLine()); var removingBook = GetBook(removeID); context.Remove(removingBook); context.SaveChanges(); Console.Clear(); break; case 4: Console.Clear(); Console.WriteLine("Here's a list of books in the inventory"); context.Print(); break; case 5: Console.Clear(); Console.WriteLine("Please enter the ID of the Book you wish to read:"); int singleBookId = Convert.ToInt32(Console.ReadLine()); context.Print(GetBook(singleBookId)); break; } } } catch (Exception ex) { Console.WriteLine($"You entered invalid data: {ex.Message}"); Console.WriteLine($"{ex.StackTrace}"); } Console.WriteLine("Would you like to Add, Update or Remove any more Books? Y/N:"); string endLoop = Console.ReadLine().ToUpper(); if (endLoop == "Y") { continue; } else if (endLoop == "N") { entering = false; } } Console.ReadKey(); }
static void Main(string[] args) { //variable to determine when the user is done entering books bool done = false; //instantiate an instance of the context BooksContext library = new BooksContext(); //make sure that the table exists, and create it if it does not already exist. library.Database.EnsureCreated(); while (!done) { //ask the user for a book to add Console.WriteLine("Do you want to 'review' the books, 'add' a book, 'update' a book, or 'delete' a book? Type 'done' when finished."); string action = Console.ReadLine(); if (action.ToLower() != "done") { if (action.ToLower() == "review" || action.ToLower() == "update" || action.ToLower() == "delete") { //display a listing of the books in the library BooksContext.ReviewBooks(library.Books); //what to do next if the user wants to update a book if (action.ToLower() == "update") { //ask which book the user wishes to update Console.WriteLine("Enter the ID of the book to update."); string bookID = Console.ReadLine(); Book UpdatedBook = library.Books.Where(x => x.ID == int.Parse(bookID)).FirstOrDefault(); library.Update(BooksContext.GetBook(action, UpdatedBook)); library.SaveChanges(); BooksContext.ReviewBooks(library.Books); } //what to do next if the user wants to delete a book else if (action.ToLower() == "delete") { string verify = "NO"; string bookID = ""; while (verify == "NO") { //ask which book the user wishes to delete Console.WriteLine("Enter the ID of the book to delete or type 'CANCEL'."); bookID = Console.ReadLine(); if (bookID == "CANCEL") { break; } Console.WriteLine("You have chosen to delete book " + bookID + "."); Console.WriteLine("Is the correct? YES or NO?"); verify = Console.ReadLine(); } if (bookID != "CANCEL") { Book DeleteBook = library.Books.Where(x => x.ID == int.Parse(bookID)).FirstOrDefault(); library.Remove(DeleteBook); library.SaveChanges(); BooksContext.ReviewBooks(library.Books); } } } else if (action.ToLower() == "add") { //call method to get the book object to add. //add the newly created book instance to the context. //notice how similar this is to adding an item to a list. library.Add(BooksContext.GetBook(action, null)); //ask the context to save any changes to the database library.SaveChanges(); //use a foreach loop to loop through the students in the context //notice how similar this is to looping through a list BooksContext.ReviewBooks(library.Books); } } else { done = true; } } }