Exemple #1
0
 public void ClearRepository()
 {
     bookRepository.Remove(book);
     readerRepository.Remove(reader);
     librarianRepository.Remove(librarian);
     libraryDepartmentRepository.Remove(department);
 }
        public void deleteButton_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int  index     = dataGridView1.SelectedRows[0].Index;
                int  id        = 0;
                bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);
                if (converted == false)
                {
                    return;
                }

                var readerRepository         = new ReaderRepository();
                var dealRepository           = new DealRepository();
                var bookCopyInDealRepository = new BookCopyInDealRepository();

                List <Deal>           deals           = dealRepository.GetDealsByReaderId(id);
                List <BookCopyInDeal> bookCopyInDeals = bookCopyInDealRepository.GetBookCopiesInDealByReaderId(id);

                for (int i = 0; i < bookCopyInDeals.Count; i++)
                {
                    bookCopyInDealRepository.Remove(bookCopyInDeals[i].Id);
                }

                for (int i = 0; i < deals.Count; i++)
                {
                    dealRepository.Remove(deals[i].Id);
                }

                readerRepository.Remove(id);
                _db.SaveChanges();
                SetDataGridView();

                MessageBox.Show("Объект удален");
            }
        }
Exemple #3
0
 public void ClearRepository()
 {
     readerRepository.Remove(reader2);
     readerRepository.Remove(reader1);
 }
