public static int BorrowBook(int id, string emailAddress) { if (string.IsNullOrEmpty(emailAddress)) { throw new ArgumentNullException("You need to be logged in to borrow a book!"); } using (var model = new LibraryModel()) { var account = model.Accounts.Where(a => a.EmailAddless == emailAddress).FirstOrDefault(); if (account == null) { account = new Account(); account.EmailAddless = emailAddress; model.Accounts.Add(account); model.SaveChanges(); } var book = model.Books.Where(book => book.ISBN == id).FirstOrDefault(); if (book == null) { throw new ArgumentException("Book not found!"); } book.Count--; var rental = new Rental(); rental.AccountId = account.AccountId; rental.RentalType = RentalTypes.Book; rental.Id = id; model.Rentals.Add(rental); model.SaveChanges(); return rental.RentalId; } }
public static List <Book> GetBooks() { using (var model = new LibraryModel()) { var books = model.Books.Include(b => b.Author); return(books.ToList()); } }
public static void AddBook(Book book) { using (var model = new LibraryModel()) { model.Books.Add(book); model.SaveChanges(); } }
public static List <Book> GetBooks() { ///connect to db using (var model = new LibraryModel()) { var books = model.Books.Include("Author"); return(books.ToList <Book>()); } }
//delete book property and constructor #region Methods public static void AddBook(Book book) { using (var model = new LibraryModel())//open a connection to the database { //model is the connection model.Books.Add(book); //go to the database find the table Books and add a book model.SaveChanges(); //save the changes } //using is for closing connection }
public static List<Book> GetBooks() { using (var model = new LibraryModel()) { //var books = model.Books.Include(b => b.Author); var books = model.Books.Include("Author").Where(b => b.Count > 0); return books.ToList<Book>(); } }
/// <summary> /// data type: collections of books, list is a collection type /// </summary> /// public static List<Book> Books { get; set; } #endregion #region Methods public static void AddBook(Book book) { /// open new connection /// using statement closes connection using (var model = new LibraryModel()) { model.Books.Add(book); model.SaveChanges(); } }
public static void PrintBooks() { using (var model = new LibraryModel()) { foreach (var book in model.Books) // var is a variable { Console.WriteLine("Title: {0}, ISBN: {1}, Price: {2}, Published: {3}", // {} are place holders book.Title, book.ISBN, book.Price, book.PublishedYear); // things to fill in place holders } } }
public static List<Book> GetMyRentals(string emailAddress) { var model = new LibraryModel() var account = model.Accounts.Where(a => a.EmailAddless == emailAddress).FirstOrDefault(); if (account == null) { throw new ArgumentException("User account not found "); } var books = new List<Book>(); var rentals = model.Rentals.Where(r => r.AccountId == account.AccountId); foreach (var rental in rentals) { books.Add(model.Books.Where(b => b.ISBN == rental.Id).FirstOrDefault(); } }
public static int BorrowBook(int id, string emailAddress) { using (var model = new LibraryModel()) { var account = model.Accounts.Where(a => a.EmailAddress == emailAddress).FirstOrDefault(); if (account == null) { account = new Account(); account.EmailAddress = emailAddress; model.Accounts.Add(account); model.SaveChanges(); } var rental = new Rental(); rental.AccountId = account.AccountId; rental.RentalType = Rentaltypes.Book; rental.Id = id; model.Rentals.Add(rental); model.SaveChanges(); return(rental.RentalId); } }