Пример #1
0
        public void Do(string param, Book b, bool reverse = false)
        {
            try
            {
                switch (param)
                {
                    case "delete":
                        using (var c = new Context())
                        {
                            var temp = c.Books.Include("Author").Include("Genre").First(p => p.Title == b.Title && p.Author.Name == b.Author.Name && p.Year == b.Year);
                            RemoveBookFromDB(temp.Id);
                            if (reverse)
                            {
                                Undos.Push(new object[] { "add", temp });
                                OnPropertyChanged("UndoIsEnabled");
                            }
                            else
                            {
                                Redos.Push(new object[] { "add", temp });
                                OnPropertyChanged("RedoIsEnabled");
                            }
                        }
                        break;
                    case "add":
                        using (var c = new Context())
                        {
                            AddBookToDB(b);
                            if (reverse)
                            {
                                Undos.Push(new object[] { "delete", b });
                                OnPropertyChanged("UndoIsEnabled");

                            }
                            else
                            {
                                try
                                {
                                    if ((string)(Undos.Peek()[0]) == "unedit")
                                    {
                                        Undos.Pop();
                                        OnPropertyChanged("UndoIsEnabled");
                                    }
                                }
                                catch { }
                                Redos.Push(new object[] { "delete", b });
                                OnPropertyChanged("RedoIsEnabled");

                            }
                        }
                        break;
                    case "unedit":
                        using (var c = new Context())
                        {
                            var temp = c.Books.Include("Author").Include("Genre").First(p => p.Id == b.Id);
                            if (reverse)
                            {
                                Undos.Push(new object[] { "edit", temp.Clone() });
                                OnPropertyChanged("UndoIsEnabled");

                            }
                            else
                            {
                                Redos.Push(new object[] { "edit", temp.Clone() });
                                OnPropertyChanged("RedoIsEnabled");

                            }

                            temp.PullChanges(b);
                            if (temp.Author != null)
                            {
                                var k = c.Authors.FirstOrDefault(p => p.Name == temp.Author.Name);
                                if (k != null) { temp.Author = k; }
                            }
                            if (temp.Genre != null)
                            {
                                var t = c.Genres.FirstOrDefault(p => p.Name == temp.Genre.Name);
                                if (t != null) { temp.Genre = t; }
                            }
                            c.SaveChanges();
                        }
                        break;
                    case "edit":
                        using (var c = new Context())
                        {

                            var temp = c.Books.Include("Author").Include("Genre").First(p => p.Id == b.Id);
                            if (reverse)
                            {
                                Undos.Push(new object[] { "unedit", temp.Clone() });
                                OnPropertyChanged("UndoIsEnabled");

                            }
                            else
                            {
                                Redos.Push(new object[] { "unedit", temp.Clone() });
                                OnPropertyChanged("RedoIsEnabled");
                            }

                            temp.PullChanges(b);
                            if (temp.Author != null)
                            {
                                var k = c.Authors.FirstOrDefault(p => p.Name == temp.Author.Name);
                                if (k != null) { temp.Author = k; }
                            }
                            if (temp.Genre != null)
                            {
                                var t = c.Genres.FirstOrDefault(p => p.Name == temp.Genre.Name);
                                if (t != null) { temp.Genre = t; }
                            }
                            c.SaveChanges();
                        }
                        break;
                    default:
                        break;
                }
            }
            catch { MessageBox.Show("Последнняя вызванная вами команда могла привести к ошибке во временном пространстве.\n\n  \"Мы вновь спасли этот грешный мир\" - Ваша команда разработчиков"); }
            OpenList(SelectedMode);
        }
Пример #2
0
 private void AddBookToDB(Book b)
 {
     using (var c = new Context())
     {
         if (b.Author != null)
         {
             var k = c.Authors.FirstOrDefault(p => p.Name == b.Author.Name);
             if (k != null) { b.Author = k; }
         }
         if (b.Genre != null)
         {
             var t = c.Genres.FirstOrDefault(p => p.Name == b.Genre.Name);
             if (t != null) { b.Genre = t; }
         }
         c.Books.Add(b);
         c.SaveChanges();
     }
 }
Пример #3
0
 public void OnAdd(Book obj)
 {
     bb.Add(obj);
     dataGrid2.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => dataGrid2.Items.Refresh()));
 }