Beispiel #1
0
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (!sageAge.Text.All(char.IsDigit))
            {
                MessageBox.Show($"Age should be a number. But is: {sageAge.Text}");
            }
            else if (sageName.Text == null || sageCity == null)
            {
                MessageBox.Show("All fields shooould be filled!");
            }
            else
            {
                Sage _sage = new Sage {
                    Name = sageName.Text, City = sageCity.Text, Age = int.Parse(sageAge.Text)
                };
                using (SgDbContext sbc = new SgDbContext())
                {
                    if (sbc.Sages.Where(s => s.Age == _sage.Age && s.Name == _sage.Name && s.City == _sage.City).ToList().Count() != 0)
                    {
                        MessageBox.Show($"Sage {_sage} already exist. Please add new or cancel");
                    }
                    else
                    {
                        if (IsNewItem)
                        {
                            if (sbc.Sages.Any(s => s.Name == _sage.Name && s.City == _sage.City && s.Age == _sage.Age))
                            {
                                MessageBox.Show("Sage already exists.Please edit your form!");
                            }
                            else
                            {
                                sbc.Sages.Add(_sage);
                            }
                        }
                        else
                        {
                            Sage editedSage = sbc.Sages.Where(p => p.Id == SageId).FirstOrDefault();
                            sbc.Sages.Attach(editedSage);
                            editedSage.Name = SageDefaultName;
                            editedSage.City = SageDefaultCity;
                            editedSage.Age  = int.Parse(SageDefaultAge);
                        }

                        sbc.SaveChanges();
                    }

                    Close();
                }
            }
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            string bookName        = ((TextBox)FindName("bookNameField")).Text;
            string bookDescription = ((RichTextBox)FindName("bookDescriptionField")).Document.Blocks.FirstOrDefault().ToString();

            if (bookName == "")
            {
                MessageBox.Show("Please enter the book name");
            }
            if (sagesList.Items.Count == 0)
            {
                MessageBox.Show("The book has no author. Please add at least one author!");
            }

            using (SgDbContext sbc = new SgDbContext())
            {
                if (IsNewItem)
                {
                    Book book = new Book {
                        Title = bookName, Description = bookDescription, Sages = SelectedSages
                    };
                    if (sbc.Books.Any(b => b.Title == book.Title && b.Description == book.Description && b.Sages == book.Sages))
                    {
                        MessageBox.Show("Book already exists");
                    }
                    else
                    {
                        sbc.Books.Add(book);
                    }
                }
                else
                {
                    Book book = sbc.Books.Where(b => b.Id == BookId).FirstOrDefault();
                    sbc.Books.Attach(book);
                    book.Title       = bookNameField.Text;
                    book.Description = bookDescriptionField.Document.Blocks.FirstOrDefault().ToString();
                    book.Sages       = SelectedSages;
                }
                sbc.SaveChanges();
            }

            DialogResult = true;
            Close();
        }