예제 #1
0
        /// <summary>
        /// Delete a book from tabBook
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteSelectedBook_Click(object sender, EventArgs e)
        {
            //check if book has been returned
            List <ViewBookBorrowedModel> _bookBorrowed = bl.CheckBookSelected(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());

            if (_bookBorrowed.Count > 0)
            {
                //Book borrowed can't delete
                MessageBox.Show("You can't delete a book still borrowed");
            }
            else
            {
                List <TabReservedModel> _bookReserved = bl.ListBooksReservedByISBN(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                if (_bookReserved.Count > 0)
                {
                    DialogResult dialogResult = MessageBox.Show("WARNING ! Deleting this book \n" +
                                                                "will prevent " + _bookReserved.Count + " user(s) to borrowed it\n\n" +
                                                                "----- By deleting a book you will loose any stats relating to this book -----\n\nWould you like to proceed anyway ?", "Warning Message !...", MessageBoxButtons.OKCancel);
                    if (dialogResult == DialogResult.OK)
                    {
                        MessageBox.Show("The book: " + dataGridView1.SelectedRows[0].Cells[1].Value.ToString() + " has been remove from the database", "A message from AIT Library");
                        // deleting a book will result to
                        //delete from TabReserve, TabBorrowed, TabBook
                        result2 = bl.DeleteFromTabReservedByISBN(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                        result1 = bl.DeleteFromTabBorrowed(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                        result  = bl.DeleteFromTabBook(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                        dataGridView1.DataSource = bl.ListBooks();
                    }
                    else if (dialogResult == DialogResult.Cancel)
                    {
                        //do nothing
                    }
                }
                else
                {
                    int          result       = 0;
                    DialogResult dialogResult = MessageBox.Show("Are you sure ?\nThis book will be remove from the database\n\n----- By deleting a book you will loose any stats relating to this book -----", "Confirmation...", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        MessageBox.Show("The book: " + dataGridView1.SelectedRows[0].Cells[1].Value.ToString() + " has been remove from the database", "A message from AIT Library");
                        result2 = bl.DeleteFromTabReservedByISBN(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                        result1 = bl.DeleteFromTabBorrowed(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                        result  = bl.DeleteFromTabBook(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                        dataGridView1.DataSource = bl.ListBooks();
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        //do nothing
                    }
                }
            }
        }
        /// <summary>
        /// join table TabBorrowed (uid - >isbn) and ViewBook (isbn) and add to datagridview
        /// </summary>
        /// <param name="UID">int, condition</param>
        private void parseDataGridViewBorrowed(int UID)
        {
            //Get list of all the book borrowed by the user (UID)
            List <TabBorrowModel> _bookBorrowed = bl.ListBooksBorrowedByUID(UID);
            //create a datatable to store all the data
            DataTable dtable = new DataTable();
            DataRow   dRow;

            //Add the name of the colum for display
            dtable.Columns.Add(new DataColumn("BID"));
            dtable.Columns.Add(new DataColumn("Borrow on"));
            dtable.Columns.Add(new DataColumn("Return date"));
            dtable.Columns.Add(new DataColumn("Isbn"));
            dtable.Columns.Add(new DataColumn("Book Name"));
            dtable.Columns.Add(new DataColumn("Author"));
            dtable.Columns.Add(new DataColumn("Language"));
            dtable.Columns.Add(new DataColumn("Category"));

            if (isbn_txtbox.Text != "")
            {
                //check if book has been return or still borrowed
                List <ViewBookBorrowedModel> _bookBorrowedRunning = bl.CheckBookSelected(isbn_txtbox.Text);
                if (_bookBorrowedRunning.Count > 0)
                {
                    //Get name of the book from ViewBook with ISBN
                    List <ViewBookModel> _bookBorrowed2 = bl.GetBookInfo(isbn_txtbox.Text);
                    dRow = dtable.NewRow();
                    string borrowDate = _bookBorrowed[0].borrowDate;
                    borrowDate = Convert.ToDateTime(borrowDate).ToString("dd MMM yyyy");
                    string returnDate = _bookBorrowed[0].returnDate;
                    returnDate          = Convert.ToDateTime(returnDate).ToString("dd MMM yyyy");
                    dRow["BID"]         = _bookBorrowed[0].bid;
                    dRow["Borrow on"]   = borrowDate;
                    dRow["Return date"] = returnDate;
                    dRow["Isbn"]        = _bookBorrowed2[0].Isbn;
                    dRow["Book Name"]   = _bookBorrowed2[0].BookName;
                    dRow["Author"]      = _bookBorrowed2[0].AuthorName;
                    dRow["Language"]    = _bookBorrowed2[0].LanguageName;
                    dRow["Category"]    = _bookBorrowed2[0].CategoryName;
                    dtable.Rows.Add(dRow);
                }
            }
            else
            {
                for (int i = 0; i < _bookBorrowed.Count(); i++)
                {
                    ISBN = _bookBorrowed[i].isbn;

                    //check if book has been return or still borrowed
                    List <ViewBookBorrowedModel> _bookBorrowedRunning = bl.CheckBookSelected(ISBN);
                    if (_bookBorrowedRunning.Count > 0)
                    {
                        //Get name of the book from ViewBook with ISBN
                        List <ViewBookModel> _bookBorrowed2 = bl.GetBookInfo(ISBN);
                        dRow = dtable.NewRow();
                        string borrowDate = _bookBorrowed[i].borrowDate;
                        borrowDate = Convert.ToDateTime(borrowDate).ToString("dd MMM yyyy");
                        string returnDate = _bookBorrowed[i].returnDate;
                        returnDate          = Convert.ToDateTime(returnDate).ToString("dd MMM yyyy");
                        dRow["BID"]         = _bookBorrowed[i].bid;
                        dRow["Borrow on"]   = borrowDate;
                        dRow["Return date"] = returnDate;
                        dRow["Isbn"]        = _bookBorrowed2[0].Isbn;
                        dRow["Book Name"]   = _bookBorrowed2[0].BookName;
                        dRow["Author"]      = _bookBorrowed2[0].AuthorName;
                        dRow["Language"]    = _bookBorrowed2[0].LanguageName;
                        dRow["Category"]    = _bookBorrowed2[0].CategoryName;
                        dtable.Rows.Add(dRow);
                    }
                }
            }
            //Add datatable to datagridview
            bookBorrowed_dataGridView.DataSource = dtable;
            this.bookBorrowed_dataGridView.Columns[0].Visible = false;
        }
        public void btnReserveBookSelected_Click(object sender, EventArgs e)
        {
            bool      borrowedAllowed = true;
            BookLogic bl = new BookLogic();
            //check if the book is not available, if available we can reserve it
            List <ViewBookBorrowedModel> _bookBorrowed = bl.CheckBookSelected(showBooks_dataGridView.SelectedRows[0].Cells[0].Value.ToString());

            //only admin and supervisor
            if (staticUserLevel == 2 || staticUserLevel == 3)
            {
                try
                {
                    //check if userID is not empty
                    if (userID_input.Text == "")
                    {
                        MessageBox.Show("User ID is empty, you need to fill it first", "A message from AIT Library");
                        borrowedAllowed = false;
                    }
                    else
                    {
                        //check if user exist
                        UserLogic           ul          = new UserLogic();
                        List <TabUserModel> _userSearch = ul.GetUserInfoByUID(Int32.Parse(userID_input.Text));
                        if (_userSearch.Count > 0)
                        {
                            //try to parse to INT
                            staticUID = Int32.Parse(userID_input.Text);
                        }
                        else
                        {
                            MessageBox.Show("No user correspond to your criteria", "Warning !");
                            borrowedAllowed = false;
                        }
                    }
                }
                catch (FormatException)
                {
                    MessageBox.Show("User ID must be a valid number", "A message from AIT Library");
                    borrowedAllowed = false;
                }
            }
            if (borrowedAllowed == true)
            {
                if (_bookBorrowed.Count < 1)
                {
                    DialogResult dialogResult = MessageBox.Show("Sorry ! You can't reserve a book available.\n\nWould you like to borrowed it ?", "Please make a choice", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        btnBorrowBookSelected_click(null, e);
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        //do nothing
                    }
                }
                else
                {
                    //we check if user not have already the book in his possession
                    List <TabBorrowModel> _bookBorrowedAlready = bl.ListBooksBorrowedByUIDIsbnActual(staticUID, showBooks_dataGridView.SelectedRows[0].Cells[0].Value.ToString());
                    if (_bookBorrowedAlready.Count > 0)
                    {
                        MessageBox.Show("Sorry ! You can't reserved a book in your possession", "A message from AIT Library");
                    }
                    else
                    {
                        //get isbn
                        List <ViewBookModel> _book = bl.GetBookInfo(showBooks_dataGridView.SelectedRows[0].Cells[0].Value.ToString());
                        ISBN = _book[0].Isbn;
                        //check if user already has reserved this book
                        List <TabReservedModel> _bookReserved = bl.CheckBooksReserved(staticUID, ISBN);
                        if (_bookReserved.Count > 0)
                        {
                            string   reservedDate = _bookReserved[0].reservedDate;
                            DateTime dateReserved = DateTime.Parse(reservedDate);
                            MessageBox.Show("Sorry, You have already reserved this book on " + dateReserved.ToString("dddd, dd MMMM yyyy"), "Error");
                        }
                        else
                        {
                            //add book to tabReserved
                            DateTime todayDate         = DateTime.Now;
                            string   todayReservedDate = todayDate.ToString("dddd, dd MMMM yyyy HH:mm:ss");
                            bl.InsertBookTabReserved(staticUID, ISBN, todayReservedDate);
                            MessageBox.Show("You just reserved the book " + showBooks_dataGridView.SelectedRows[0].Cells[1].Value.ToString(), "Reservation Confirmation");
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Borrow a book
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBorrowBookSelected_click(object sender, EventArgs e)
        {
            bool      borrowedAllowed = true;
            BookLogic bl = new BookLogic();

            List <ViewBookBorrowedModel> _bookBorrowed = bl.CheckBookSelected(showBooks_dataGridView.SelectedRows[0].Cells[0].Value.ToString());

            if (staticUserLevel == 2 || staticUserLevel == 3)
            {
                try
                {
                    //check if userID is not empty
                    if (userID_input.Text == "")
                    {
                        MessageBox.Show("User ID is empty, you need to fill it first", "A message from AIT Library");
                        borrowedAllowed = false;
                    }
                    else
                    {
                        //check if user exist
                        UserLogic           ul          = new UserLogic();
                        List <TabUserModel> _userSearch = ul.GetUserInfoByUID(Int32.Parse(userID_input.Text));
                        if (_userSearch.Count > 0)
                        {
                            //try to parse to INT
                            staticUID = Int32.Parse(userID_input.Text);
                        }
                        else
                        {
                            MessageBox.Show("No user correspond to your criteria", "Warning !");
                            borrowedAllowed = false;
                        }
                    }
                }
                catch (FormatException)
                {
                    MessageBox.Show("User ID must be a valid number", "A message from AIT Library");
                    borrowedAllowed = false;
                }
            }
            //If no error then we proceed to the rest of the script
            if (borrowedAllowed == true)
            {
                if (_bookBorrowed.Count < 1)
                {
                    List <ViewBookModel> _book = bl.GetBookInfo(showBooks_dataGridView.SelectedRows[0].Cells[0].Value.ToString());
                    ISBN = _book[0].Isbn;
                    string BookName     = _book[0].BookName;
                    string CategoryName = _book[0].CategoryName;
                    string Publisher    = _book[0].Publisher;
                    int    PublishYear  = _book[0].PublisherYear;
                    int    Page         = _book[0].Pages;
                    string AuthorName   = _book[0].AuthorName;
                    string LanguageName = _book[0].LanguageName;

                    List <TabBookModel> _bookID = bl.GetTabBookInfo(showBooks_dataGridView.SelectedRows[0].Cells[0].Value.ToString());
                    int Author   = _bookID[0].AuthorID;
                    int Category = _bookID[0].CategoryID;
                    int Language = _bookID[0].LanguageID;

                    //string BorrowDate = DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss");
                    DateTime borrowDate = DateTime.Now;
                    DateTime returnDate = borrowDate.AddDays(3);
                    string   BorrowDate = borrowDate.ToString("dddd, dd MMMM yyyy HH:mm:ss");
                    string   ReturnDate = returnDate.ToString("dddd, dd MMMM yyyy HH:mm:ss");

                    MessageBox.Show("Book Borrowed: " + showBooks_dataGridView.SelectedRows[0].Cells[1].Value.ToString() +
                                    "\n-----------------------\nBorrow date: " + BorrowDate + "\nReturn Date: " + ReturnDate, "Book Confirmation");
                    bl.InsertBookTabBorrow(staticUID, ISBN, BorrowDate, ReturnDate, "2000-01-01", 0);
                }
                //Book not available so we ask the user if he want to reserve it
                else
                {
                    DialogResult dialogResult = MessageBox.Show("Sorry ! This book is not available.\n\nWould you like to reserved it ?", "Please make a choice", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        //we check if user not have already the book in his possession
                        List <TabBorrowModel> _bookBorrowedAlready = bl.ListBooksBorrowedByUIDIsbnActual(staticUID, showBooks_dataGridView.SelectedRows[0].Cells[0].Value.ToString());
                        if (_bookBorrowedAlready.Count > 0)
                        {
                            MessageBox.Show("Sorry ! You can't reserved a book in your possession", "A message from AIT Library");
                        }
                        else
                        {
                            //get isbn
                            List <ViewBookModel> _book = bl.GetBookInfo(showBooks_dataGridView.SelectedRows[0].Cells[0].Value.ToString());
                            ISBN = _book[0].Isbn;
                            //check if user already has reserved this book
                            List <TabReservedModel> _bookReserved = bl.CheckBooksReserved(staticUID, ISBN);
                            if (_bookReserved.Count > 0)
                            {
                                string   reservedDate = _bookReserved[0].reservedDate;
                                DateTime dateReserved = DateTime.Parse(reservedDate);
                                MessageBox.Show("Sorry, You have already reserved this book on " + dateReserved.ToString("dddd, dd MMMM yyyy"), "Error");
                            }
                            else
                            {
                                //add book to tabReserved
                                DateTime todayDate         = DateTime.Now;
                                string   todayReservedDate = todayDate.ToString("dddd, dd MMMM yyyy HH:mm:ss");
                                bl.InsertBookTabReserved(staticUID, ISBN, todayReservedDate);
                                MessageBox.Show("You just reserved the book " + showBooks_dataGridView.SelectedRows[0].Cells[1].Value.ToString(), "Reservation Confirmation");
                            }
                        }
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        //do nothing
                    }
                }

                //refresh list user with image
                loadDataGridViewWithImage(null, e);
            }
        }