/// <summary>
 /// The create book.
 /// </summary>
 /// <param name="book">
 /// The book.
 /// </param>
 /// <returns>
 /// The <see cref="Book"/>.
 /// </returns>
 public Book CreateBook(Book book)
 {
     // TODO Check exist Category
     // BUG new book added to dbo.Book even without category. so BookCategory is empty
     Book createdBook = this.bookDalManager.CreateBook(book);
     return createdBook;
 }
        /// <summary>
        /// The create book.
        /// </summary>
        /// <param name="book">
        /// The book.
        /// </param>
        /// <returns>
        /// The <see cref="Book"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// </exception>
        public Book CreateBook(Book book)
        {
            int categoryID = this.categoryDalManager.GetCategoryIDByCategoryName(book.Category);

            SqlParameter[] sqlCategoryParameters = new SqlParameter[2];
            sqlCategoryParameters[0] = new SqlParameter("@CategoryID", SqlDbType.Int) { Value = categoryID };

            SqlParameter[] sqlParameters = new SqlParameter[3];
            sqlParameters[0] = new SqlParameter("@Title", SqlDbType.VarChar) { Value = book.Title };
            sqlParameters[1] = new SqlParameter("@Author", SqlDbType.VarChar) { Value = book.Author };

            sqlParameters[2] = new SqlParameter("@QuantityOfBooksIssued", SqlDbType.Int)
                                   {
                                       Value =
                                           book
                                           .QuantityOfBooksIssued
                                   };

            try
            {
                book.BookID = this.sqlDalManager.InsertProcedureWithOutputInsertedId("CreateBook", sqlParameters);
                sqlCategoryParameters[1] = new SqlParameter("@BookID", SqlDbType.Int) { Value = book.BookID };
                this.sqlDalManager.InsertProcedure("BindingBookWithCategory", sqlCategoryParameters);
            }
            catch (Exception exception)
            {
                throw exception;
            }

            return book;
        }
 /// <summary>
 /// The delete book.
 /// </summary>
 /// <param name="book">
 /// The book.
 /// </param>
 public void DeleteBook(Book book, int countRemove)
 {
     if (countRemove > book.QuantityOfBooksIssued)
     {
         //TODO Exception base
         throw new Exception("You try to remove more books than available");
     }
     else
     {
         this.bookDalManager.DeleteBook(book.BookID, countRemove);
     }
   
 }
        public void GetCategoryNameByBookIDBasicTest()
        {
            Book book = new Book();
            book.Title = "The Lean Startup";
            book.Author = "Eric Ries";
            book.Category = "Business";
            book.QuantityOfBooksIssued = 3;

            Book newBook = bookDalManager.CreateBook(book);

            string categoryName = bookDalManager.GetCategoryNameByBookID(newBook.BookID);
            Assert.That(book.Category, Is.EqualTo(categoryName));
        }
        public void CreateBookBasicTest()
        {
            // Book book = new Book();
            // book.Title = "Dune";
            // book.Author = "Frank Herbert";
            // book.Category = "Fiction";
            // book.QuantityOfBooksIssued = 7;
            Book book = new Book();
            book.Title = "Neuromancer";
            book.Author = "William Gibson";
            book.Category = "Fiction";
            book.QuantityOfBooksIssued = 10;

            Book newBook = bookDalManager.CreateBook(book);

            Assert.That(newBook.BookID, Is.Not.EqualTo(0));

            Console.WriteLine("BookID = " + newBook.BookID);
        }
        /// <summary>
        /// The issue book.
        /// </summary>
        /// <param name="book">
        /// The book.
        /// </param>
        /// <param name="countOfBorrowedBook">
        /// The count of issued books.
        /// </param>
        /// <exception cref="Exception">
        /// </exception>
        public void IssueBook(Book book, int countOfBorrowedBook)
        {
            // we borrow that count of books, so we subtract from  total number of Books Issued
            countOfBorrowedBook = -countOfBorrowedBook;

            // TODO GetCurrentUser session
            int currentUserID = 1;
         //   currentUserID = int.TryParse(System.Windows.Application.Current.Properties["UserID"]);
           
            if (book.QuantityOfBooksIssued == 0)
            {
                throw new Exception("Current book are not available.");
            }

            this.bookDalManager.CreateIssueBook(book.BookID, currentUserID);

            // UPDATE QuantityOfBooksIssued in Book
            bool isUpdate = this.bookDalManager.UpdateQuantityOfBooksIssued(book.BookID, countOfBorrowedBook);
            if (!isUpdate)
            {
                throw new Exception("Quantity Of Books didn't update");
            }
        }
        /// <summary>
        /// The return book.
        /// </summary>
        /// <param name="book">
        /// The book.
        /// </param>
        /// <param name="countOfReturnedBooks">
        /// The count Of Returned Books.
        /// </param>
        public void ReturnBook(Book book, int countOfReturnedBooks)
        {
            // TODO GetCurrentUser session
            int currentUserID = 1;

            // we return that count of books, so we add count to total number of Books Issued
            // UPDATE QuantityOfBooksIssued in Book
            this.bookDalManager.UpdateQuantityOfBooksIssued(book.BookID, countOfReturnedBooks);

            // delete row from IssueBook or update BookReturnedOn
            this.bookDalManager.ReturnBook(book.BookID, currentUserID);
        }
 /// <summary>
 /// The add new book event handler.
 /// </summary>
 /// <param name="book">
 /// The book.
 /// </param>
 private void AddNewBookEventHandler(Book book)
 {
     BooksAvailableForIssue.Add(book);
 }
        /// <summary>
        /// The add book.
        /// </summary>
        private void AddBook()
        {
            Book book = new Book();
            book.Author = this.Author;
            book.Title = this.Title;
            book.QuantityOfBooksIssued = this.SelectedQuantity;
            book.Category = this.SelectedCategory.CategoryName;
            Book newBook = this.bookManager.CreateBook(book);

            this._eventAggregator.GetEvent<CreateBookEvent>().Publish(newBook);

            // Empty fields
            this.Author = string.Empty;
            this.Title = string.Empty;
            this.SelectedCategory = null;

            // TODO empty SelectedQuantity
            this.SelectedQuantity = new int();
        }
 /// <summary>
 /// The add new book event handler.
 /// </summary>
 /// <param name="book">
 /// The book.
 /// </param>
 private void AddNewBookEventHandler(Book book)
 {
     Books.Add(book);
 }
        /// <summary>
        /// The parse book from data table and binding with associated category.
        /// </summary>
        /// <param name="dataTable">
        /// The data table.
        /// </param>
        /// <returns>
        /// The <see cref="List"/>.
        /// </returns>
        public List<Book> ParseBookFromDataTable(DataTable dataTable)
        {
            List<Book> books = new List<Book>();

            foreach (DataRow dataRow in dataTable.Rows)
            {
                Book book = new Book();
                book.BookID = int.Parse(dataRow["BookID"].ToString());
                book.Author = dataRow["Author"].ToString();
                book.Title = dataRow["Title"].ToString();
                book.QuantityOfBooksIssued = int.Parse(dataRow["QuantityOfBooksIssued"].ToString());
                book.Category = this.GetCategoryNameByBookID(book.BookID);
                books.Add(book);
            }

            return books;
        }
        /// <summary>
        /// The get book by book id.
        /// </summary>
        /// <param name="bookID">
        /// The book id.
        /// </param>
        /// <returns>
        /// The <see cref="Book"/>.
        /// </returns>
        /// <exception cref="Exception">
        /// </exception>
        public Book GetBookByBookID(int bookID)
        {
            Book book = new Book();
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@BookID", SqlDbType.Int) { Value = bookID };
            try
            {
                // TODO create procedure GetBookByBookID
                DataTable dataTable = this.sqlDalManager.SelectProcedure("GetBookByBookID", sqlParameters);

                // int categoryID = new int();
                if (dataTable.Rows.Count > 0)
                {
                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        book.BookID = int.Parse(dataRow["BookID"].ToString());
                        book.Author = dataRow["Author"].ToString();
                        book.Title = dataRow["Title"].ToString();
                        book.QuantityOfBooksIssued = int.Parse(dataRow["QuantityOfBooksIssued"].ToString());
                    }
                }

                // book.Category = this.categoryDalManager.GetCategoryNameByCategoryID(categoryID);
            }
            catch (Exception exception)
            {
                throw exception;
            }

            return book;
        }