Ejemplo n.º 1
0
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            // First, we need to create the book, as the author list demands an id of the book
            if (textBoxName.Text.Length > 0 && textBoxISBN.Text.Length > 0 && textBoxGenre.Text.Length > 0 &&
                textBoxName.Text.Length < 63 && textBoxISBN.Text.Length < 15 && textBoxGenre.Text.Length < 31)
            {
                var bookToAdd = new Book();
                bookToAdd.Name      = textBoxName.Text;
                bookToAdd.ISBN      = textBoxISBN.Text;
                bookToAdd.Genre     = textBoxGenre.Text;
                bookToAdd.DateOfAdd = DateTime.Now;
                //Jak z tymi kopiami robimy

                context.Books.InsertOnSubmit(bookToAdd);
                context.SubmitChanges();

                //As we have just created the book, we check the id of it
                var bookId = from Book in context.Books
                             where (Book.Name == textBoxName.Text) && (Book.ISBN == textBoxISBN.Text) &&
                             (Book.Genre == textBoxGenre.Text)
                             select Book.Id;

                int quantity = int.Parse(textBoxCopies.Text);
                for (int i = 0; i < quantity; i++)
                {
                    Copy copyToAdd = new Copy();
                    copyToAdd.BookId = bookId.FirstOrDefault();
                    copyToAdd.Status = "Dostępna";
                    context.Copies.InsertOnSubmit(copyToAdd);
                    context.SubmitChanges();
                }

                List <int> authors = new List <int>();
                //checking if the Athor is already in DB
                foreach (var element in listOfAuthors)
                {
                    var author = from auth in context.Authors
                                 where (auth.Firstname == element.Firstname) && (auth.Lastname == element.Lastname)
                                 select auth.Id;

                    var authorFirst = author.FirstOrDefault();

                    if (authorFirst != 0)
                    {
                        var authorList = new AuthorList();
                        authorList.AuthorId = authorFirst;
                        authorList.BookId   = bookId.FirstOrDefault();

                        context.AuthorLists.InsertOnSubmit(authorList);
                        context.SubmitChanges();
                    }
                    else
                    {
                        var id = (from auth in context.Authors
                                  select auth.Id).Max();

                        var authorToAdd = new Author();
                        authorToAdd.Firstname = element.Firstname;
                        authorToAdd.Lastname  = element.Lastname;

                        context.Authors.InsertOnSubmit(authorToAdd);
                        context.SubmitChanges();

                        var addedAuthor = (from authrs in context.Authors
                                           where authrs.Lastname == element.Lastname
                                           select authrs.Id).FirstOrDefault();

                        var authorList = new AuthorList()
                        {
                            AuthorId = addedAuthor,
                            BookId   = bookId.FirstOrDefault()
                        };

                        context.AuthorLists.InsertOnSubmit(authorList);
                        context.SubmitChanges();
                    }
                }
            }
            else
            {
                MessageBox.Show("Zły format danych", "Błąd!");
            }
            this.Close();
        }
Ejemplo n.º 2
0
 private void detach_AuthorLists(AuthorList entity)
 {
     this.SendPropertyChanging();
     entity.Book = null;
 }
Ejemplo n.º 3
0
 private void attach_AuthorLists(AuthorList entity)
 {
     this.SendPropertyChanging();
     entity.Book = this;
 }
Ejemplo n.º 4
0
 partial void DeleteAuthorList(AuthorList instance);
Ejemplo n.º 5
0
 partial void UpdateAuthorList(AuthorList instance);
Ejemplo n.º 6
0
 partial void InsertAuthorList(AuthorList instance);