Example #1
0
        private void addBookButton_Click(object sender, EventArgs e)
        {
            var          addBookForm  = new AddBookForm();
            DialogResult dialogResult = addBookForm.ShowDialog(this);

            if (dialogResult == DialogResult.Cancel)
            {
                return;
            }

            string name        = addBookForm.nameTextBox.Text;
            string editor      = addBookForm.editorTextBox.Text;
            string type        = addBookForm.typeTextBox.Text;
            string genre       = addBookForm.genreTextBox.Text;
            string language    = addBookForm.languageTextBox.Text;
            var    editionYear = new DateTime((int)addBookForm.editionNumericUpDown.Value, 1, 1);

            BookRepository bookRepository = new BookRepository();

            var book = new Book
            {
                Name        = name,
                EditorsName = editor,
                EditionYear = editionYear,
                Type        = type,
                Genre       = genre,
                Language    = language
            };

            bookRepository.Create(book);

            int        id = 0;
            var        bookAuthorRepository = new BookAuthorRepository();
            BookAuthor bookAuthor;


            foreach (object ch in addBookForm.authorsCheckedListBox.CheckedItems)
            {
                id = int.Parse(ch.ToString().Split(' ')[0]);

                bookAuthor = new BookAuthor
                {
                    BookId   = book.Id,
                    AuthorId = id
                };

                bookAuthorRepository.Create(bookAuthor);
            }

            int      booksCount         = (int)addBookForm.countNumericUpDown.Value;
            var      bookCopyRepository = new BookCopyRepository();
            BookCopy bookCopy;

            for (int i = 0; i < booksCount; i++)
            {
                bookCopy = new BookCopy
                {
                    BookId     = book.Id,
                    CopyNumber = i + 1
                };

                bookCopyRepository.Create(bookCopy);
            }

            SetDataGridView();
            _db.SaveChanges();
        }