Exemple #4
0
        public MainViewController(DbContext dbContext)
        {
            booksRepo   = new BookRepository(dbContext);
            readersRepo = new ReaderRepository(dbContext);
            authorsRepo = new AuthorRepository(dbContext);


            CommandsHandler = new Dictionary <string, Func <MainViewArgs, object> >
            {
                #region booksCRUD
                {
                    "get_books",
                    x =>
                    {
                        string res = "";
                        foreach (var book in booksRepo.GetAll())
                        {
                            res += (book.ToString() + "\n");
                        }
                        return(res);
                    }
                },
                {
                    "get_book",
                    x =>
                    {
                        return(booksRepo.Get(x.BookId).ToString());
                    }
                },
                {
                    "delete_book",
                    x =>
                    {
                        booksRepo.Remove(x.BookId);
                        return(okMessage);
                    }
                },
                {
                    "insert_book",
                    x =>
                    {
                        booksRepo.CreateOrUpdate(x.Book);
                        return(okMessage);
                    }
                },
                {
                    "update_book",
                    x =>
                    {
                        booksRepo.CreateOrUpdate(x.Book);
                        return(okMessage);
                    }
                },
                #endregion
                #region booksAuthors
                {
                    "AddAuthorToBook",
                    x =>
                    {
                        booksRepo.Get(x.BookId).Authors.Add(authorsRepo.Get(x.AuthorId));
                        return(okMessage);
                    }
                },
                {
                    "DeleteAuthorFromBook",
                    x =>
                    {
                        booksRepo.Get(x.BookId).Authors.Remove(authorsRepo.Get(x.AuthorId));
                        return(okMessage);
                    }
                },
                {
                    "GetAuthorsOfBook",
                    x =>
                    {
                        string res = "";
                        foreach (var author in booksRepo.Get(x.BookId).Authors)
                        {
                            res += (author.ToString() + "\n");
                        }
                        return(res);
                    }
                },
                #endregion
                #region booksReaders
                {
                    "AddReaderToBook",
                    x =>
                    {
                        booksRepo.Get(x.BookId).Readers.Add(readersRepo.Get(x.ReaderId));
                        return(okMessage);
                    }
                },
                {
                    "DeleteReaderFromBook",
                    x =>
                    {
                        booksRepo.Get(x.BookId).Readers.Remove(readersRepo.Get(x.ReaderId));
                        return(okMessage);
                    }
                },
                {
                    "GetReadersOfBook",
                    x =>
                    {
                        string res = "";
                        foreach (var reader in booksRepo.Get(x.BookId).Readers)
                        {
                            res += (reader.ToString() + "\n");
                        }
                        return(res);
                    }
                },
                #endregion
                #region authorsCRUD
                {
                    "get_authors",
                    x =>
                    {
                        string res = "";
                        foreach (var author in authorsRepo.GetAll())
                        {
                            res += (author.ToString() + "\n");
                        }
                        return(res);
                    }
                },
                {
                    "get_author",
                    x =>
                    {
                        return(authorsRepo.Get(x.AuthorId).ToString());
                    }
                },
                {
                    "delete_author",
                    x =>
                    {
                        authorsRepo.Remove(x.AuthorId);
                        return(okMessage);
                    }
                },
                {
                    "insert_author",
                    x =>
                    {
                        authorsRepo.CreateOrUpdate(x.Author);
                        return(okMessage);
                    }
                },
                {
                    "update_author",
                    x =>
                    {
                        authorsRepo.CreateOrUpdate(x.Author);
                        return(okMessage);
                    }
                },
                {
                    "get_authorsbooks",
                    x =>
                    {
                        string res = "";
                        foreach (var book in authorsRepo.Get(x.AuthorId).Books)
                        {
                            res += (book.ToString() + "\n");
                        }
                        return(res);
                    }
                },
                #endregion
                #region readersCRUD
                {
                    "get_readers",
                    x =>
                    {
                        string res = "";
                        foreach (var reader in readersRepo.GetAll())
                        {
                            res += (reader.ToString() + "\n");
                        }
                        return(res);
                    }
                },
                {
                    "get_reader",
                    x =>
                    {
                        return(readersRepo.Get(x.ReaderId).ToString());
                    }
                },
                {
                    "delete_reader",
                    x =>
                    {
                        readersRepo.Remove(x.ReaderId);
                        return(okMessage);
                    }
                },
                {
                    "insert_reader",
                    x =>
                    {
                        readersRepo.CreateOrUpdate(x.Reader);
                        return(okMessage);
                    }
                },
                {
                    "update_reader",
                    x =>
                    {
                        readersRepo.CreateOrUpdate(x.Reader);
                        return(okMessage);
                    }
                },
                {
                    "get_readersbooks",
                    x =>
                    {
                        string res = "";
                        foreach (var book in readersRepo.Get(x.ReaderId).Books)
                        {
                            res += (book.ToString() + "\n");
                        }
                        return(res);
                    }
                },
                #endregion
                {
                    "add_random_authors",
                    x =>
                    {
                        return(GetBooLReturnMessage(authorsRepo.AddRandomAuthorsToDB(x.RandomCount)));
                    }
                },
                {
                    "search_books",
                    x =>
                    {
                        string res = "";
                        foreach (var book in booksRepo.SearchBooks(x.SearchBookParameters.title, x.SearchBookParameters.minPagesCount, x.SearchBookParameters.maxPagesCount))
                        {
                            res += (book.ToString() + "\n");
                        }
                        return(res);
                    }
                },
                {
                    "search_authors",
                    x =>
                    {
                        string res = "";
                        foreach (var author in authorsRepo.SearchAuthors(x.SearchAuthorParameters.Signature, x.SearchAuthorParameters.Name, x.SearchAuthorParameters.MinDate))
                        {
                            res += (author.ToString() + "\n");
                        }
                        return(res);
                    }
                },
                {
                    "search_readers",
                    x =>
                    {
                        string res = "";
                        foreach (var reader in readersRepo.SearchReaders(x.SearchReaderParameters.Favouritegenre, x.SearchReaderParameters.Name, x.SearchReaderParameters.Surname))
                        {
                            res += (reader.ToString() + "\n");
                        }
                        return(res);
                    }
                },
            };
        }
 public void SaveTest()
 {
     readerRepository.Save(reader1);
     Assert.AreEqual(readerRepository.GetItems().Count(), countItems + 1);
     readerRepository.Save(reader2);
     Assert.AreEqual(readerRepository.GetItems().Count(), countItems + 2);
     readerRepository.Save(reader2);
     Assert.AreEqual(readerRepository.GetItems().Count(), countItems + 2);
     readerRepository.Remove(reader2);
     readerRepository.Remove(reader1);
 }