public ActionResult Return(int id, int bookId) { PatronBook joinEntry = db.PatronsBooks.FirstOrDefault(entry => entry.BookId == bookId); db.PatronsBooks.Remove(joinEntry); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Checkout(int id, List <int> BookIds) { foreach (var bookId in BookIds) { Book book = db.Books.FirstOrDefault(bookItem => bookItem.BookId == bookId); PatronBook newPatronBook = new PatronBook(id, book.BookId); db.PatronsBooks.Add(newPatronBook); } db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Checkout(Patron patron, List <int> BookIds) { // Add PatronBook entries based on BookId. foreach (var id in BookIds) { Book book = db.Books.FirstOrDefault(_ => _.BookId == id); PatronBook newPatronBook = new PatronBook(patron.PatronId, book.BookId); db.PatronsBooks.Add(newPatronBook); } db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Delete(int id) { Patron patron = db.Patrons.FirstOrDefault(patrons => patrons.PatronId == id); PatronBook joinEntry = db.PatronsBooks.FirstOrDefault(entry => entry.PatronId == id); db.Patrons.Remove(patron); if (joinEntry != null) { db.PatronsBooks.Remove(joinEntry); } db.SaveChanges(); return(RedirectToAction("Index")); }
public Book GetSingleBookStatus(int id) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = @"SELECT Book.Id, Book.Title, Book.Author, Patron.FirstName, Patron.LastName FROM Book JOIN PatronBook ON PatronBook.BookId = Book.Id JOIN Patron ON PatronBook.PatronId = Patron.Id WHERE Book.Id=@id"; cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader reader = cmd.ExecuteReader(); Book book = new Book(); while (reader.Read()) { // If we haven't already assigned the book's info, assign the book's info // This conditional should only be true on the first row of data that comes back if (book.Id == 0) { book.Id = reader.GetInt32(reader.GetOrdinal("Id")); book.Title = reader.GetString(reader.GetOrdinal("Title")); book.Author = reader.GetString(reader.GetOrdinal("Author")); } ; // If multiple patrons have checked out the book, we want to create a new instance of PatronBook for each one and add their first and last name PatronBook currentPatron = new PatronBook { Patron = new Patron() { FirstName = reader.GetString(reader.GetOrdinal("FirstName")), LastName = reader.GetString(reader.GetOrdinal("LastName")) } }; // Then we add the new instance to the book's list of patrons book.Patrons.Add(currentPatron); } reader.Close(); return(book); } } }
public void CheckOutBook(PatronBook patronBook) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { // Insert a new entry into the join table based on the PatronBook instance we passed in cmd.CommandText = $"INSERT INTO PatronBook (BookId, PatronId) Values ({patronBook.BookId}, {patronBook.PatronId})"; // Execute the command-- create the thing! cmd.ExecuteNonQuery(); } } }
public static void CollectInput() { // List all the possible books Console.WriteLine("Which book would you like to check out?"); BookRepository bookRepo = new BookRepository(); List <Book> allBooks = bookRepo.GetAllBooks(); foreach (Book singleBook in allBooks) { Console.WriteLine($"{singleBook.Title} - {singleBook.Id}"); } Console.WriteLine(@"Please enter the unique id of the book you want to check out."); // Collect the book id int bookId = Int32.Parse(Console.ReadLine()); // List all the possible patrons Console.WriteLine(@"Which customer is checking out this book?"); PatronRepository patronRepo = new PatronRepository(); List <Patron> allPatrons = patronRepo.GetAllPatrons(); foreach (Patron singlePatron in allPatrons) { Console.WriteLine($"{singlePatron.FirstName} {singlePatron.LastName} - {singlePatron.Id}"); } Console.WriteLine(@"Please enter the unique id of the patron."); // Collect the patron Id int patronId = Int32.Parse(Console.ReadLine()); // Create a new instance of a PatronBook join table entry PatronBook checkedOutBook = new PatronBook() { PatronId = patronId, BookId = bookId, }; // Create the new join table entry in the database PatronBookRepository patronBookRepo = new PatronBookRepository(); patronBookRepo.CheckOutBook(checkedOutBook); Console.WriteLine("Congratulations! The book has been checked out."); Program.PrintMainMenu(); }