示例#1
0
        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;

            }
        }
示例#2
0
 public static List <Book> GetBooks()
 {
     using (var model = new LibraryModel())
     {
         var books = model.Books.Include(b => b.Author);
         return(books.ToList());
     }
 }
示例#3
0
 public static void AddBook(Book book)
 {
     using (var model = new LibraryModel())
     {
         model.Books.Add(book);
         model.SaveChanges();
     }
 }
示例#4
0
 public static List <Book> GetBooks()
 {
     ///connect to db
     using (var model = new LibraryModel())
     {
         var books = model.Books.Include("Author");
         return(books.ToList <Book>());
     }
 }
示例#5
0
 //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
 }
示例#6
0
        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>();

            }
        }
示例#7
0
        /// <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();
            }
        }
示例#8
0
 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
         }
     }
 }
示例#9
0
 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();
     }
 }
示例#10
0
 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);
     }
 }