private void BookTable_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int bookId = Convert.ToInt32(bookTable.Rows[e.RowIndex].Cells[0].Value);

            using (LibraryEntities db = new LibraryEntities())
            {
                bookFound = db.Books.Where(b => b.Id == bookId).FirstOrDefault();
                List <Models.AuthorsBooks> abList = db.AuthorsBooks.ToList();

                if (bookFound != null)
                {
                    nameBox.Text = bookFound.Name;
                    //foreach(var y in abList)
                    //{
                    //    if(bookFound.Id == y.BookId)
                    //    {
                    //        authorDrop.SelectedText = y.Authors.Name;
                    //    }
                    //}
                    priceBox.Text             = Convert.ToString(bookFound.Price);
                    quantityBox.Text          = Convert.ToString(bookFound.Quantity);
                    categoryDrop.SelectedItem = bookFound.Categories.Name;
                }
                saveBtn.Text      = "Update";
                deleteBtn.Enabled = true;
            }
        }
        public ActionResult AddBookSubmit()
        {
            HttpPostedFileBase file = Request.Files["picture"];

            // Initiate a class instance
            Books book = new Books();

            // Save the book added
            string title = Request.Form["title"];
            string auteur = Request.Form["author"];
            int authorNum = Int32.Parse(auteur);
            string synopsis = Request.Form["synopsis"];

            string picture = "";
            if (file != null && file.ContentLength > 0)
            {
                string fname = System.IO.Path.GetFileName(file.FileName);
                file.SaveAs(Server.MapPath(System.IO.Path.Combine("~/Content/img", fname)));
                picture = "../Content/img/" + fname;

                book.AddBooks(title, authorNum, picture, synopsis);
            }
            else
            {
                book.AddBooks(title, authorNum, synopsis);
            }

            return RedirectToAction("Books");
        }
        private void DeleteCategory(object sender, EventArgs e)
        {
            using (LibraryEntities db = new LibraryEntities())
            {
                int          bookId = Convert.ToInt32(bookTable.SelectedRows[0].Cells[0].Value);
                Models.Books b1     = db.Books.Where(b => b.Id == bookId).FirstOrDefault();
                db.Books.Remove(b1);
                db.SaveChanges();
            }

            FillBooks();
            Reset();
        }
        // GET: Books
        public ActionResult Books()
        {
            // Initiate a class instance
            Books book = new Books();

            // Get all the books from the database
            var books = new List<Tuple<string, string, string, string>>();
            books = book.GetAllBooks();

            ViewBag.Books = books;
            ViewBag.BookObject = book;

            return View();
        }
        public ActionResult AuthorBooks(string id)
        {
            // Initiate a class instance
            Authors author = new Authors();

            // Get author's books
            var authorBooks = new List<Tuple<string, string, string, string>>();
            authorBooks = author.GetAuthorBooks(id);

            Books book = new Books();
            string authorName;
            authorName = book.GetNameAuthor(id);

            ViewBag.AuthorBooks = authorBooks;
            ViewBag.AuthorName = authorName;

            return View();
        }
        public ActionResult Book(string id)
        {
            // Initiate a class instance
            Books book = new Books();

            // Get the book's data
            var dataBook = new List<Tuple<string, string, string, string, string>>();
            dataBook = book.GetBookById(id);

            string title = book.GetBookTitle(id);

            string author = book.GetNameAuthor(id);

            ViewBag.DataBook = dataBook;
            ViewBag.Title = title;
            ViewBag.Author = author;
            ViewBag.BookObject = book;

            return View();
        }
        private void AddBook(object sender, EventArgs e)
        {
            if (saveBtn.Text == "Save")
            {
                Regex letters       = new Regex(@"^[A-Za-z ]+$");
                Match bookNameMatch = letters.Match(nameBox.Text);

                Regex price      = new Regex(@"^[0-9.]*$");
                Match priceMatch = price.Match(priceBox.Text);

                Regex numbers       = new Regex(@"^[0-9]*$");
                Match quantityMatch = numbers.Match(quantityBox.Text);

                if (!bookNameMatch.Success)
                {
                    MessageBox.Show("Book name contains only letters");
                }
                else if (!priceMatch.Success)
                {
                    MessageBox.Show("Price contains only numbers and dot");
                }
                else if (!quantityMatch.Success)
                {
                    MessageBox.Show("Quantity contains only numbers");
                }
                else
                {
                    using (LibraryEntities db = new LibraryEntities())
                    {
                        Models.AuthorsBooks ab  = new AuthorsBooks();
                        int          categoryId = (db.Categories.Where(c => c.Name == categoryDrop.SelectedItem.ToString()).FirstOrDefault()).Id;
                        int          authorId   = (db.Authors.Where(a => a.Name + " " + a.Surname == authorDrop.SelectedItem.ToString()).FirstOrDefault()).Id;
                        Models.Books b1         = new Models.Books();
                        b1.Name     = nameBox.Text;
                        ab.BookId   = b1.Id;
                        ab.AuthorId = authorId;
                        db.AuthorsBooks.Add(ab);
                        b1.Price      = Convert.ToDecimal(priceBox.Text);
                        b1.CategoryId = categoryId;
                        b1.Quantity   = Convert.ToInt32(quantityBox.Text);
                        db.Books.Add(b1);
                        db.SaveChanges();
                    }
                    FillBooks();
                    nameBox.Clear();
                    priceBox.Clear();
                    quantityBox.Clear();
                    categoryDrop.SelectedIndex = -1;
                    authorDrop.SelectedIndex   = -1;
                }
            }
            else
            {
                using (LibraryEntities db = new LibraryEntities())
                {
                    int bookId = Convert.ToInt32(bookTable.SelectedRows[0].Cells[0].Value);

                    Models.Books newBook = db.Books.Where(b => b.Id == bookFound.Id).FirstOrDefault();

                    newBook.Id = bookId;
                    Regex letters       = new Regex(@"^[A-Za-z ]+$");
                    Match bookNameMatch = letters.Match(nameBox.Text);

                    Regex price      = new Regex(@"^[0-9.]*$");
                    Match priceMatch = price.Match(priceBox.Text);

                    Regex numbers       = new Regex(@"^[0-9]*$");
                    Match quantityMatch = numbers.Match(quantityBox.Text);

                    if (!bookNameMatch.Success)
                    {
                        MessageBox.Show("Book name contains only letters");
                    }
                    else if (!priceMatch.Success)
                    {
                        MessageBox.Show("Price contains only numbers and dot");
                    }
                    else if (!quantityMatch.Success)
                    {
                        MessageBox.Show("Quantity contains only numbers");
                    }
                    else
                    {
                        newBook.Name            = nameBox.Text;
                        newBook.Categories.Name = (db.Categories.Where(c => c.Name == categoryDrop.SelectedItem.ToString()).FirstOrDefault()).Name;
                        newBook.Price           = Convert.ToDecimal(priceBox.Text);
                        newBook.Quantity        = Convert.ToInt32(quantityBox.Text);
                    }

                    db.SaveChanges();
                    FillBooks();
                    Reset();
                }
            }
        }